Skip to content

Commit

Permalink
Merge pull request #255 from s-diez/feat/wait-for-statefulset
Browse files Browse the repository at this point in the history
Feat/wait for statefulset
  • Loading branch information
pst authored Jul 7, 2024
2 parents 2333640 + 2b9c32b commit 44c7083
Show file tree
Hide file tree
Showing 17 changed files with 359 additions and 136 deletions.
4 changes: 2 additions & 2 deletions docs/resources/resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ resource "kustomization_resource" "p0" {
# then loop through resources in ids_prio[1]
# and set an explicit depends_on on kustomization_resource.p0
# wait 2 minutes for any deployment or daemonset to become ready
# wait 2 minutes for any deployment, statefulset or daemonset to become ready
resource "kustomization_resource" "p1" {
for_each = data.kustomization_build.test.ids_prio[1]
Expand Down Expand Up @@ -105,5 +105,5 @@ resource "kustomization_resource" "p2" {
## Argument Reference

- `manifest` - (Required) JSON encoded Kubernetes resource manifest.
- `wait` - Whether to wait for pods to become ready (default false). Currently only has an effect for Deployments and DaemonSets.
- `wait` - Whether to wait for pods to become ready (default false). Currently only has an effect for Deployments, StatefulSets and DaemonSets.
- 'timeouts' - (Optional) Overwrite `create`, `update` or `delete` timeout defaults. Defaults are 5 minutes for `create` and `update` and 10 minutes for `delete`.
37 changes: 35 additions & 2 deletions kustomize/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (
)

var waitRefreshFunctions = map[string]waitRefreshFunction{
"apps/Deployment": waitDeploymentRefresh,
"apps/Daemonset": waitDaemonsetRefresh,
"apps/Deployment": waitDeploymentRefresh,
"apps/DaemonSet": waitDaemonsetRefresh,
"apps/StatefulSet": waitStatefulSetRefresh,
}

type kManifestId struct {
Expand Down Expand Up @@ -429,6 +430,38 @@ func waitDeploymentRefresh(km *kManifest) (interface{}, string, error) {
return nil, "in progress", nil
}

func statefulSetReady(u *k8sunstructured.Unstructured) (bool, error) {
var statefulSet k8sappsv1.StatefulSet
if err := k8sruntime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), &statefulSet); err != nil {
return false, err
}
if statefulSet.Generation == statefulSet.Status.ObservedGeneration &&
statefulSet.Status.AvailableReplicas == *statefulSet.Spec.Replicas &&
statefulSet.Status.AvailableReplicas == statefulSet.Status.Replicas {
return true, nil
} else {
return false, nil
}
}

func waitStatefulSetRefresh(km *kManifest) (interface{}, string, error) {
resp, err := km.apiGet(k8smetav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
return nil, "missing", nil
}
return nil, "error", err
}
ready, err := statefulSetReady(resp)
if err != nil {
return nil, "error", err
}
if ready {
return resp, "done", nil
}
return nil, "in progress", nil
}

func (km *kManifest) waitCreatedOrUpdated(t time.Duration) error {
gvk := km.gvk()
if refresh, ok := waitRefreshFunctions[fmt.Sprintf("%s/%s", gvk.Group, gvk.Kind)]; ok {
Expand Down
Loading

0 comments on commit 44c7083

Please sign in to comment.