Skip to content

Commit e717edb

Browse files
Add network policy config. (#1200)
1 parent 016baaa commit e717edb

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

google/resource_container_cluster.go

+31
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ func resourceContainerCluster() *schema.Resource {
160160
},
161161
},
162162
},
163+
"network_policy_config": {
164+
Type: schema.TypeList,
165+
Optional: true,
166+
Computed: true,
167+
MaxItems: 1,
168+
Elem: &schema.Resource{
169+
Schema: map[string]*schema.Schema{
170+
"disabled": {
171+
Type: schema.TypeBool,
172+
Optional: true,
173+
},
174+
},
175+
},
176+
},
163177
},
164178
},
165179
},
@@ -1187,6 +1201,15 @@ func expandClusterAddonsConfig(configured interface{}) *containerBeta.AddonsConf
11871201
ForceSendFields: []string{"Disabled"},
11881202
}
11891203
}
1204+
1205+
if v, ok := config["network_policy_config"]; ok && len(v.([]interface{})) > 0 {
1206+
addon := v.([]interface{})[0].(map[string]interface{})
1207+
ac.NetworkPolicyConfig = &containerBeta.NetworkPolicyConfig{
1208+
Disabled: addon["disabled"].(bool),
1209+
ForceSendFields: []string{"Disabled"},
1210+
}
1211+
}
1212+
11901213
return ac
11911214
}
11921215

@@ -1304,6 +1327,14 @@ func flattenClusterAddonsConfig(c *containerBeta.AddonsConfig) []map[string]inte
13041327
},
13051328
}
13061329
}
1330+
if c.NetworkPolicyConfig != nil {
1331+
result["network_policy_config"] = []map[string]interface{}{
1332+
{
1333+
"disabled": c.NetworkPolicyConfig.Disabled,
1334+
},
1335+
}
1336+
}
1337+
13071338
return []map[string]interface{}{result}
13081339
}
13091340

google/resource_container_cluster_test.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,16 @@ func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) {
146146
),
147147
},
148148
{
149-
Config: testAccContainerCluster_withNetworkPolicyDisabled(clusterName),
149+
Config: testAccContainerCluster_withNetworkPolicyConfigDisabled(clusterName),
150+
Check: resource.ComposeTestCheckFunc(
151+
testAccCheckContainerCluster(
152+
"google_container_cluster.with_network_policy_enabled"),
153+
resource.TestCheckResourceAttr("google_container_cluster.with_network_policy_enabled",
154+
"addons_config.0.network_policy_config.0.disabled", "true"),
155+
),
156+
},
157+
{
158+
Config: testAccContainerCluster_withNetworkPolicyConfigDisabled(clusterName),
150159
PlanOnly: true,
151160
ExpectNonEmptyPlan: false,
152161
},
@@ -1142,6 +1151,11 @@ resource "google_container_cluster" "with_network_policy_enabled" {
11421151
enabled = true
11431152
provider = "CALICO"
11441153
}
1154+
addons_config {
1155+
network_policy_config {
1156+
disabled = false
1157+
}
1158+
}
11451159
}`, clusterName)
11461160
}
11471161

@@ -1165,6 +1179,22 @@ resource "google_container_cluster" "with_network_policy_enabled" {
11651179
}`, clusterName)
11661180
}
11671181

1182+
func testAccContainerCluster_withNetworkPolicyConfigDisabled(clusterName string) string {
1183+
return fmt.Sprintf(`
1184+
resource "google_container_cluster" "with_network_policy_enabled" {
1185+
name = "%s"
1186+
zone = "us-central1-a"
1187+
initial_node_count = 1
1188+
1189+
network_policy = {}
1190+
addons_config {
1191+
network_policy_config {
1192+
disabled = true
1193+
}
1194+
}
1195+
}`, clusterName)
1196+
}
1197+
11681198
func testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName string, cidrs []string) string {
11691199

11701200
cidrBlocks := ""

website/docs/r/container_cluster.html.markdown

+4-1
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,13 @@ The `addons_config` block supports:
169169
* `http_load_balancing` - (Optional) The status of the HTTP (L7) load balancing
170170
controller addon, which makes it easy to set up HTTP load balancers for services in a
171171
cluster. It is enabled by default; set `disabled = true` to disable.
172-
173172
* `kubernetes_dashboard` - (Optional) The status of the Kubernetes Dashboard
174173
add-on, which controls whether the Kubernetes Dashboard is enabled for this cluster.
175174
It is enabled by default; set `disabled = true` to disable.
175+
* `network_policy_config` - (Optional) Whether we should enable the network policy addon
176+
for the master. This must be enabled in order to enable network policy for the nodes.
177+
It can only be disabled if the nodes already do not have network policies enabled.
178+
Set `disabled = true` to disable.
176179

177180
This example `addons_config` disables two addons:
178181

0 commit comments

Comments
 (0)