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

openshift inventory plugin: fix exception when auth fails #45826

Conversation

pilou-
Copy link
Contributor

@pilou- pilou- commented Sep 19, 2018

SUMMARY

Fix ForbiddenError object has no attribute message:

[WARNING]:  * Failed to parse test.yml with openshift plugin: 'ForbiddenError' object has no attribute 'message'
 File "ansible/lib/ansible/inventory/manager.py", line 270, in parse_source
   plugin.parse(self._inventory, self._loader, source, cache=cache)
 File "ansible/lib/ansible/plugins/inventory/openshift.py", line 122, in parse
   self.setup(config_data, cache, cache_key)
 File "ansible/lib/ansible/module_utils/k8s/inventory.py", line 58, in setup
   self.fetch_objects(connections)
 File "ansible/lib/ansible/module_utils/k8s/inventory.py", line 250, in fetch_objects
   super(OpenShiftInventoryHelper, self).fetch_objects(connections)
 File "ansible/lib/ansible/module_utils/k8s/inventory.py", line 81, in fetch_objects
   namespaces = self.get_available_namespaces(client)
 File "ansible/lib/ansible/module_utils/k8s/inventory.py", line 95, in get_available_namespaces
   raise K8sInventoryException('Error fetching Namespace list: {0}'.format(exc.message))

Don't try to get message attribute from:

  • K8sInventoryException instances
  • Exception instances
  • KubernetesException instances (because KubernetesException can be Exception)
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

lib/ansible/plugins/inventory/openshift.py

ANSIBLE VERSION
ansible 2.8.0.dev0 (devel af40d8c2a5) last updated 2018/09/19 01:46:21 (GMT +200)

@ansibot
Copy link
Contributor

ansibot commented Sep 19, 2018

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. 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 Sep 19, 2018
@willthames
Copy link
Contributor

willthames commented Sep 19, 2018

I'd rather not use str.format(exc) for DynamicApiException, it's very verbose - can we use json.loads(exc.body)['message'] instead?

(would be good to have a helper function to do that, I want to make the same change in k8s/raw.py too)

@ansibot ansibot removed the needs_triage Needs a first human triage before being processed. label Sep 19, 2018
@pilou- pilou- force-pushed the oc_plugin_inventory_object_has_no_attribute_message branch from 9152b23 to 065bf81 Compare September 19, 2018 10:22
@pilou-
Copy link
Contributor Author

pilou- commented Sep 19, 2018

Done.

Note that we loose the original backtrace. Once merged, I will ask if backports (2.7 and 2.6) are allowed (if so, i will create backport PRs) and create another PR in order to use format_dynamic_api_exc helper in k8s/raw.py.

@pilou- pilou- force-pushed the oc_plugin_inventory_object_has_no_attribute_message branch 2 times, most recently from 15af713 to e804f24 Compare September 19, 2018 10:37
Fix 'ForbiddenError' object has no attribute 'message':

    [WARNING]:  * Failed to parse test.yml with openshift plugin: 'ForbiddenError' object has no attribute 'message'
     File "ansible/lib/ansible/inventory/manager.py", line 270, in parse_source
       plugin.parse(self._inventory, self._loader, source, cache=cache)
     File "ansible/lib/ansible/plugins/inventory/openshift.py", line 122, in parse
       self.setup(config_data, cache, cache_key)
     File "ansible/lib/ansible/module_utils/k8s/inventory.py", line 58, in setup
       self.fetch_objects(connections)
     File "ansible/lib/ansible/module_utils/k8s/inventory.py", line 250, in fetch_objects
       super(OpenShiftInventoryHelper, self).fetch_objects(connections)
     File "ansible/lib/ansible/module_utils/k8s/inventory.py", line 81, in fetch_objects
       namespaces = self.get_available_namespaces(client)
     File "ansible/lib/ansible/module_utils/k8s/inventory.py", line 95, in get_available_namespaces
       raise K8sInventoryException('Error fetching Namespace list: {0}'.format(exc.message))

