Skip to content

Commit

Permalink
Merge pull request #139 from terraform-providers/b-pvs-forcenew-19
Browse files Browse the repository at this point in the history
resource/kubernetes_persistent_volume: Mark persistent_volume_source as ForceNew on 1.9+
  • Loading branch information
radeksimko authored Mar 23, 2018
2 parents 7e31bc2 + 68c37cd commit 0eef610
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
39 changes: 39 additions & 0 deletions kubernetes/resource_kubernetes_persistent_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"time"

gversion "github.com/hashicorp/go-version"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -25,6 +26,44 @@ func resourceKubernetesPersistentVolume() *schema.Resource {
State: schema.ImportStatePassthrough,
},

CustomizeDiff: func(diff *schema.ResourceDiff, meta interface{}) error {
if diff.Id() == "" {
// We only care about updates, not creation
return nil
}

// Mutation of PersistentVolumeSource after creation is no longer allowed in 1.9+
// See https://github.com/kubernetes/kubernetes/blob/v1.9.3/CHANGELOG-1.9.md#storage-3
conn := meta.(*kubernetes.Clientset)
serverVersion, err := conn.ServerVersion()
if err != nil {
return err
}

k8sVersion, err := gversion.NewVersion(serverVersion.String())
if err != nil {
return err
}

v1_9_0, _ := gversion.NewVersion("1.9.0")
if k8sVersion.Equal(v1_9_0) || k8sVersion.GreaterThan(v1_9_0) {
if diff.HasChange("spec.0.persistent_volume_source") {
keys := diff.GetChangedKeysPrefix("spec.0.persistent_volume_source")
for _, key := range keys {
if diff.HasChange(key) {
err := diff.ForceNew(key)
if err != nil {
return err
}
}
}
return nil
}
}

return nil
},

Schema: map[string]*schema.Schema{
"metadata": metadataSchema("persistent volume", false),
"spec": {
Expand Down
11 changes: 2 additions & 9 deletions kubernetes/resource_kubernetes_persistent_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ func TestAccKubernetesPersistentVolume_googleCloud_volumeSource(t *testing.T) {
name := fmt.Sprintf("tf-acc-test-%s", randString)
diskName := fmt.Sprintf("tf-acc-test-disk-%s", randString)

region := os.Getenv("GOOGLE_REGION")
zone := os.Getenv("GOOGLE_ZONE")

resource.Test(t, resource.TestCase{
Expand All @@ -232,10 +231,7 @@ func TestAccKubernetesPersistentVolume_googleCloud_volumeSource(t *testing.T) {
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.annotations.%", "0"),
testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{}),
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.%", "0"),
testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{
"failure-domain.beta.kubernetes.io/region": region,
"failure-domain.beta.kubernetes.io/zone": zone,
}),
testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}),
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.name", name),
resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.generation"),
resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.resource_version"),
Expand All @@ -256,10 +252,7 @@ func TestAccKubernetesPersistentVolume_googleCloud_volumeSource(t *testing.T) {
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.annotations.%", "0"),
testAccCheckMetaAnnotations(&conf.ObjectMeta, map[string]string{}),
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.labels.%", "0"),
testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{
"failure-domain.beta.kubernetes.io/region": region,
"failure-domain.beta.kubernetes.io/zone": zone,
}),
testAccCheckMetaLabels(&conf.ObjectMeta, map[string]string{}),
resource.TestCheckResourceAttr("kubernetes_persistent_volume.test", "metadata.0.name", name),
resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.generation"),
resource.TestCheckResourceAttrSet("kubernetes_persistent_volume.test", "metadata.0.resource_version"),
Expand Down

0 comments on commit 0eef610

Please sign in to comment.