Skip to content

Commit

Permalink
Fix waiting on StatefulSet scale down (#391)
Browse files Browse the repository at this point in the history
Fix waiting on StatefulSet scale down

SUMMARY

When scaling a StatefulSet down to 0 replicas the wait will fail
because some properties of the status (readyReplicas, updatedReplicas)
will not exist. These are probably defined as omitempty in the API and
since the value is zero are not present in the response.

Fixes #203
ISSUE TYPE


Bugfix Pull Request

COMPONENT NAME

k8s_scale
ADDITIONAL INFORMATION

Reviewed-by: Gonéri Le Bouder <[email protected]>
  • Loading branch information
gravesm authored Mar 11, 2022
1 parent 30e84fa commit d68dec3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/391-fix-statefulset-wait.yaml
Original file line number Diff line number Diff line change
@@ -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).
8 changes: 6 additions & 2 deletions plugins/module_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,15 +633,19 @@ def _daemonset_ready(daemonset):
)

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"
and statefulset.status.observedGeneration
== (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
)

Expand Down
48 changes: 48 additions & 0 deletions tests/integration/targets/k8s_scale/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d68dec3

Please sign in to comment.