Don't try to get 'message' attribute from:
- K8sInventoryException instances
- Exception instances
- KubernetesException instances (because KubernetesException can be
  Exception)
@pilou- pilou- force-pushed the oc_plugin_inventory_object_has_no_attribute_message branch from e804f24 to 9d06668 Compare September 19, 2018 10:40
@willthames
Copy link
Contributor

willthames commented Sep 19, 2018

@pilou- that looks great. I'm not clear why we lose the backtrace though, and if there's anything we can do to get it back (is it just that it was part of e.message?)

We can presumably use the traceback library and maybe put that in as a keyword arg to the exception (a bit like how fail_json_aws works - perhaps a fail_json_k8s would look pretty similar?)

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Sep 19, 2018
inventory plugin specific code should not be located in
lib/ansible/module_utils directory. Then ansible.utils methods can be
reused (for example Display).
@pilou-
Copy link
Contributor Author

pilou- commented Sep 20, 2018

About the traceback: it is not possible to use something like fail_json_aws because there is no AnsibleModule instance there. In fact module.utils.Display should be used in order to be more verbose when debug is enabled. That's why I moved all code specific to inventory only from ansible.module_utils.k8s to ansible.plugins.inventory.k8s.

Now Display can be used and traceback is displayed when -vvv switch is used.
Note there isn't any license problem since ansible.module_utils.k8s is GPL.

cache_key = self._get_cache_prefix(path)
config_data = self._read_config_data(path)
self.setup(config_data, cache, cache_key)
helper = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It comes from:

Not used since 4d77878: I removed it.

@fabianvf
Copy link
Contributor

:shipit:

NAME = 'k8s'

transport = 'kubectl'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for? kubectl is not used at all by ansible, afaik

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didnt' add it, it comes from:

It is used there in order to define connection plugin (kubectl or oc) which will be used with the generated inventory:

self.inventory.set_variable(container_name, 'ansible_connection', self.transport)
self.inventory.set_variable(container_name, 'ansible_{0}_pod'.format(self.transport),
pod_name)
self.inventory.set_variable(container_name, 'ansible_{0}_container'.format(self.transport),
container.name)
self.inventory.set_variable(container_name, 'ansible_{0}_namespace'.format(self.transport),
namespace)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kubectl is a valid connection plugin, without looking into the code further I assume that's being used as the transport backend in that inventory plugin.

https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/kubectl.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, ansible_connection selects the connection plugin :)

@fabianvf
Copy link
Contributor

bot_status

@ansibot
Copy link
Contributor

ansibot commented Sep 26, 2018

Components

lib/ansible/module_utils/k8s/common.py
support: community
maintainers: chouseknecht fabianvf flaper87 maxamillion willthames

lib/ansible/module_utils/k8s/inventory.py
support: community
maintainers: chouseknecht fabianvf flaper87 maxamillion willthames

lib/ansible/module_utils/k8s/scale.py
support: community
maintainers: chouseknecht fabianvf flaper87 maxamillion willthames

lib/ansible/plugins/inventory/k8s.py
support: community
maintainers:

lib/ansible/plugins/inventory/openshift.py
support: community
maintainers:

Metadata

waiting_on: maintainer
changes_requested_by: null
needs_info: False
needs_revision: False
needs_rebase: False
merge_commits: []
too many files or commits: False
mergeable_state: clean
shippable_status: success
maintainer_shipits (module maintainers): 0
community_shipits (namespace maintainers): 0
ansible_shipits (core team members): 0
shipit_actors (maintainer or core team member): []
shipit_actors_other: []
automerge: automerge shipit test failed

click here for bot help

@fabianvf
Copy link
Contributor

shipit

@maxamillion
Copy link
Contributor

rebuild_merge

@ansibot ansibot added the inventory Inventory category label Sep 26, 2018
@ansibot ansibot merged commit 2fd18c7 into ansible:devel Sep 26, 2018
@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
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. inventory Inventory category support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants