From 73410df006ecb01dee72d6b46ff130c2c09dfa73 Mon Sep 17 00:00:00 2001 From: Andrew Starr-Bochicchio Date: Mon, 17 Jul 2023 10:29:21 -0400 Subject: [PATCH] k8s: Add destroy_all_associated_resources option. (#1007) --- .../kubernetes/resource_kubernetes_cluster.go | 40 ++++++++++++++++--- .../resource_kubernetes_cluster_test.go | 40 +++++++++++++++++++ docs/resources/kubernetes_cluster.md | 1 + 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/digitalocean/kubernetes/resource_kubernetes_cluster.go b/digitalocean/kubernetes/resource_kubernetes_cluster.go index 29afe5b9c..9a0e08243 100644 --- a/digitalocean/kubernetes/resource_kubernetes_cluster.go +++ b/digitalocean/kubernetes/resource_kubernetes_cluster.go @@ -169,6 +169,12 @@ func ResourceDigitalOceanKubernetesCluster() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "destroy_all_associated_resources": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, }, Timeouts: &schema.ResourceTimeout{ @@ -487,15 +493,37 @@ func resourceDigitalOceanKubernetesClusterUpdate(ctx context.Context, d *schema. func resourceDigitalOceanKubernetesClusterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { client := meta.(*config.CombinedConfig).GodoClient() + destroyAssociatedResources := d.Get("destroy_all_associated_resources").(bool) - resp, err := client.Kubernetes.Delete(context.Background(), d.Id()) - if err != nil { - if resp != nil && resp.StatusCode == 404 { - d.SetId("") - return nil + if destroyAssociatedResources { + log.Printf("[WARN] destroy_all_associated_resources set to true. All resources (load balancers, volumes, and volume snapshots) associated with the cluster will be destroyed.") + + list, _, err := client.Kubernetes.ListAssociatedResourcesForDeletion(ctx, d.Id()) + if err != nil { + return diag.Errorf("Failed to list associated resources: %s", err) + } + + log.Printf("[WARN] The following resources will be destroyed: %s", godo.Stringify(list)) + + resp, err := client.Kubernetes.DeleteDangerous(ctx, d.Id()) + if err != nil { + if resp != nil && resp.StatusCode == 404 { + d.SetId("") + return nil + } + + return diag.Errorf("Unable to delete cluster: %s", err) } + } else { + resp, err := client.Kubernetes.Delete(ctx, d.Id()) + if err != nil { + if resp != nil && resp.StatusCode == 404 { + d.SetId("") + return nil + } - return diag.Errorf("Unable to delete cluster: %s", err) + return diag.Errorf("Unable to delete cluster: %s", err) + } } d.SetId("") diff --git a/digitalocean/kubernetes/resource_kubernetes_cluster_test.go b/digitalocean/kubernetes/resource_kubernetes_cluster_test.go index 7cabfa10e..4521c9fd4 100644 --- a/digitalocean/kubernetes/resource_kubernetes_cluster_test.go +++ b/digitalocean/kubernetes/resource_kubernetes_cluster_test.go @@ -93,6 +93,7 @@ func TestAccDigitalOceanKubernetesCluster_Basic(t *testing.T) { resource.TestCheckResourceAttrSet("digitalocean_kubernetes_cluster.foobar", "maintenance_policy.0.day"), resource.TestCheckResourceAttrSet("digitalocean_kubernetes_cluster.foobar", "maintenance_policy.0.start_time"), resource.TestCheckResourceAttr("digitalocean_kubernetes_cluster.foobar", "registry_integration", "false"), + resource.TestCheckResourceAttr("digitalocean_kubernetes_cluster.foobar", "destroy_all_associated_resources", "false"), ), }, // Update: remove default node_pool taints @@ -735,6 +736,27 @@ func TestAccDigitalOceanKubernetesCluster_UpgradeVersion(t *testing.T) { }) } +func TestAccDigitalOceanKubernetesCluster_DestroyAssociated(t *testing.T) { + rName := acceptance.RandomTestName() + var k8s godo.KubernetesCluster + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: acceptance.TestAccProviderFactories, + CheckDestroy: testAccCheckDigitalOceanKubernetesClusterDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDigitalOceanKubernetesConfigDestroyAssociated(testClusterVersionPrevious, rName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckDigitalOceanKubernetesClusterExists("digitalocean_kubernetes_cluster.foobar", &k8s), + resource.TestCheckResourceAttrPair("digitalocean_kubernetes_cluster.foobar", "version", "data.digitalocean_kubernetes_versions.test", "latest_version"), + resource.TestCheckResourceAttr("digitalocean_kubernetes_cluster.foobar", "destroy_all_associated_resources", "true"), + ), + }, + }, + }) +} + func testAccDigitalOceanKubernetesConfigBasic(testClusterVersion string, rName string) string { return fmt.Sprintf(`%s @@ -906,6 +928,24 @@ resource "kubernetes_namespace" "example" { `, testClusterVersion, rName) } +func testAccDigitalOceanKubernetesConfigDestroyAssociated(testClusterVersion string, rName string) string { + return fmt.Sprintf(`%s + +resource "digitalocean_kubernetes_cluster" "foobar" { + name = "%s" + region = "nyc1" + version = data.digitalocean_kubernetes_versions.test.latest_version + destroy_all_associated_resources = true + + node_pool { + name = "default" + size = "s-1vcpu-2gb" + node_count = 1 + } +} +`, testClusterVersion, rName) +} + func testAccCheckDigitalOceanKubernetesClusterDestroy(s *terraform.State) error { client := acceptance.TestAccProvider.Meta().(*config.CombinedConfig).GodoClient() diff --git a/docs/resources/kubernetes_cluster.md b/docs/resources/kubernetes_cluster.md index 693df334e..2ac38dfb8 100644 --- a/docs/resources/kubernetes_cluster.md +++ b/docs/resources/kubernetes_cluster.md @@ -163,6 +163,7 @@ The following arguments are supported: * `maintenance_policy` - (Optional) A block representing the cluster's maintenance window. Updates will be applied within this window. If not specified, a default maintenance window will be chosen. `auto_upgrade` must be set to `true` for this to have an effect. - `day` - (Required) The day of the maintenance window policy. May be one of "monday" through "sunday", or "any" to indicate an arbitrary week day. - `start_time` (Required) The start time in UTC of the maintenance window policy in 24-hour clock format / HH:MM notation (e.g., 15:00). +* `destroy_all_associated_resources` - (Optional) **Use with caution.** When set to true, all associated DigitalOcean resources created via the Kubernetes API (load balancers, volumes, and volume snapshots) will be destroyed along with the cluster when it is destroyed. This resource supports [customized create timeouts](https://www.terraform.io/docs/language/resources/syntax.html#operation-timeouts). The default timeout is 30 minutes.