Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k8s module does not work with openshift Template kind due to ResourceNotUniqueError #58130

Closed
lucastheisen opened this issue Jun 20, 2019 · 4 comments
Labels
affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. clustering Clustering category k8s module This issue/PR relates to a module. support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback.

Comments

@lucastheisen
Copy link

SUMMARY

OpenShift returns two values when you search for kind: Template which causes a request to create a template to fail.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

k8s

ANSIBLE VERSION
ansible 2.8.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ltheisen/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0]
CONFIGURATION

OS / ENVIRONMENT

WSL:

Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:        18.04
Codename:       bionic

Windows:

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      16299  0
STEPS TO REPRODUCE

Replace the placeholders in the playbook that follows with your openshift info, and run the playbook

---
- hosts: localhost

  vars:
    k8s:
      context: <yourcontext>
      namespace: <yournamespace>
      verify_ssl: <yourverifyssl>

  tasks:

  - name: Create template
    k8s:
      context: "{{ k8s.context }}"
      namespace: "{{ k8s.namespace }}"
      verify_ssl: "{{ k8s.verify_ssl }}"
      definition:
        apiVersion: v1
        kind: Template
        name: foo-template
        objects:
        - apiVersion: v1
          kind: Pod
          spec:
            containers:
            - command: ['/bin/echo', 'hello']
              image: alpine
              imagePullPolicy: Always
              name: echo-hello
              resources:
                requests:
                  memory: 1M
                  cpu: 1m
EXPECTED RESULTS

The template gets created

