Skip to content

Commit 39f22ef

Browse files
Sébastien GLONdanawillow
Sébastien GLON
authored andcommitted
[WIP] Add container cluster network policy addon (#630)
* replalce TypeList by TypeSet * Add network policy * test improvement * correct test * Add cluster network polocy enabled * Replalce network_policy addons by global network_policy enabled * Update node_config.go * Update resource_container_cluster.go * clean * clean * Correct PR * COrrect PR * pr * fix test to use same name * add more documentation
1 parent 40c2004 commit 39f22ef

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

google/resource_container_cluster.go

+77
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,28 @@ func resourceContainerCluster() *schema.Resource {
292292
StateFunc: StoreResourceName,
293293
},
294294

295+
"network_policy": {
296+
Type: schema.TypeList,
297+
Optional: true,
298+
Computed: true,
299+
MaxItems: 1,
300+
Elem: &schema.Resource{
301+
Schema: map[string]*schema.Schema{
302+
"enabled": {
303+
Type: schema.TypeBool,
304+
Optional: true,
305+
Default: false,
306+
},
307+
"provider": {
308+
Type: schema.TypeString,
309+
Default: "PROVIDER_UNSPECIFIED",
310+
Optional: true,
311+
ValidateFunc: validation.StringInSlice([]string{"PROVIDER_UNSPECIFIED", "CALICO"}, false),
312+
},
313+
},
314+
},
315+
},
316+
295317
"node_config": schemaNodeConfig,
296318

297319
"node_pool": {
@@ -443,6 +465,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
443465
cluster.Network = network
444466
}
445467

468+
if v, ok := d.GetOk("network_policy"); ok && len(v.([]interface{})) > 0 {
469+
cluster.NetworkPolicy = expandNetworkPolicy(v)
470+
}
471+
446472
if v, ok := d.GetOk("subnetwork"); ok {
447473
cluster.Subnetwork = v.(string)
448474
}
@@ -525,6 +551,9 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
525551
}
526552

527553
d.Set("name", cluster.Name)
554+
555+
d.Set("network_policy", flattenNetworkPolicy(cluster.NetworkPolicy))
556+
528557
d.Set("zone", cluster.Zone)
529558

530559
locations := []string{}
@@ -802,6 +831,29 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
802831
d.SetPartial("monitoring_service")
803832
}
804833

