ansible-collections / kubernetes.core

The collection includes a variety of Ansible content to help automate the management of applications in Kubernetes and OpenShift clusters, as well as the provisioning and maintenance of clusters themselves.
Other
214 stars 133 forks source link

Using only label_selectors with k8s finds nothing but the same label_selectors used with k8s_info finds resources #617

Open jc305697 opened 1 year ago

jc305697 commented 1 year ago
SUMMARY

Hello, I have a weird problem and I hope somebody will be able to help me :) . If I only use label_selectors with k8s, it finds nothing but the same label_selectors if used with k8s_info finds the Nodes with this label.

I couldn't find an issue that covers this topic and any answers online in my research. Cheers, jc305697

ISSUE TYPE
COMPONENT NAME

k8s

ANSIBLE VERSION
ansible [core 2.14.4]
  config file = None
  configured module search path = ['/home/<myname>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/<myname>/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/<myname>/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/<myname>/.local/bin/ansible
  python version = 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True
COLLECTION VERSION
# /home/<myname>/.ansible/collections/ansible_collections
Collection      Version
--------------- -------
kubernetes.core 2.4.0

# /home/<myname>/.local/lib/python3.10/site-packages/ansible_collections
Collection      Version
--------------- -------
kubernetes.core 2.4.0
CONFIGURATION
CONFIG_FILE() = None
OS / ENVIRONMENT

workstation OS: Ubuntu 22.04.2 LTS (Jammy Jellyfish) target: OpenShift Cluster 4.12.11

STEPS TO REPRODUCE
# finds the worker nodes
- name: Get a list of all worker node
  kubernetes.core.k8s_info:
    label_selectors: node-role.kubernetes.io/worker=
    kind: node
  register: worker_nodes_list

- name: print content of worker_nodes_list
  ansible.builtin.debug:
    var: worker_nodes_list

- name: add label to worker nodes for ODF
  kubernetes.core.k8s:
    state: patched
    kind: Node
    label_selectors: node-role.kubernetes.io/worker=
    apply: true
    definition:
      metadata:
        labels:
          cluster.ocs.openshift.io/openshift-storage: ''
EXPECTED RESULTS

All worker nodes should have the following label applied to them: cluster.ocs.openshift.io/openshift-storage: ''

ACTUAL RESULTS

The specified label is not applied on the worker nodes since it seems that the module k8s finds no Node ressources matching the label selector but the k8s_info module finds nodes matching the label_selector.

TASK [. : add label to worker nodes for ODF] ***************************************************************************************************************************************************************************************************************************************************************************
task path: /home/<myname>/ocp-helm-config-ansible-role/tasks/preparatory_commands.yaml:12
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: <myname>
<127.0.0.1> EXEC /bin/sh -c 'echo ~<myname>&& sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/<myname>/.ansible/tmp `"&& mkdir "` echo /home/<myname>/.ansible/tmp/ansible-tmp-1682098280.6825972-25854-24837724271609 `" && echo ansible-tmp-1682098280.6825972-25854-24837724271609="` echo /home/<myname>/.ansible/tmp/ansible-tmp-1682098280.6825972-25854-24837724271609 `" ) && sleep 0'
Loading collection cloud.common from /home/<myname>/.local/lib/python3.10/site-packages/ansible_collections/cloud/common
Using module file /home/<myname>/.ansible/collections/ansible_collections/kubernetes/core/plugins/modules/k8s.py
<127.0.0.1> PUT /home/<myname>/.ansible/tmp/ansible-local-25707v91h1pxa/tmpogm75qvg TO /home/<myname>/.ansible/tmp/ansible-tmp-1682098280.6825972-25854-24837724271609/AnsiballZ_k8s.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/<myname>/.ansible/tmp/ansible-tmp-1682098280.6825972-25854-24837724271609/ /home/<myname>/.ansible/tmp/ansible-tmp-1682098280.6825972-25854-24837724271609/AnsiballZ_k8s.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python3 /home/<myname>/.ansible/tmp/ansible-tmp-1682098280.6825972-25854-24837724271609/AnsiballZ_k8s.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/<myname>/.ansible/tmp/ansible-tmp-1682098280.6825972-25854-24837724271609/ > /dev/null 2>&1 && sleep 0'
ok: [localhost] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "api_key": null,
            "api_version": "v1",
            "append_hash": false,
            "apply": true,
            "ca_cert": null,
            "client_cert": null,
            "client_key": null,
            "context": null,
            "continue_on_error": false,
            "definition": {
                "apiVersion": "v1",
                "kind": "Node",
                "metadata": {
                    "labels": {
                        "cluster.ocs.openshift.io/openshift-storage": "",
                        "mydate": "1682098276"
                    }
                }
            },
            "delete_options": null,
            "force": false,
            "generate_name": null,
            "host": null,
            "impersonate_groups": null,
            "impersonate_user": null,
            "kind": "Node",
            "kubeconfig": null,
            "label_selectors": [
                "node-role.kubernetes.io/worker="
            ],
            "merge_type": null,
            "name": null,
            "namespace": null,
            "no_proxy": null,
            "password": null,
            "persist_config": null,
            "proxy": null,
            "proxy_headers": null,
            "resource_definition": {
                "apiVersion": "v1",
                "kind": "Node",
                "metadata": {
                    "labels": {
                        "cluster.ocs.openshift.io/openshift-storage": "",
                        "mydate": "1682098276"
                    }
                }
            },
            "server_side_apply": null,
            "src": null,
            "state": "patched",
            "template": null,
            "username": null,
            "validate": null,
            "validate_certs": null,
            "wait": false,
            "wait_condition": null,
            "wait_sleep": 5,
            "wait_timeout": 120
        }
    },
    "msg": "resource 'kind=Node,name=None,namespace=None' filtered by label_selectors.",
    "result": {}
}
mjtrangoni commented 1 year ago

Any news on this? I faced exactly the same issue.

dgabrysch commented 10 months ago

Same here, wil this be fixed?

gialloguitar commented 9 months ago

The same request, will any updates? Of course theres workaround to use cli command: kubectl label node <label_key>=<label_value> -l <label_selector>

But it's not an Ansible way