ansible-collections / community.kubernetes

Kubernetes Collection for Ansible
https://galaxy.ansible.com/community/kubernetes
GNU General Public License v3.0
265 stars 104 forks source link

k8s_info with wait option returns immediately when resource doesn't exist #344

Closed hojerst closed 3 years ago

hojerst commented 3 years ago
SUMMARY

k8s_info with the wait option does return immediately when no object is found.

From the example in the

ISSUE TYPE
COMPONENT NAME

community.kubernetes.k8s_info (in collection v1.1.1)

ANSIBLE VERSION
ansible 2.9.13
  config file = /Users/hojerst/.ansible.cfg
  configured module search path = ['/Users/hojerst/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/hojerst/devel/.direnv/python-3.9.0/lib/python3.9/site-packages/ansible
  executable location = /Users/hojerst/devel/.direnv/python-3.9.0/bin/ansible
  python version = 3.9.0 (default, Nov 28 2020, 00:47:30) [Clang 12.0.0 (clang-1200.0.32.27)]
CONFIGURATION
(none)
OS / ENVIRONMENT
STEPS TO REPRODUCE

run the example from the documentation without a object "pod/pod-not-yet-created"

- name: Wait till the Object is created
  community.kubernetes.k8s_info:
    kind: Pod
    wait: yes
    name: pod-not-yet-created
    namespace: default
    wait_sleep: 10
    wait_timeout: 360
EXPECTED RESULTS

I'm expecting that the module waits for up to 360 seconds until the resource "pod/pod-not-yet-created" is in the desired state (Running for Pods).

ACTUAL RESULTS

The module returns immediately without any waiting if the resource is not yet created. This makes the module unusable when the resources are indirectly created by other resources. e.g. a pod which is created by a deployment (or a pod created by a operator)

(no error text output, but this is the "register: result" json object:

{
    "changed": false,
    "failed": false,
    "resources": []
}
gravesm commented 3 years ago

@hojerst Thank you for reporting this. I'll take a look.

gravesm commented 3 years ago

@tima @Akasurde Thinking about this and I'm not actually sure what the behavior should be. In the case of a specific object as described here--we're given a name, for example--we can wait until the object is created or wait_timeout, whichever comes first. If we are given a less specific query, like listing all the pods in a namespace, for example:

k8s_info:
  kind: Pod
  namespace: foo
  wait: true
  wait_timeout: 180

the only way that's ever satisfied is to always wait the full timeout period.

hojerst commented 3 years ago

imho there are two sane algorithms which might work in this case:

  1. query once and then check/wait for all resulting items to reach the desired condition (which seems to be implemented right now)
  2. query continuously (on every iteration) and then check if the desired condition is reached

maybe this can be switched by parameter which defaults to the current implementation. (or use a "magic default" depending if "name" is specified)

This should make this module usable for two usecases:

Which ever implementation is used, I think the documentation needs to be clear about the implications. As both scenarios might be needed in plays.