Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

k8s_info doesn't work with Kubernetes Custom Resources (CR) #202

Closed
fmuyassarov opened this issue Aug 21, 2020 · 8 comments
Closed

k8s_info doesn't work with Kubernetes Custom Resources (CR) #202

fmuyassarov opened this issue Aug 21, 2020 · 8 comments
Assignees
Labels
type/documentation Improvements or additions to documentation

Comments

@fmuyassarov
Copy link

SUMMARY

Ansible Module k8s_info doesn't recognize Kubernetes Custom Resources (CR) even though kubectl client does.

I have created custom resource on my Kubernetes cluster called Baremetalhost. I can do any CRUD (create, read, update, delete) operations on that custom resource using kubectl.
Example:

$ kubectl get baremetalhost -n dev
NAME     STATUS   PROVISIONING STATUS   CONSUMER   BMC                                                                                         HARDWARE PROFILE   ONLINE   ERROR
node-0   OK       ready                            ipmi://192.168.111.1:6230                                                                   unknown            false    
node-1   OK       ready                            redfish+http://192.168.111.1:8000/redfish/v1/Systems/78078490-0e97-4230-853b-63d248107cdf   unknown            false    

However, when try to use k8s_info module, the playbook doesn't print out expected list of Baremetalhosts. Currently, I can run k8s_info module only against standard Kubernetes objects, such as Pod, Deployment, ConfigMap, etc but not custom objects that are already recognized by the Kubernetes API server.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

k8s_info

ANSIBLE VERSION
ansible 2.9.8
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/centos/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
OS / ENVIRONMENT

CentOS Linux 8 (Core)

STEPS TO REPRODUCE
  1. Apply Machine Custom Resource Definition (CRD) to the cluster
  2. Run the following example playbook:
---

- name: example
  hosts: localhost 
  tasks:
    - name: get the list of the Baremetalhost objects
      k8s_info:
        kind: Baremetalhost
        namespace: dev
EXPECTED RESULTS

List of the Baremetalhost objects.

ACTUAL RESULTS
ok: [localhost] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "api_key": null,
            "api_version": "v1",
            "ca_cert": null,
            "client_cert": null,
            "client_key": null,
            "context": null,
            "field_selectors": [],
            "host": null,
            "kind": "Baremetalhost",
            "kubeconfig": null,
            "label_selectors": [],
            "name": null,
            "namespace": "metal3",
            "password": null,
            "proxy": null,
            "username": null,
            "validate_certs": null
        }
    },
    "resources": []
}
META: ran handlers
META: ran handlers
@geerlingguy
Copy link
Collaborator

@fmuyassarov - Just to confirm, if you run kubectl get Baremetalhost -n dev does it print a list of Baremetalhost resources?

@fmuyassarov
Copy link
Author

fmuyassarov commented Aug 21, 2020

@fmuyassarov - Just to confirm, if you run kubectl get Baremetalhost -n dev does it print a list of Baremetalhost resources?

Yes it does. Here is the example:

[centos@centos-feruz ~]$ kubectl get Baremetalhost -n dev
NAME     STATUS   PROVISIONING STATUS   CONSUMER   BMC                                                                                         HARDWARE PROFILE   ONLINE   ERROR
node-0   OK       ready                            ipmi://192.168.111.1:6230                                                                   unknown            false    
node-1   OK       ready                            redfish+http://192.168.111.1:8000/redfish/v1/Systems/78078490-0e97-4230-853b-63d248107cdf   unknown            false    
[centos@centos-feruz ~]$ 

@fmuyassarov
Copy link
Author

I have tried with other custom resources as well that are recognized by the k8s API server. However, I get the same result from Ansible k8s_info for all of them. I also thought that the kind of the object is key sensitive, so I tried with both Baremetalhost and baremetalhost, but no success.

@tima tima added type/bug Something isn't working needs_triage labels Aug 21, 2020
@Akasurde
Copy link
Member

@fmuyassarov Since you are using custom resources, it typically has different api_version which is different than default value v1. You will need to specify the api_version in those cases.

For example - if you create a custom resource crontab like https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/ and custom object like https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#create-custom-objects, then

your playbook will look like -

---
- hosts: localhost
  tasks:
    - community.kubernetes.k8s_info:
        kind: crontab
        api_version: "stable.example.com/v1"

you will get -

ok: [localhost] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "api_key": null,
            "api_version": "stable.example.com/v1",
            "ca_cert": null,
            "client_cert": null,
            "client_key": null,
            "context": null,
            "field_selectors": [],
            "host": null,
            "kind": "crontab",
            "kubeconfig": null,
            "label_selectors": [],
            "name": null,
            "namespace": null,
            "password": null,
            "persist_config": null,
            "proxy": null,
            "username": null,
            "validate_certs": null
        }
    },
    "resources": [
        {
            "apiVersion": "stable.example.com/v1",
            "kind": "CronTab",
            "metadata": {
                "annotations": {
                    "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"stable.example.com/v1\",\"kind\":\"CronTab\",\"metadata\":{\"annotations\":{},\"name\":\"my-new-cron-object\",\"namespace\":\"default\"},\"spec\":{\"cronSpec\":\"* * * * */5\",\"image\":\"my-awesome-cron-image\"}}\n"
                },
                "creationTimestamp": "2020-08-24T05:05:14Z",
                "generation": 1,
                "name": "my-new-cron-object",
                "namespace": "default",
                "resourceVersion": "1941808",
                "selfLink": "/apis/stable.example.com/v1/namespaces/default/crontabs/my-new-cron-object",
                "uid": "3800ecbb-3473-49ea-b1a8-4ff4fab42654"
            },
            "spec": {
                "cronSpec": "* * * * */5",
                "image": "my-awesome-cron-image"
            }
        }
    ]
}

needs_info

@Akasurde Akasurde added type/documentation Improvements or additions to documentation and removed needs_triage type/bug Something isn't working labels Aug 24, 2020
@Akasurde Akasurde added this to the 1.1.0 milestone Aug 24, 2020
@Akasurde
Copy link
Member

Let me know if it does not work after specifying api_version. I updated the example section for this case - #203

@Akasurde Akasurde self-assigned this Aug 24, 2020
@Akasurde Akasurde removed this from the 1.1.0 milestone Aug 24, 2020
@fmuyassarov
Copy link
Author

Let me know if it does not work after specifying api_version. I updated the example section for this case - #203

@Akasurde specifying API version worked. Thanks a lot. I forgot to add custom API in the task. But I think it's worth to add a note to the documentation.

@Akasurde
Copy link
Member

@fmuyassarov Could you please take a look at #203 and comment if it looks to you? Thanks for the feedback.

@fabianvf
Copy link
Collaborator

Documentation issue addressed in #203

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type/documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

5 participants