From 61ffbf0ac227a6a0852db05ced93e3742bfacdb8 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Fri, 30 Apr 2021 10:50:04 +0300 Subject: [PATCH] allow update from rolling upgrade to recreate --- kubernetes/resource_kubernetes_deployment.go | 11 +++++++ .../resource_kubernetes_deployment_test.go | 33 +++++++++++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/kubernetes/resource_kubernetes_deployment.go b/kubernetes/resource_kubernetes_deployment.go index 99c3506879..8c39ef712d 100644 --- a/kubernetes/resource_kubernetes_deployment.go +++ b/kubernetes/resource_kubernetes_deployment.go @@ -278,6 +278,17 @@ func resourceKubernetesDeploymentUpdate(ctx context.Context, d *schema.ResourceD Value: spec, }) } + + if d.HasChange("spec.0.strategy") { + o, n := d.GetChange("spec.0.strategy.0.type") + + if o.(string) == "RollingUpdate" && n.(string) == "Recreate" { + ops = append(ops, &RemoveOperation{ + Path: "/spec/strategy/rollingUpdate", + }) + } + } + data, err := ops.MarshalJSON() if err != nil { return diag.Errorf("Failed to marshal update operations: %s", err) diff --git a/kubernetes/resource_kubernetes_deployment_test.go b/kubernetes/resource_kubernetes_deployment_test.go index da8109f52c..677a76ebfd 100644 --- a/kubernetes/resource_kubernetes_deployment_test.go +++ b/kubernetes/resource_kubernetes_deployment_test.go @@ -746,6 +746,7 @@ func TestAccKubernetesDeployment_with_deployment_strategy_rollingupdate(t *testi deploymentName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) imageName := nginxImageVersion + resourceName := "kubernetes_deployment.test" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -755,12 +756,32 @@ func TestAccKubernetesDeployment_with_deployment_strategy_rollingupdate(t *testi { Config: testAccKubernetesDeploymentConfigWithDeploymentStrategy(deploymentName, "RollingUpdate", imageName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckKubernetesDeploymentExists("kubernetes_deployment.test", &conf), - resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.#", "1"), - resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.type", "RollingUpdate"), - resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.#", "1"), - resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.0.max_surge", "25%"), - resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.rolling_update.0.max_unavailable", "25%"), + testAccCheckKubernetesDeploymentExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.#", "1"), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.type", "RollingUpdate"), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.#", "1"), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_surge", "25%"), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_unavailable", "25%"), + ), + }, + { + Config: testAccKubernetesDeploymentConfigWithDeploymentStrategy(deploymentName, "Recreate", imageName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckKubernetesDeploymentExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.#", "1"), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.type", "Recreate"), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.#", "0"), + ), + }, + { + Config: testAccKubernetesDeploymentConfigWithDeploymentStrategy(deploymentName, "RollingUpdate", imageName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckKubernetesDeploymentExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.#", "1"), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.type", "RollingUpdate"), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.#", "1"), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_surge", "25%"), + resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_unavailable", "25%"), ), }, },