Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add container cluster network policy addon #630

Merged
merged 15 commits into from
Nov 27, 2017
79 changes: 79 additions & 0 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,30 @@ func resourceContainerCluster() *schema.Resource {
StateFunc: StoreResourceName,
},

"network_policy": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
ForceNew: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused why you've decided to allow adding/removing the network policy but not changing the values within it. Was that intentional? If so, can you add a comment explaining why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry @danawillow i don't understand exactly what do you say about 'changing the value".
if you do not put network_policy block, it's the same than disable it.
if you put network_policy block, you can choice to enable or diable it.

Optional: true,
Default: false,
},
"provider": {
Type: schema.TypeString,
ForceNew: true,
Default: "PROVIDER_UNSPECIFIED",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"PROVIDER_UNSPECIFIED", "CALICO"}, false),
},
},
},
},

"node_config": schemaNodeConfig,

"node_pool": {
Expand Down Expand Up @@ -398,6 +422,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
cluster.Network = network
}

if v, ok := d.GetOk("network_policy"); ok && len(v.([]interface{})) > 0 {
cluster.NetworkPolicy = expandNetworkPolicy(v)
}

if v, ok := d.GetOk("subnetwork"); ok {
cluster.Subnetwork = v.(string)
}
Expand Down Expand Up @@ -480,6 +508,9 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
}

d.Set("name", cluster.Name)

d.Set("network_policy", flattenNetworkPolicy(cluster.NetworkPolicy))

d.Set("zone", cluster.Zone)

locations := []string{}
Expand Down Expand Up @@ -743,6 +774,29 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
d.SetPartial("monitoring_service")
}

if d.HasChange("network_policy") {
np, _ := d.GetOk("network_policy")

req := &container.SetNetworkPolicyRequest{
NetworkPolicy: expandNetworkPolicy(np),
}
op, err := config.clientContainer.Projects.Zones.Clusters.SetNetworkPolicy(
project, zoneName, clusterName, req).Do()
if err != nil {
return err
}

// Wait until it's updated
waitErr := containerOperationWait(config, op, project, zoneName, "updating GKE cluster network policy", timeoutInMinutes, 2)
if waitErr != nil {
return waitErr
}
log.Printf("[INFO] Network policy for GKE cluster %s has been updated", d.Id())

d.SetPartial("network_policy")

}

if n, ok := d.GetOk("node_pool.#"); ok {
for i := 0; i < n.(int); i++ {
if err := nodePoolUpdate(d, meta, clusterName, fmt.Sprintf("node_pool.%d.", i), timeoutInMinutes); err != nil {
Expand Down Expand Up @@ -886,6 +940,31 @@ func expandMasterAuthorizedNetworksConfig(configured interface{}) *container.Mas
return result
}

func expandNetworkPolicy(configured interface{}) *container.NetworkPolicy {
result := &container.NetworkPolicy{}
if configured != nil && len(configured.([]interface{})) > 0 {
config := configured.([]interface{})[0].(map[string]interface{})
if enabled, ok := config["enabled"]; ok && enabled.(bool) {
result.Enabled = true
if provider, ok := config["provider"]; ok {
result.Provider = provider.(string)
}
}
}
return result
}

func flattenNetworkPolicy(c *container.NetworkPolicy) []map[string]interface{} {
result := []map[string]interface{}{}
if c != nil {
result = append(result, map[string]interface{}{
"enabled": c.Enabled,
"provider": c.Provider,
})
}
return result
}

func flattenClusterAddonsConfig(c *container.AddonsConfig) []map[string]interface{} {
result := make(map[string]interface{})
if c.HorizontalPodAutoscaling != nil {
Expand Down
68 changes: 67 additions & 1 deletion google/resource_container_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,38 @@ func TestAccContainerCluster_withMasterAuth(t *testing.T) {
})
}

func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("cluster-test-%s", acctest.RandString(10))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withNetworkPolicyEnabled(clusterName),
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerCluster(
"google_container_cluster.with_network_policy_enabled"),
resource.TestCheckResourceAttr("google_container_cluster.with_network_policy_enabled",
"network_policy.#", "1"),
),
},
{
Config: testAccContainerCluster_removeNetworkPolicy(clusterName),
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerCluster(
"google_container_cluster.with_network_policy_enabled"),
resource.TestCheckNoResourceAttr("google_container_cluster.with_network_policy_enabled",
"network_policy"),
),
},
},
})
}

func TestAccContainerCluster_withMasterAuthorizedNetworksConfig(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -659,6 +691,12 @@ func testAccCheckContainerCluster(n string) resource.TestCheckFunc {
{"node_version", cluster.CurrentNodeVersion},
}

if cluster.NetworkPolicy != nil {
clusterTests = append(clusterTests,
clusterTestField{"network_policy.0.enabled", cluster.NetworkPolicy.Enabled},
clusterTestField{"network_policy.0.provider", cluster.NetworkPolicy.Provider},
)
}
// Remove Zone from additional_zones since that's what the resource writes in state
additionalZones := []string{}
for _, location := range cluster.Locations {
Expand Down Expand Up @@ -911,7 +949,7 @@ resource "google_container_cluster" "primary" {

var testAccContainerCluster_withMasterAuth = fmt.Sprintf(`
resource "google_container_cluster" "with_master_auth" {
name = "cluster-test-%s"
name = "%s"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you changed the wrong test here

zone = "us-central1-a"
initial_node_count = 3

Expand All @@ -921,6 +959,34 @@ resource "google_container_cluster" "with_master_auth" {
}
}`, acctest.RandString(10))

func testAccContainerCluster_withNetworkPolicyEnabled(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_network_policy_enabled" {
name = "%s"
zone = "us-central1-a"
initial_node_count = 1

network_policy {
enabled = true
provider = "CALICO"
}
}`, clusterName)
}

func testAccContainerCluster_removeNetworkPolicy(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_network_policy_enabled" {
name = "cluster-test-%s"
zone = "us-central1-a"
initial_node_count = 1

// remove network_policy is equal than enabled=false
//network_policy {
// enabled = "false"
//}
}`, clusterName)
}

func testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName string, cidrs []string) string {

cidrBlocks := ""
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/container_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ output "cluster_ca_certificate" {
* `network` - (Optional) The name or self_link of the Google Compute Engine
network to which the cluster is connected.

* `network_policy` - (Optional) The status of the Network Policy.
It is disable by default; set `enabled = true` to enable.

* `node_config` - (Optional) Parameters used in creating the cluster's nodes.
Structure is documented below.

Expand Down