Skip to content

Commit

Permalink
Update wait_for logic to wait until pods are Ready
Browse files Browse the repository at this point in the history
  • Loading branch information
dak1n1 committed Nov 18, 2020
1 parent 6eba3ab commit a246cf8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
6 changes: 5 additions & 1 deletion kubernetes/resource_kubernetes_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func waitForDeploymentReplicasFunc(ctx context.Context, conn *kubernetes.Clients
return resource.NonRetryableError(err)
}

var specReplicas int32 = 1 // default, acording to API docs
var specReplicas int32 = 1 // default, according to API docs
if dply.Spec.Replicas != nil {
specReplicas = *dply.Spec.Replicas
}
Expand All @@ -436,6 +436,10 @@ func waitForDeploymentReplicasFunc(ctx context.Context, conn *kubernetes.Clients
return resource.RetryableError(fmt.Errorf("Waiting for rollout to finish: %d old replicas are pending termination...", dply.Status.Replicas-dply.Status.UpdatedReplicas))
}

if dply.Status.Replicas > dply.Status.ReadyReplicas {
return resource.RetryableError(fmt.Errorf("Waiting for rollout to finish: %d replicas wanted; %d replicas Ready", dply.Status.Replicas, dply.Status.ReadyReplicas))
}

if dply.Status.AvailableReplicas < dply.Status.UpdatedReplicas {
return resource.RetryableError(fmt.Errorf("Waiting for rollout to finish: %d of %d updated replicas are available...", dply.Status.AvailableReplicas, dply.Status.UpdatedReplicas))
}
Expand Down
25 changes: 4 additions & 21 deletions kubernetes/resource_kubernetes_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestAccKubernetesDeployment_basic(t *testing.T) {
Config: testAccKubernetesDeploymentConfig_basic(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentExists("kubernetes_deployment.test", &conf),
testAccCheckKubernetesDeploymentRolledOut("kubernetes_deployment.test"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "metadata.0.annotations.%", "2"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "metadata.0.annotations.TestAnnotationOne", "one"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "metadata.0.annotations.TestAnnotationTwo", "two"),
Expand Down Expand Up @@ -915,14 +914,14 @@ func TestAccKubernetesDeployment_regression(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "kubernetes_deployment.test",
IDRefreshIgnore: []string{"metadata.0.resource_version"},
ExternalProviders: testAccExternalProviders,
CheckDestroy: testAccCheckKubernetesDeploymentDestroy,
Steps: []resource.TestStep{
{
Config: requiredProviders() + testAccKubernetesDeploymentConfig_regression("kubernetes-released", name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentExists("kubernetes_deployment.test", &conf1),
testAccCheckKubernetesDeploymentRolledOut("kubernetes_deployment.test"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "metadata.0.annotations.%", "2"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "metadata.0.annotations.TestAnnotationOne", "one"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "metadata.0.annotations.TestAnnotationTwo", "two"),
Expand All @@ -938,7 +937,7 @@ func TestAccKubernetesDeployment_regression(t *testing.T) {
resource.TestCheckResourceAttrSet("kubernetes_deployment.test", "metadata.0.self_link"),
resource.TestCheckResourceAttrSet("kubernetes_deployment.test", "metadata.0.uid"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.template.0.spec.0.container.0.image", defaultNginxImage),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.template.0.spec.0.container.0.name", "tf-acc-test"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.template.0.spec.0.container.0.name", "containername"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.type", "RollingUpdate"),
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%"),
Expand All @@ -949,7 +948,6 @@ func TestAccKubernetesDeployment_regression(t *testing.T) {
Config: requiredProviders() + testAccKubernetesDeploymentConfig_regression("kubernetes-local", name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentExists("kubernetes_deployment.test", &conf2),
testAccCheckKubernetesDeploymentRolledOut("kubernetes_deployment.test"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "metadata.0.annotations.%", "2"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "metadata.0.annotations.TestAnnotationOne", "one"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "metadata.0.annotations.TestAnnotationTwo", "two"),
Expand All @@ -965,7 +963,7 @@ func TestAccKubernetesDeployment_regression(t *testing.T) {
resource.TestCheckResourceAttrSet("kubernetes_deployment.test", "metadata.0.self_link"),
resource.TestCheckResourceAttrSet("kubernetes_deployment.test", "metadata.0.uid"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.template.0.spec.0.container.0.image", defaultNginxImage),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.template.0.spec.0.container.0.name", "tf-acc-test"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.template.0.spec.0.container.0.name", "containername"),
resource.TestCheckResourceAttr("kubernetes_deployment.test", "spec.0.strategy.0.type", "RollingUpdate"),
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%"),
Expand Down Expand Up @@ -1146,21 +1144,6 @@ func testAccCheckKubernetesDeploymentRollingOut(n string) resource.TestCheckFunc
}
}

func testAccCheckKubernetesDeploymentRolledOut(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
d, err := getDeploymentFromResourceName(s, n)
if err != nil {
return err
}

if d.Status.Replicas != d.Status.ReadyReplicas {
return fmt.Errorf("deployment is still rolling out")
}

return nil
}
}

func testAccKubernetesDeploymentConfig_basic(name string) string {
return fmt.Sprintf(`resource "kubernetes_deployment" "test" {
metadata {
Expand Down Expand Up @@ -2439,7 +2422,7 @@ func testAccKubernetesDeploymentConfig_regression(provider, name string) string
spec {
container {
image = %q
name = "tf-acc-test"
name = "containername"
port {
container_port = 80
Expand Down

0 comments on commit a246cf8

Please sign in to comment.