From c9182af92723d4b1c801a8018c244100ebdc122c Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 23 Jan 2023 23:50:36 +0300 Subject: [PATCH] Fix 1322: allow_volume_expansion fix (#1519) * fix src * add tests Co-authored-by: Mauricio Alvarez Leon <65101411+BBBmau@users.noreply.github.com> --- .../resource_kubernetes_storage_class.go | 9 +++ .../resource_kubernetes_storage_class_test.go | 66 +++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/kubernetes/resource_kubernetes_storage_class.go b/kubernetes/resource_kubernetes_storage_class.go index 3cd85eccf9..7b427981b8 100644 --- a/kubernetes/resource_kubernetes_storage_class.go +++ b/kubernetes/resource_kubernetes_storage_class.go @@ -201,10 +201,19 @@ func resourceKubernetesStorageClassUpdate(ctx context.Context, d *schema.Resourc name := d.Id() ops := patchMetadata("metadata.0.", "/metadata/", d) + + if d.HasChange("allow_volume_expansion") { + newVal := d.Get("allow_volume_expansion").(bool) + ops = append(ops, &ReplaceOperation{ + Path: "/allowVolumeExpansion", + Value: newVal, + }) + } data, err := ops.MarshalJSON() if err != nil { return diag.Errorf("Failed to marshal update operations: %s", err) } + log.Printf("[INFO] Updating storage class %q: %v", name, string(data)) out, err := conn.StorageV1().StorageClasses().Patch(ctx, name, pkgApi.JSONPatchType, data, metav1.PatchOptions{}) if err != nil { diff --git a/kubernetes/resource_kubernetes_storage_class_test.go b/kubernetes/resource_kubernetes_storage_class_test.go index 5499d4bee1..7e91b02d5a 100644 --- a/kubernetes/resource_kubernetes_storage_class_test.go +++ b/kubernetes/resource_kubernetes_storage_class_test.go @@ -82,6 +82,48 @@ func TestAccKubernetesStorageClass_minikube(t *testing.T) { }) } +func TestAccKubernetesStorageClass_volumeExpansion(t *testing.T) { + var conf api.StorageClass + name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + resourceName := "kubernetes_storage_class.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); skipIfNotRunningInMinikube(t) }, + IDRefreshName: resourceName, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccCheckKubernetesStorageClassDestroy, + Steps: []resource.TestStep{ + { + Config: testAccKubernetesStorageClassConfig_volumeExpansion(name, "k8s.io/minikube-hostpath"), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckKubernetesStorageClassExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name), + resource.TestCheckResourceAttrSet(resourceName, "metadata.0.generation"), + resource.TestCheckResourceAttrSet(resourceName, "metadata.0.resource_version"), + resource.TestCheckResourceAttrSet(resourceName, "metadata.0.uid"), + resource.TestCheckResourceAttr(resourceName, "storage_provisioner", "k8s.io/minikube-hostpath"), + resource.TestCheckResourceAttr(resourceName, "reclaim_policy", "Delete"), + resource.TestCheckResourceAttr(resourceName, "volume_binding_mode", "Immediate"), + resource.TestCheckResourceAttr(resourceName, "allow_volume_expansion", "true"), + ), + }, + { + Config: testAccKubernetesStorageClassConfig_volumeExpansionModified(name, "k8s.io/minikube-hostpath"), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckKubernetesStorageClassExists(resourceName, &conf), + resource.TestCheckResourceAttrSet(resourceName, "metadata.0.generation"), + resource.TestCheckResourceAttrSet(resourceName, "metadata.0.resource_version"), + resource.TestCheckResourceAttrSet(resourceName, "metadata.0.uid"), + resource.TestCheckResourceAttr(resourceName, "storage_provisioner", "k8s.io/minikube-hostpath"), + resource.TestCheckResourceAttr(resourceName, "reclaim_policy", "Delete"), + resource.TestCheckResourceAttr(resourceName, "volume_binding_mode", "Immediate"), + resource.TestCheckResourceAttr(resourceName, "allow_volume_expansion", "false"), + ), + }, + }, + }) +} + func TestAccKubernetesStorageClass_basic(t *testing.T) { var conf api.StorageClass name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) @@ -364,6 +406,30 @@ func testAccKubernetesStorageClassConfig_modified(name, provisioner string) stri `, name, provisioner) } +func testAccKubernetesStorageClassConfig_volumeExpansion(name, provisioner string) string { + return fmt.Sprintf(`resource "kubernetes_storage_class" "test" { + metadata { + name = "%s" + } + + storage_provisioner = "%s" + allow_volume_expansion = true +} +`, name, provisioner) +} + +func testAccKubernetesStorageClassConfig_volumeExpansionModified(name, provisioner string) string { + return fmt.Sprintf(`resource "kubernetes_storage_class" "test" { + metadata { + name = "%s" + } + + storage_provisioner = "%s" + allow_volume_expansion = false +} +`, name, provisioner) +} + func testAccKubernetesStorageClassConfig_noParameters(name, provisioner string) string { return fmt.Sprintf(`resource "kubernetes_storage_class" "test" { metadata {