diff --git a/changelogs/fragments/756-fix-daemonset-waiting.yaml b/changelogs/fragments/756-fix-daemonset-waiting.yaml new file mode 100644 index 0000000000..e186e5ef62 --- /dev/null +++ b/changelogs/fragments/756-fix-daemonset-waiting.yaml @@ -0,0 +1,2 @@ +bugfixes: + - waiter - Fix waiting for daemonset when desired number of pods is 0. (https://github.com/ansible-collections/kubernetes.core/pull/756). diff --git a/plugins/module_utils/k8s/waiter.py b/plugins/module_utils/k8s/waiter.py index 5328d63180..16ee10dda5 100644 --- a/plugins/module_utils/k8s/waiter.py +++ b/plugins/module_utils/k8s/waiter.py @@ -51,7 +51,7 @@ def daemonset_ready(daemonset: ResourceInstance) -> bool: return bool( daemonset.status and daemonset.status.desiredNumberScheduled is not None - and daemonset.status.updatedNumberScheduled + and (daemonset.status.updatedNumberScheduled or 0) == daemonset.status.desiredNumberScheduled and daemonset.status.numberReady == daemonset.status.desiredNumberScheduled and daemonset.status.observedGeneration == daemonset.metadata.generation diff --git a/tests/integration/targets/k8s_waiter/defaults/main.yml b/tests/integration/targets/k8s_waiter/defaults/main.yml index 04c873ce36..002c00238c 100644 --- a/tests/integration/targets/k8s_waiter/defaults/main.yml +++ b/tests/integration/targets/k8s_waiter/defaults/main.yml @@ -5,6 +5,7 @@ k8s_pod_metadata: k8s_pod_spec: serviceAccount: "{{ k8s_pod_service_account }}" + nodeSelector: "{{ k8s_pod_node_selector }}" containers: - image: "{{ k8s_pod_image }}" imagePullPolicy: Always @@ -33,6 +34,8 @@ k8s_pod_ports: [] k8s_pod_env: [] +k8s_pod_node_selector: {} + k8s_pod_template: metadata: "{{ k8s_pod_metadata }}" spec: "{{ k8s_pod_spec }}" diff --git a/tests/integration/targets/k8s_waiter/tasks/main.yml b/tests/integration/targets/k8s_waiter/tasks/main.yml index 1cb7e4c405..3bfb668bb6 100644 --- a/tests/integration/targets/k8s_waiter/tasks/main.yml +++ b/tests/integration/targets/k8s_waiter/tasks/main.yml @@ -127,6 +127,48 @@ - ds.result.status.currentNumberScheduled == ds.result.status.desiredNumberScheduled - updated_ds_pods.resources[0].spec.containers[0].image.endswith(":3") + - name: Create daemonset with nodeSelector and not existing label + k8s: + definition: + apiVersion: apps/v1 + kind: DaemonSet + metadata: + name: wait-daemonset-not-existing-label + namespace: "{{ wait_namespace }}" + spec: + selector: + matchLabels: + app: "{{ k8s_pod_name }}" + template: "{{ k8s_pod_template }}" + wait: yes + wait_sleep: 5 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" + vars: + k8s_pod_name: wait-daemonset-not-existing-label + k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:1 + k8s_pod_command: + - sleep + - "600" + k8s_pod_node_selector: + nonExisitingLabel: test-not-exiting-label + register: ds_not_existing_label + + - name: Get updated pods + k8s_info: + api_version: v1 + kind: Pod + namespace: "{{ wait_namespace }}" + label_selectors: + - app=wait-daemonset-not-existing-label + register: updated_ds_pods_not_existing_label + + - name: Check that daemonset wait worked (when desired number is 0) + assert: + that: + - ds_not_existing_label.result.status.currentNumberScheduled == ds_not_existing_label.result.status.desiredNumberScheduled + - ds_not_existing_label.result.status.desiredNumberScheduled == 0 + - updated_ds_pods_not_existing_label.resources | length == 0 + - name: Add a statefulset k8s: definition: