diff --git a/changelogs/fragments/391-fix-statefulset-wait.yaml b/changelogs/fragments/391-fix-statefulset-wait.yaml new file mode 100644 index 00000000..2fe84d84 --- /dev/null +++ b/changelogs/fragments/391-fix-statefulset-wait.yaml @@ -0,0 +1,2 @@ +bugfixes: + - k8s_scale - fix waiting on statefulset when scaled down to 0 replicas (https://github.com/ansible-collections/kubernetes.core/issues/203). diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index 9fec2f77..51343a9b 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -633,6 +633,10 @@ class K8sAnsibleMixin(object): ) def _statefulset_ready(statefulset): + # These may be None + updated_replicas = statefulset.status.updatedReplicas or 0 + ready_replicas = statefulset.status.readyReplicas or 0 + return ( statefulset.status and statefulset.spec.updateStrategy.type == "RollingUpdate" @@ -640,8 +644,8 @@ class K8sAnsibleMixin(object): == (statefulset.metadata.generation or 0) and statefulset.status.updateRevision == statefulset.status.currentRevision - and statefulset.status.updatedReplicas == statefulset.spec.replicas - and statefulset.status.readyReplicas == statefulset.spec.replicas + and updated_replicas == statefulset.spec.replicas + and ready_replicas == statefulset.spec.replicas and statefulset.status.replicas == statefulset.spec.replicas ) diff --git a/tests/integration/targets/k8s_scale/tasks/main.yml b/tests/integration/targets/k8s_scale/tasks/main.yml index 81e2146e..d58a01bc 100644 --- a/tests/integration/targets/k8s_scale/tasks/main.yml +++ b/tests/integration/targets/k8s_scale/tasks/main.yml @@ -341,6 +341,54 @@ - scale_out.changed - scale_out.results | map(attribute='result.status.replicas') | list | unique == [2] + - name: Create a StatefulSet + kubernetes.core.k8s: + wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" + definition: + apiVersion: apps/v1 + kind: StatefulSet + metadata: + namespace: "{{ scale_namespace }}" + name: scale-set + spec: + replicas: 2 + selector: + matchLabels: + app: foo + template: + metadata: + labels: + app: foo + spec: + terminationGracePeriodSeconds: 10 + containers: + - image: busybox + name: busybox + command: + - sleep + - "600" + register: output + + - assert: + that: + - output.result.status.replicas == 2 + + - name: Wait for StatefulSet to scale down to 0 + kubernetes.core.k8s_scale: + kind: StatefulSet + api_version: apps/v1 + name: scale-set + namespace: "{{ scale_namespace }}" + replicas: 0 + wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" + register: output + + - assert: + that: + - output.result.status.replicas == 0 + always: - name: Remove namespace k8s: