From bd4b65f41b4f407401d3efecc7bb367cb9df805b Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Thu, 22 Mar 2018 17:06:13 -0700 Subject: [PATCH] add ability to delete the default node pool --- google/resource_container_cluster.go | 28 +++++++ google/resource_container_cluster_test.go | 76 +++++++++++++++---- .../docs/r/container_cluster.html.markdown | 2 + 3 files changed, 90 insertions(+), 16 deletions(-) diff --git a/google/resource_container_cluster.go b/google/resource_container_cluster.go index eb3e80cb8e0..6c64c676a62 100644 --- a/google/resource_container_cluster.go +++ b/google/resource_container_cluster.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/hashicorp/errwrap" "github.com/hashicorp/go-version" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" @@ -421,6 +422,11 @@ func resourceContainerCluster() *schema.Resource { }, }, }, + + "remove_default_node_pool": { + Type: schema.TypeBool, + Optional: true, + }, }, } } @@ -616,6 +622,17 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er log.Printf("[INFO] GKE cluster %s has been created", clusterName) + if d.Get("remove_default_node_pool").(bool) { + op, err := config.clientContainer.Projects.Zones.Clusters.NodePools.Delete(project, zoneName, clusterName, "default-pool").Do() + if err != nil { + return errwrap.Wrapf("Error deleting default node pool: {{err}}", err) + } + err = containerSharedOperationWait(config, op, project, zoneName, "removing default node pool", timeoutInMinutes, 3) + if err != nil { + return errwrap.Wrapf("Error deleting default node pool: {{err}}", err) + } + } + return resourceContainerClusterRead(d, meta) } @@ -1108,6 +1125,17 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er d.SetPartial("logging_service") } + if d.HasChange("remove_default_node_pool") && d.Get("remove_default_node_pool").(bool) { + op, err := config.clientContainer.Projects.Zones.Clusters.NodePools.Delete(project, zoneName, clusterName, "default-pool").Do() + if err != nil { + return errwrap.Wrapf("Error deleting default node pool: {{err}}", err) + } + err = containerSharedOperationWait(config, op, project, zoneName, "removing default node pool", timeoutInMinutes, 3) + if err != nil { + return errwrap.Wrapf("Error deleting default node pool: {{err}}", err) + } + } + d.Partial(false) return resourceContainerClusterRead(d, meta) diff --git a/google/resource_container_cluster_test.go b/google/resource_container_cluster_test.go index 5dc3c24a0f4..87a6cd09342 100644 --- a/google/resource_container_cluster_test.go +++ b/google/resource_container_cluster_test.go @@ -137,10 +137,11 @@ func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) { ), }, { - ResourceName: "google_container_cluster.with_network_policy_enabled", - ImportStateIdPrefix: "us-central1-a/", - ImportState: true, - ImportStateVerify: true, + ResourceName: "google_container_cluster.with_network_policy_enabled", + ImportStateIdPrefix: "us-central1-a/", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"remove_default_node_pool"}, }, { Config: testAccContainerCluster_removeNetworkPolicy(clusterName), @@ -150,10 +151,11 @@ func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) { ), }, { - ResourceName: "google_container_cluster.with_network_policy_enabled", - ImportStateIdPrefix: "us-central1-a/", - ImportState: true, - ImportStateVerify: true, + ResourceName: "google_container_cluster.with_network_policy_enabled", + ImportStateIdPrefix: "us-central1-a/", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"remove_default_node_pool"}, }, { Config: testAccContainerCluster_withNetworkPolicyDisabled(clusterName), @@ -163,10 +165,11 @@ func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) { ), }, { - ResourceName: "google_container_cluster.with_network_policy_enabled", - ImportStateIdPrefix: "us-central1-a/", - ImportState: true, - ImportStateVerify: true, + ResourceName: "google_container_cluster.with_network_policy_enabled", + ImportStateIdPrefix: "us-central1-a/", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"remove_default_node_pool"}, }, { Config: testAccContainerCluster_withNetworkPolicyConfigDisabled(clusterName), @@ -176,10 +179,11 @@ func TestAccContainerCluster_withNetworkPolicyEnabled(t *testing.T) { ), }, { - ResourceName: "google_container_cluster.with_network_policy_enabled", - ImportStateIdPrefix: "us-central1-a/", - ImportState: true, - ImportStateVerify: true, + ResourceName: "google_container_cluster.with_network_policy_enabled", + ImportStateIdPrefix: "us-central1-a/", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"remove_default_node_pool"}, }, { Config: testAccContainerCluster_withNetworkPolicyConfigDisabled(clusterName), @@ -787,6 +791,30 @@ func TestAccContainerCluster_withNodePoolNodeConfig(t *testing.T) { }) } +func TestAccContainerCluster_withDefaultNodePoolRemoved(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckContainerClusterDestroy, + Steps: []resource.TestStep{ + { + Config: testAccContainerCluster_withDefaultNodePoolRemoved(), + Check: resource.TestCheckResourceAttr( + "google_container_cluster.with_default_node_pool_removed", "node_pool.#", "0"), + }, + { + ResourceName: "google_container_cluster.with_default_node_pool_removed", + ImportStateIdPrefix: "us-central1-a/", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"remove_default_node_pool"}, + }, + }, + }) +} + func TestAccContainerCluster_withMaintenanceWindow(t *testing.T) { t.Parallel() clusterName := acctest.RandString(10) @@ -1100,6 +1128,7 @@ resource "google_container_cluster" "with_network_policy_enabled" { name = "%s" zone = "us-central1-a" initial_node_count = 1 + remove_default_node_pool = true network_policy { enabled = true @@ -1119,6 +1148,7 @@ resource "google_container_cluster" "with_network_policy_enabled" { name = "%s" zone = "us-central1-a" initial_node_count = 1 + remove_default_node_pool = true }`, clusterName) } @@ -1128,6 +1158,7 @@ resource "google_container_cluster" "with_network_policy_enabled" { name = "%s" zone = "us-central1-a" initial_node_count = 1 + remove_default_node_pool = true network_policy = {} }`, clusterName) @@ -1139,6 +1170,7 @@ resource "google_container_cluster" "with_network_policy_enabled" { name = "%s" zone = "us-central1-a" initial_node_count = 1 + remove_default_node_pool = true network_policy = {} addons_config { @@ -1632,6 +1664,18 @@ resource "google_container_cluster" "with_node_pool_node_config" { `, testId, testId) } +func testAccContainerCluster_withDefaultNodePoolRemoved() string { + return fmt.Sprintf(` +resource "google_container_cluster" "with_default_node_pool_removed" { + name = "cluster-test-%s" + zone = "us-central1-a" + initial_node_count = 1 + + remove_default_node_pool = true +} +`, acctest.RandString(10)) +} + func testAccContainerCluster_withMaintenanceWindow(clusterName string, startTime string) string { maintenancePolicy := "" if len(startTime) > 0 { diff --git a/website/docs/r/container_cluster.html.markdown b/website/docs/r/container_cluster.html.markdown index 3ec0da30494..42fc3c78af0 100644 --- a/website/docs/r/container_cluster.html.markdown +++ b/website/docs/r/container_cluster.html.markdown @@ -155,6 +155,8 @@ output "cluster_ca_certificate" { * `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used. +* `remove_default_node_pool` - (Optional) If true, deletes the default node pool upon cluster creation. + * `subnetwork` - (Optional) The name of the Google Compute Engine subnetwork in which the cluster's instances are launched.