834+
if d.HasChange("network_policy") {
835+
np, _ := d.GetOk("network_policy")
836+
837+
req := &container.SetNetworkPolicyRequest{
838+
NetworkPolicy: expandNetworkPolicy(np),
839+
}
840+
op, err := config.clientContainer.Projects.Zones.Clusters.SetNetworkPolicy(
841+
project, zoneName, clusterName, req).Do()
842+
if err != nil {
843+
return err
844+
}
845+
846+
// Wait until it's updated
847+
waitErr := containerOperationWait(config, op, project, zoneName, "updating GKE cluster network policy", timeoutInMinutes, 2)
848+
if waitErr != nil {
849+
return waitErr
850+
}
851+
log.Printf("[INFO] Network policy for GKE cluster %s has been updated", d.Id())
852+
853+
d.SetPartial("network_policy")
854+
855+
}
856+
805857
if n, ok := d.GetOk("node_pool.#"); ok {
806858
for i := 0; i < n.(int); i++ {
807859
if err := nodePoolUpdate(d, meta, clusterName, fmt.Sprintf("node_pool.%d.", i), timeoutInMinutes); err != nil {
@@ -945,6 +997,31 @@ func expandMasterAuthorizedNetworksConfig(configured interface{}) *container.Mas
945997
return result
946998
}
947999

1000+
func expandNetworkPolicy(configured interface{}) *container.NetworkPolicy {
1001+
result := &container.NetworkPolicy{}
1002+
if configured != nil && len(configured.([]interface{})) > 0 {
1003+
config := configured.([]interface{})[0].(map[string]interface{})
1004+
if enabled, ok := config["enabled"]; ok && enabled.(bool) {
1005+
result.Enabled = true
1006+
if provider, ok := config["provider"]; ok {
1007+
result.Provider = provider.(string)
1008+
}
1009+
}
1010+
}
1011+
return result
1012+
}
1013+
1014+
func flattenNetworkPolicy(c *container.NetworkPolicy) []map[string]interface{} {
1015+
result := []map[string]interface{}{}
1016+
if c != nil {
1017+
result = append(result, map[string]interface{}{
1018+
"enabled": c.Enabled,
1019+
"provider": c.Provider,
1020+
})
1021+
}
1022+
return result
1023+
}
1024+
9481025
func flattenClusterAddonsConfig(c *container.AddonsConfig) []map[string]interface{} {
9491026
result := make(map[string]interface{})
9501027
if c.HorizontalPodAutoscaling != nil {

google/resource_container_cluster_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,38 @@ func TestAccContainerCluster_withMasterAuth(t *testing.T) {
107107
})
108108
}
109109

110+
func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) {
111+
t.Parallel()
112+
113+
clusterName := fmt.Sprintf("cluster-test-%s", acctest.RandString(10))
114+
115+
resource.Test(t, resource.TestCase{
116+
PreCheck: func() { testAccPreCheck(t) },
117+
Providers: testAccProviders,
118+
CheckDestroy: testAccCheckContainerClusterDestroy,
119+
Steps: []resource.TestStep{
120+
{
121+
Config: testAccContainerCluster_withNetworkPolicyEnabled(clusterName),
122+
Check: resource.ComposeTestCheckFunc(
123+
testAccCheckContainerCluster(
124+
"google_container_cluster.with_network_policy_enabled"),
125+
resource.TestCheckResourceAttr("google_container_cluster.with_network_policy_enabled",
126+
"network_policy.#", "1"),
127+
),
128+
},
129+
{
130+
Config: testAccContainerCluster_removeNetworkPolicy(clusterName),
131+
Check: resource.ComposeTestCheckFunc(
132+
testAccCheckContainerCluster(
133+
"google_container_cluster.with_network_policy_enabled"),
134+
resource.TestCheckNoResourceAttr("google_container_cluster.with_network_policy_enabled",
135+
"network_policy"),
136+
),
137+
},
138+
},
139+
})
140+
}
141+
110142
func TestAccContainerCluster_withMasterAuthorizedNetworksConfig(t *testing.T) {
111143
t.Parallel()
112144

@@ -678,6 +710,12 @@ func testAccCheckContainerCluster(n string) resource.TestCheckFunc {
678710
{"node_version", cluster.CurrentNodeVersion},
679711
}
680712

713+
if cluster.NetworkPolicy != nil {
714+
clusterTests = append(clusterTests,
715+
clusterTestField{"network_policy.0.enabled", cluster.NetworkPolicy.Enabled},
716+
clusterTestField{"network_policy.0.provider", cluster.NetworkPolicy.Provider},
717+
)
718+
}
681719
// Remove Zone from additional_zones since that's what the resource writes in state
682720
additionalZones := []string{}
683721
for _, location := range cluster.Locations {
@@ -947,6 +985,29 @@ resource "google_container_cluster" "with_master_auth" {
947985
}
948986
}`, acctest.RandString(10))
949987

988+
func testAccContainerCluster_withNetworkPolicyEnabled(clusterName string) string {
989+
return fmt.Sprintf(`
990+
resource "google_container_cluster" "with_network_policy_enabled" {
991+
name = "%s"
992+
zone = "us-central1-a"
993+
initial_node_count = 1
994+
995+
network_policy {
996+
enabled = true
997+
provider = "CALICO"
998+
}
999+
}`, clusterName)
1000+
}
1001+
1002+
func testAccContainerCluster_removeNetworkPolicy(clusterName string) string {
1003+
return fmt.Sprintf(`
1004+
resource "google_container_cluster" "with_network_policy_enabled" {
1005+
name = "%s"
1006+
zone = "us-central1-a"
1007+
initial_node_count = 1
1008+
}`, clusterName)
1009+
}
1010+
9501011
func testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName string, cidrs []string) string {
9511012

9521013
cidrBlocks := ""

website/docs/r/container_cluster.html.markdown

+10
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ output "cluster_ca_certificate" {
126126
* `network` - (Optional) The name or self_link of the Google Compute Engine
127127
network to which the cluster is connected.
128128

129+
* `network_policy` - (Optional) Configuration options for the
130+
[NetworkPolicy](https://kubernetes.io/docs/concepts/services-networking/networkpolicies/)
131+
feature. Structure is documented below.
132+
129133
* `node_config` - (Optional) Parameters used in creating the cluster's nodes.
130134
Structure is documented below.
131135

@@ -204,6 +208,12 @@ The `master_authorized_networks_config.cidr_blocks` block supports:
204208

205209
* `display_name` - (Optional) Field for users to identify CIDR blocks.
206210

211+
The `network_policy` block supports:
212+
213+
* `provider` - (Optional) The selected network policy provider. Defaults to PROVIDER_UNSPECIFIED.
214+
215+
* `enabled` - (Optional) Whether network policy is enabled on the cluster. Defaults to false.
216+
207217
The `node_config` block supports:
208218

209219
* `disk_size_gb` - (Optional) Size of the disk attached to each node, specified

0 commit comments

Comments
 (0)