Skip to content

Commit

Permalink
Allow updating deployment strategy from rolling to recreate (#1255)
Browse files Browse the repository at this point in the history
Allow in-place update to change the Deployment strategy from `rolling` to `recreate`.
  • Loading branch information
DrFaust92 authored May 14, 2021
1 parent a3546ce commit 6eb06c1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
11 changes: 11 additions & 0 deletions kubernetes/resource_kubernetes_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 27 additions & 6 deletions kubernetes/resource_kubernetes_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,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) },
Expand All @@ -763,12 +764,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%"),
),
},
},
Expand Down

0 comments on commit 6eb06c1

Please sign in to comment.