Skip to content

Commit 33b550c

Browse files
authored
Add wait_for_rollout attribute to kubernetes_deployment (#916)
1 parent 9329192 commit 33b550c

File tree

3 files changed

+296
-158
lines changed

3 files changed

+296
-158
lines changed

kubernetes/resource_kubernetes_deployment.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1010
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1111
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
12+
1213
appsv1 "k8s.io/api/apps/v1"
1314
"k8s.io/apimachinery/pkg/api/errors"
1415
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15-
pkgApi "k8s.io/apimachinery/pkg/types"
16+
"k8s.io/apimachinery/pkg/types"
1617
"k8s.io/client-go/kubernetes"
1718
)
1819

@@ -190,6 +191,12 @@ func resourceKubernetesDeployment() *schema.Resource {
190191
},
191192
},
192193
},
194+
"wait_for_rollout": {
195+
Type: schema.TypeBool,
196+
Description: "Wait for the rollout of the deployment to complete. Defaults to true.",
197+
Default: true,
198+
Optional: true,
199+
},
193200
},
194201
}
195202
}
@@ -221,11 +228,13 @@ func resourceKubernetesDeploymentCreate(d *schema.ResourceData, meta interface{}
221228

222229
log.Printf("[DEBUG] Waiting for deployment %s to schedule %d replicas", d.Id(), *out.Spec.Replicas)
223230

224-
// 10 mins should be sufficient for scheduling ~10k replicas
225-
err = resource.Retry(d.Timeout(schema.TimeoutCreate),
226-
waitForDeploymentReplicasFunc(conn, out.GetNamespace(), out.GetName()))
227-
if err != nil {
228-
return err
231+
if d.Get("wait_for_rollout").(bool) {
232+
log.Printf("[INFO] Waiting for deployment %s/%s to rollout", out.ObjectMeta.Namespace, out.ObjectMeta.Name)
233+
err := resource.Retry(d.Timeout(schema.TimeoutCreate),
234+
waitForDeploymentReplicasFunc(conn, out.GetNamespace(), out.GetName()))
235+
if err != nil {
236+
return err
237+
}
229238
}
230239

231240
log.Printf("[INFO] Submitted new deployment: %#v", out)
@@ -262,16 +271,19 @@ func resourceKubernetesDeploymentUpdate(d *schema.ResourceData, meta interface{}
262271
return fmt.Errorf("Failed to marshal update operations: %s", err)
263272
}
264273
log.Printf("[INFO] Updating deployment %q: %v", name, string(data))
265-
out, err := conn.AppsV1().Deployments(namespace).Patch(name, pkgApi.JSONPatchType, data)
274+
out, err := conn.AppsV1().Deployments(namespace).Patch(name, types.JSONPatchType, data)
266275
if err != nil {
267276
return fmt.Errorf("Failed to update deployment: %s", err)
268277
}
269278
log.Printf("[INFO] Submitted updated deployment: %#v", out)
270279

271-
err = resource.Retry(d.Timeout(schema.TimeoutUpdate),
272-
waitForDeploymentReplicasFunc(conn, namespace, name))
273-
if err != nil {
274-
return err
280+
if d.Get("wait_for_rollout").(bool) {
281+
log.Printf("[INFO] Waiting for deployment %s/%s to rollout", out.ObjectMeta.Namespace, out.ObjectMeta.Name)
282+
err := resource.Retry(d.Timeout(schema.TimeoutCreate),
283+
waitForDeploymentReplicasFunc(conn, out.GetNamespace(), out.GetName()))
284+
if err != nil {
285+
return err
286+
}
275287
}
276288

277289
return resourceKubernetesDeploymentRead(d, meta)

0 commit comments

Comments
 (0)