ACTUAL RESULTS
   TASK [Create template] ************************************************************************************************************************************************************************************************************************************************
   task path: /opt/ansible/test-k8s-template-openshift.yml:18
   Thursday 20 June 2019  16:43:29 +0000 (0:00:00.529)       0:00:02.465 *********
   <foo-dev> ESTABLISH LOCAL CONNECTION FOR USER: root
   <foo-dev> EXEC /bin/sh -c 'echo ~root && sleep 0'
   <foo-dev> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1561049009.53-187290390623533 `" && echo ansible-tmp-1561049009.53-187290390623533="` echo /root/.ansible/tmp/ansible-tmp-1561049009.53-187290390623533 `" ) && sleep 0'
   Using module file /usr/lib/python2.7/site-packages/ansible/modules/clustering/k8s/k8s.py
   <foo-dev> PUT /root/.ansible/tmp/ansible-local-61cqADTe/tmpPmuh9K TO /root/.ansible/tmp/ansible-tmp-1561049009.53-187290390623533/AnsiballZ_k8s.py
   <foo-dev> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1561049009.53-187290390623533/ /root/.ansible/tmp/ansible-tmp-1561049009.53-187290390623533/AnsiballZ_k8s.py && sleep 0'
   <foo-dev> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1561049009.53-187290390623533/AnsiballZ_k8s.py && sleep 0'
   <foo-dev> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1561049009.53-187290390623533/ > /dev/null 2>&1 && sleep 0'
   The full traceback is:
   WARNING: The below traceback may *not* be related to the actual failure.
     File "/tmp/ansible_k8s_payload_xec7AK/ansible_k8s_payload.zip/ansible/module_utils/k8s/common.py", line 193, in find_resource
       return self.client.resources.get(api_version=api_version, short_names=[kind])
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 753, in get
       results = self.search(**kwargs)
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 794, in search
       results = self.__search(self.__build_search(**kwargs), self.__resources, [])
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 836, in __search
       matches.extend(self.__search([key] + parts[1:], resources, reqParams))
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 822, in __search
       return self.__search(parts[1:], resourcePart, reqParams + [part] )
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 836, in __search
       matches.extend(self.__search([key] + parts[1:], resources, reqParams))
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 822, in __search
       return self.__search(parts[1:], resourcePart, reqParams + [part] )
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 818, in __search
       return self.__search(parts[1:], resourcePart.resources, reqParams)
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 836, in __search
       matches.extend(self.__search([key] + parts[1:], resources, reqParams))
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 827, in __search
       if getattr(_resource, term) == value:
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 524, in __getattr__
       if self.base_resource():
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 421, in base_resource
       self.__base_resource = self.client.resources.get(group=self.group, api_version=self.api_version, kind=self.base_kind)
     File "/usr/lib/python2.7/site-packages/openshift/dynamic/client.py", line 767, in get
       raise ResourceNotUniqueError('Multiple matches found for {}: {}'.format(kwargs, results))
   fatal: [foo-dev]: FAILED! => {
       "changed": false,
       "invocation": {
           "module_args": {
               "api_key": null,
               "api_version": "v1",
               "append_hash": false,
               "ca_cert": null,
               "client_cert": null,
               "client_key": null,
               "context": "pd-kube-ci/okd-load-mitre-org:8443/ltheisen",
               "definition": {
                   "apiVersion": "v1",
                   "kind": "Template",
                   "objects": [
                       {
                           "apiVersion": "v1",
                           "kind": "Pod",
                           "spec": {
                               "containers": [
                                   {
                                       "command": [
                                           "/bin/echo",
                                           "hello"
                                       ],
                                       "image": "alpine",
                                       "imagePullPolicy": "Always",
                                       "name": "echo-hello",
                                       "resources": {
                                           "requests": {
                                               "cpu": "1m",
                                               "memory": "1M"
                                           }
                                       }
                                   }
                               ]
                           }
                       }
                   ]
               },
               "force": false,
               "host": null,
               "kind": null,
               "kubeconfig": null,
               "merge_type": null,
               "name": null,
               "namespace": "pd-kube-ci",
               "password": null,
               "resource_definition": {
                   "apiVersion": "v1",
                   "kind": "Template",
                   "objects": [
                       {
                           "apiVersion": "v1",
                           "kind": "Pod",
                           "spec": {
                               "containers": [
                                   {
                                       "command": [
                                           "/bin/echo",
                                           "hello"
                                       ],
                                       "image": "alpine",
                                       "imagePullPolicy": "Always",
                                       "name": "echo-hello",
                                       "resources": {
                                           "requests": {
                                               "cpu": "1m",
                                               "memory": "1M"
                                           }
                                       }
                                   }
                               ]
                           }
                       }
                   ]
               },
               "src": null,
               "state": "present",
               "username": null,
               "validate": null,
               "validate_certs": false,
               "verify_ssl": false,
               "wait": false,
               "wait_condition": null,
               "wait_timeout": 120
           }
       },
       "msg": "Failed to find exact match for v1.Template by [kind, name, singularName, shortNames]"
   }
@lucastheisen lucastheisen changed the title k8s module does not work with openshift Template kinde due to ResourceNotUniqueError k8s module does not work with openshift Template kind due to ResourceNotUniqueError Jun 20, 2019
@lucastheisen
Copy link
Author

Had a conversation with @fabianvf on the upstream openshift python client which he says is behaving correctly in that openshift itself replies with multiple values for kind Template. If openshift is correct in having multiple values, and the client is correct in throwing ResourceNotUniqueError when the kind is not unique, then the only solution would be for the k8s module to search prior to get and allow for more thorough filtering (using other attributes like name, short_name...)

@ansibot
Copy link
Contributor

ansibot commented Jun 20, 2019

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. clustering Clustering category k8s module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback. labels Jun 20, 2019
@LorenzBischof
Copy link

A quick workaround is to copy the k8s module_utils and patch common.py to ignore processedtemplates: copy the get method from: https://github.com/openshift/openshift-restclient-python/blob/v0.9.0/openshift/dynamic/client.py#L748 and modify it to include:

if len(results) > 1:
            results = [result for result in results if not result.name == 'processedtemplates']

rayfordj pushed a commit to rayfordj/mig-ci that referenced this issue Sep 30, 2019
@lucastheisen
Copy link
Author

Looks like this was resolved in the upstream lib as part of PR 333

@sivel sivel removed the needs_triage Needs a first human triage before being processed. label Mar 19, 2020
@ansible ansible locked and limited conversation to collaborators Apr 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. clustering Clustering category k8s module This issue/PR relates to a module. support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback.
Projects
None yet
Development

No branches or pull requests

4 participants