Skip to content

Commit

Permalink
k8s: handle fail_json API from inventory plugin
Browse files Browse the repository at this point in the history
* inventory plugin does not have module object so fail_json
  will not work. Use AnsibleError instead of ``fail_json``

Fixes: #57

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde committed Apr 16, 2021
1 parent f5eb062 commit 6cc0630
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Check out code
uses: actions/checkout@v2
with:
path: ansible_collections/community/kubernetes
path: ansible_collections/kubernetes/core

- name: Set up Python ${{ matrix.python_version }}
uses: actions/setup-python@v1
Expand All @@ -31,7 +31,7 @@ jobs:

- name: Run sanity tests on Python ${{ matrix.python_version }}
run: make test-sanity PYTHON_VERSION=${{ matrix.python_version }}
working-directory: ./ansible_collections/community/kubernetes
working-directory: ./ansible_collections/kubernetes/core

integration:
runs-on: ubuntu-latest
Expand All @@ -43,7 +43,7 @@ jobs:
- name: Check out code
uses: actions/checkout@v2
with:
path: ansible_collections/community/kubernetes
path: ansible_collections/kubernetes/core

- name: Set up Python ${{ matrix.python_version }}
uses: actions/setup-python@v1
Expand All @@ -55,11 +55,11 @@ jobs:

- name: Run integration tests on Python ${{ matrix.python_version }}
run: make test-integration PYTHON_VERSION=${{ matrix.python_version }}
working-directory: ./ansible_collections/community/kubernetes
working-directory: ./ansible_collections/kubernetes/core

- name: Generate coverage report.
run: ansible-test coverage xml -v --requirements --group-by command --group-by version
working-directory: ./ansible_collections/community/kubernetes
working-directory: ./ansible_collections/kubernetes/core

- uses: codecov/codecov-action@v1
with:
Expand All @@ -74,7 +74,7 @@ jobs:
- name: Check out code
uses: actions/checkout@v2
with:
path: ansible_collections/community/kubernetes
path: ansible_collections/kubernetes/core

- name: Set up KinD cluster
uses: engineerd/setup-kind@v0.5.0
Expand Down Expand Up @@ -110,7 +110,7 @@ jobs:
- name: Run molecule default test scenario
run: make test-molecule
working-directory: ./ansible_collections/community/kubernetes
working-directory: ./ansible_collections/kubernetes/core

downstream-sanity-29:
runs-on: ubuntu-latest
Expand All @@ -121,7 +121,7 @@ jobs:
- name: Check out code
uses: actions/checkout@v2
with:
path: ansible_collections/community/kubernetes
path: ansible_collections/kubernetes/core

- name: Set up Python ${{ matrix.python_version }}
uses: actions/setup-python@v1
Expand All @@ -133,7 +133,7 @@ jobs:

- name: Run sanity tests on Python ${{ matrix.python_version }}
run: make downstream-test-sanity
working-directory: ./ansible_collections/community/kubernetes
working-directory: ./ansible_collections/kubernetes/core

downstream-integration-29:
runs-on: ubuntu-latest
Expand All @@ -145,7 +145,7 @@ jobs:
- name: Check out code
uses: actions/checkout@v2
with:
path: ansible_collections/community/kubernetes
path: ansible_collections/kubernetes/core

- name: Set up Python ${{ matrix.python_version }}
uses: actions/setup-python@v1
Expand All @@ -157,7 +157,7 @@ jobs:

- name: Run integration tests on Python ${{ matrix.python_version }}
run: make downstream-test-integration
working-directory: ./ansible_collections/community/kubernetes
working-directory: ./ansible_collections/kubernetes/core

downstream-molecule-29:
runs-on: ubuntu-latest
Expand All @@ -168,7 +168,7 @@ jobs:
- name: Check out code
uses: actions/checkout@v2
with:
path: ansible_collections/community/kubernetes
path: ansible_collections/kubernetes/core

- name: Set up KinD cluster
uses: engineerd/setup-kind@v0.5.0
Expand All @@ -188,4 +188,4 @@ jobs:
- name: Run molecule default test scenario
run: make downstream-test-molecule
working-directory: ./ansible_collections/community/kubernetes
working-directory: ./ansible_collections/kubernetes/core
2 changes: 2 additions & 0 deletions changelogs/fragments/fail_json.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- k8s - handle ``fail_json`` API from inventory plugin (https://github.com/ansible-collections/kubernetes.core/issues/57).
9 changes: 4 additions & 5 deletions plugins/inventory/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@
import json

from ansible.errors import AnsibleError
from ansible_collections.community.kubernetes.plugins.module_utils.common import K8sAnsibleMixin, HAS_K8S_MODULE_HELPER, k8s_import_exception
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
from ansible_collections.kubernetes.core.plugins.module_utils.common import (
K8sAnsibleMixin, HAS_K8S_MODULE_HELPER, k8s_import_exception
)
from ansible_collections.kubernetes.core.plugins.module_utils.exceptions import K8sInventoryException

try:
from openshift.dynamic.exceptions import DynamicApiError
Expand All @@ -137,10 +140,6 @@ def format_dynamic_api_exc(exc):
return '%s Reason: %s' % (exc.status, exc.reason)


class K8sInventoryException(Exception):
pass


class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable, K8sAnsibleMixin):
NAME = 'community.kubernetes.k8s'

Expand Down
6 changes: 4 additions & 2 deletions plugins/module_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
from datetime import datetime
from distutils.version import LooseVersion


from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.six import iteritems, string_types
from ansible.module_utils._text import to_native, to_bytes, to_text
from ansible.module_utils.common.dict_transformations import dict_merge
from ansible.module_utils.parsing.convert_bool import boolean
from ansible_collections.kubernetes.core.plugins.module_utils.exceptions import K8sInventoryException


K8S_IMP_ERR = None
Expand Down Expand Up @@ -425,7 +425,9 @@ def diff_objects(self, existing, new):
return True, result

def fail(self, msg=None):
self.fail_json(msg=msg)
if hasattr(self, 'fail_json'):
self.fail_json(msg=msg)
raise K8sInventoryException(msg)

def _wait_for(self, resource, name, namespace, predicate, sleep, timeout, state):
start = datetime.now()
Expand Down
11 changes: 11 additions & 0 deletions plugins/module_utils/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
# Copyright: (c) 2021, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type


class K8sInventoryException(Exception):
pass

0 comments on commit 6cc0630

Please sign in to comment.