diff --git a/.ansible-lint b/.ansible-lint new file mode 100644 index 0000000000..021b91886b --- /dev/null +++ b/.ansible-lint @@ -0,0 +1,6 @@ +--- +profile: production + +exclude_paths: + - tests/integration + - tests/sanity diff --git a/.github/workflows/changelog.yaml b/.github/workflows/changelog.yaml new file mode 100644 index 0000000000..b59768b418 --- /dev/null +++ b/.github/workflows/changelog.yaml @@ -0,0 +1,21 @@ +--- +name: Changelog +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: + pull_request: + types: + - opened + - reopened + - labeled + - unlabeled + - synchronize + branches: + - main + - stable-* + +jobs: + changelog: + uses: ansible-network/github_actions/.github/workflows/changelog.yml@main diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index 091055c638..0000000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,313 +0,0 @@ -name: CI -concurrency: - group: ${{ github.head_ref }} - cancel-in-progress: true - -on: - pull_request: - types: - - opened - - reopened - - labeled - - unlabeled - - synchronize - branches: - - main - - stable-* - tags: - - '*' - -jobs: - changelog: - uses: ansible-network/github_actions/.github/workflows/changelog.yml@main - galaxy_importer: - uses: ansible-network/github_actions/.github/workflows/galaxy_importer.yml@main - linters: - uses: ansible-network/github_actions/.github/workflows/tox-linters.yml@main - sanity: - uses: ansible-network/github_actions/.github/workflows/sanity.yml@main - with: - matrix_include: "[]" - matrix_exclude: >- - [ - { - "ansible-version": "stable-2.9" - }, - { - "ansible-version": "stable-2.12", - "python-version": "3.7" - }, - { - "ansible-version": "stable-2.12", - "python-version": "3.11" - }, - { - "ansible-version": "stable-2.13", - "python-version": "3.7" - }, - { - "ansible-version": "stable-2.13", - "python-version": "3.11" - }, - { - "ansible-version": "stable-2.14", - "python-version": "3.7" - }, - { - "ansible-version": "stable-2.14", - "python-version": "3.8" - }, - { - "ansible-version": "stable-2.14", - "python-version": "3.11" - }, - { - "ansible-version": "stable-2.15", - "python-version": "3.7" - }, - { - "ansible-version": "stable-2.15", - "python-version": "3.8" - }, - { - "ansible-version": "stable-2.15", - "python-version": "3.11" - }, - { - "ansible-version": "milestone", - "python-version": "3.7" - }, - { - "ansible-version": "milestone", - "python-version": "3.8" - }, - { - "ansible-version": "devel", - "python-version": "3.7" - }, - { - "ansible-version": "devel", - "python-version": "3.8" - } - ] - unit-source: - uses: ansible-network/github_actions/.github/workflows/unit_source.yml@main - with: - matrix_exclude: >- - [ - { - "python-version": "3.11" - }, - { - "ansible-version": "stable-2.12", - "python-version": "3.7" - }, - { - "ansible-version": "stable-2.13", - "python-version": "3.7" - }, - { - "ansible-version": "stable-2.12", - "python-version": "3.8" - }, - { - "ansible-version": "stable-2.13", - "python-version": "3.8" - }, - { - "ansible-version": "stable-2.14", - "python-version": "3.7" - }, - { - "ansible-version": "stable-2.14", - "python-version": "3.8" - }, - { - "ansible-version": "stable-2.15", - "python-version": "3.7" - }, - { - "ansible-version": "stable-2.15", - "python-version": "3.8" - }, - { - "ansible-version": "milestone", - "python-version": "3.7" - }, - { - "ansible-version": "milestone", - "python-version": "3.8" - }, - { - "ansible-version": "devel", - "python-version": "3.7" - }, - { - "ansible-version": "devel", - "python-version": "3.8" - } - ] - collection_pre_install: '' - splitter: - env: - source_dir: "./source" - runs-on: ubuntu-latest - outputs: - test_targets: ${{ steps.display.outputs.test_targets }} - steps: - - name: Checkout the collection repository - uses: actions/checkout@v3 - with: - path: ${{ env.source_dir }} - fetch-depth: "0" - - - name: list changes for pull request - id: splitter - uses: ansible-network/github_actions/.github/actions/ansible_test_splitter@main - with: - collections_to_test: ${{ env.source_dir }} - total_jobs: 8 - - - name: display targets - id: display - run: echo "test_targets=${{ steps.splitter.outputs.test_targets }}" >> $GITHUB_OUTPUT - shell: bash - - integration: - needs: - - splitter - env: - source: "./source" - cloud_common: "./cloudcommon" - ansible_posix: "./ansible_posix" - test_targets: ${{ needs.splitter.outputs.test_targets }} - runs-on: ubuntu-latest - timeout-minutes: 60 - strategy: - fail-fast: false - matrix: - ansible-version: - - stable-2.12 - - milestone - - devel - python-version: - - "3.8" - - "3.9" - exclude: - - ansible-version: stable-2.9 - python-version: 3.9 - - ansible-version: stable-2.9 - python-version: 3.10 - - ansible-version: stable-2.9 - python-version: 3.11 - - ansible-version: stable-2.12 - python-version: 3.11 - - ansible-version: stable-2.13 - python-version: 3.11 - - ansible-version: stable-2.14 - python-version: 3.8 - - ansible-version: stable-2.15 - python-version: 3.8 - - ansible-version: milestone - python-version: 3.8 - - ansible-version: devel - python-version: 3.8 - enable-turbo-mode: - - true - - false - job-index: [1, 2, 3, 4, 5, 6, 7, 8] - name: "integration-py${{ matrix.python-version }}-${{ matrix.ansible-version }}-turbo-mode=${{ matrix.enable-turbo-mode }}-${{ matrix.job-index }}" - steps: - - name: Read ansible-test targets - id: read-targets - run: >- - echo "ansible_test_targets=$(echo "${{ env.test_targets }}" | sed s/';'/'\n'/g | - grep "kubernetes.core-${{ matrix.job-index }}" | cut -d ':' -f2 | sed s/','/' '/g)" >> $GITHUB_OUTPUT - shell: bash - - - name: Display targets - run: >- - echo "targets to test: $ANSIBLE_TARGETS" - shell: bash - env: - ANSIBLE_TARGETS: ${{ steps.read-targets.outputs.ansible_test_targets }} - - - name: Checkout kubernetes.core repository - uses: actions/checkout@v3 - with: - path: ${{ env.source }} - fetch-depth: "0" - if: steps.read-targets.outputs.ansible_test_targets != '' - - - name: checkout ansible-collections/cloud.common - uses: ansible-network/github_actions/.github/actions/checkout_dependency@main - with: - repository: ansible-collections/cloud.common - path: ${{ env.cloud_common }} - ref: main - if: steps.read-targets.outputs.ansible_test_targets != '' - - - name: checkout ansible-collections/ansible.posix - uses: ansible-network/github_actions/.github/actions/checkout_dependency@main - with: - repository: ansible-collections/ansible.posix - path: ${{ env.ansible_posix }} - ref: main - if: steps.read-targets.outputs.ansible_test_targets != '' - - - name: install kubernetes.core collection - id: install-collection - uses: ansible-network/github_actions/.github/actions/build_install_collection@main - with: - install_python_dependencies: true - source_path: ${{ env.source }} - if: steps.read-targets.outputs.ansible_test_targets != '' - - - name: install cloud.common collection - uses: ansible-network/github_actions/.github/actions/build_install_collection@main - with: - install_python_dependencies: true - source_path: ${{ env.cloud_common }} - if: steps.read-targets.outputs.ansible_test_targets != '' - - - name: install ansible.posix collection - uses: ansible-network/github_actions/.github/actions/build_install_collection@main - with: - install_python_dependencies: true - source_path: ${{ env.ansible_posix }} - if: steps.read-targets.outputs.ansible_test_targets != '' - - - name: create kubernetes cluster - uses: helm/kind-action@v1.4.0 - if: steps.read-targets.outputs.ansible_test_targets != '' - - - name: Run integration tests - uses: ansible-network/github_actions/.github/actions/ansible_test_integration@main - with: - collection_path: ${{ steps.install-collection.outputs.collection_path }} - python_version: ${{ matrix.python-version }} - ansible_version: ${{ matrix.ansible-version }} - ansible_test_targets: ${{ steps.read-targets.outputs.ansible_test_targets }} - ansible_test_environment: | - ENABLE_TURBO_MODE=${{ matrix.enable-turbo-mode }} - if: steps.read-targets.outputs.ansible_test_targets != '' - all_green: - if: ${{ always() }} - needs: - - changelog - - linters - - sanity - - unit-source - - integration - runs-on: ubuntu-latest - steps: - - run: >- - python -c "assert set([ - '${{ needs.changelog.result }}', - '${{ needs.linters.result }}', - '${{ needs.unit-source.result }}', - '${{ needs.integration.result }}' - ]) == {'success'}" - - run: >- - python -c "assert '${{ needs.sanity.result }}' - in ['success', 'failure']" diff --git a/.github/workflows/galaxy-import.yaml b/.github/workflows/galaxy-import.yaml new file mode 100644 index 0000000000..9c21824e31 --- /dev/null +++ b/.github/workflows/galaxy-import.yaml @@ -0,0 +1,14 @@ +name: galaxy-import +concurrency: + group: ${{ github.head_ref }} + cancel-in-progress: true + +on: + pull_request: + branches: + - main + - stable-* + +jobs: + galaxy_importer: + uses: ansible-network/github_actions/.github/workflows/galaxy_importer.yml@main diff --git a/.github/workflows/integration-tests.yaml b/.github/workflows/integration-tests.yaml new file mode 100644 index 0000000000..964f2252e2 --- /dev/null +++ b/.github/workflows/integration-tests.yaml @@ -0,0 +1,141 @@ +name: Integration tests +on: + pull_request: + types: + - opened + - reopened + - labeled + - unlabeled + - synchronize + branches: + - main + - stable-* + +jobs: + splitter: + env: + source_dir: "./source" + runs-on: ubuntu-latest + outputs: + test_targets: ${{ steps.display.outputs.test_targets }} + steps: + - name: Checkout the collection repository + uses: actions/checkout@v3 + with: + path: ${{ env.source_dir }} + fetch-depth: "0" + + - name: list changes for pull request + id: splitter + uses: ansible-network/github_actions/.github/actions/ansible_test_splitter@main + with: + collections_to_test: ${{ env.source_dir }} + total_jobs: 8 + + - name: display targets + id: display + run: echo "test_targets=${{ steps.splitter.outputs.test_targets }}" >> $GITHUB_OUTPUT + shell: bash + + integration: + needs: + - splitter + if: ${{ needs.splitter.outputs.test_targets != '' }} + env: + source: "./source" + cloud_common: "./cloudcommon" + ansible_posix: "./ansible_posix" + test_targets: ${{ needs.splitter.outputs.test_targets }} + runs-on: ubuntu-latest + timeout-minutes: 60 + strategy: + fail-fast: false + matrix: + ansible-version: + - milestone + python-version: + - "3.12" + enable-turbo-mode: + - true + - false + job-index: [1, 2, 3, 4, 5, 6, 7, 8] + name: "integration-py${{ matrix.python-version }}-${{ matrix.ansible-version }}-turbo-mode=${{ matrix.enable-turbo-mode }}-${{ matrix.job-index }}" + steps: + - name: Read ansible-test targets + id: read-targets + run: >- + echo "ansible_test_targets=$(echo "${{ env.test_targets }}" | sed s/';'/'\n'/g | + grep "kubernetes.core-${{ matrix.job-index }}" | cut -d ':' -f2 | sed s/','/' '/g)" >> $GITHUB_OUTPUT + shell: bash + + - name: Display targets + run: >- + echo "targets to test: $ANSIBLE_TARGETS" + shell: bash + env: + ANSIBLE_TARGETS: ${{ steps.read-targets.outputs.ansible_test_targets }} + + - name: Checkout kubernetes.core repository + uses: actions/checkout@v3 + with: + path: ${{ env.source }} + ref: ${{ github.event.pull_request.head.sha }} + + - name: Set up Python ${{ env.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + # install ansible + - name: Install ansible-core (${{ matrix.ansible-version }}) + run: >- + python3 -m pip install + https://github.com/ansible/ansible/archive/${{ matrix.ansible-version }}.tar.gz + --disable-pip-version-check + shell: bash + + - name: Build and install collection + id: install-src + uses: ansible-network/github_actions/.github/actions/build_install_collection@main + with: + install_python_dependencies: true + source_path: ${{ env.source }} + + - name: checkout ansible-collections/cloud.common + uses: ansible-network/github_actions/.github/actions/checkout_dependency@main + with: + repository: ansible-collections/cloud.common + path: ${{ env.cloud_common }} + ref: main + + - name: checkout ansible-collections/ansible.posix + uses: ansible-network/github_actions/.github/actions/checkout_dependency@main + with: + repository: ansible-collections/ansible.posix + path: ${{ env.ansible_posix }} + ref: main + + - name: install cloud.common collection + uses: ansible-network/github_actions/.github/actions/build_install_collection@main + with: + install_python_dependencies: true + source_path: ${{ env.cloud_common }} + + - name: install ansible.posix collection + uses: ansible-network/github_actions/.github/actions/build_install_collection@main + with: + install_python_dependencies: true + source_path: ${{ env.ansible_posix }} + + - name: create kubernetes cluster + uses: helm/kind-action@v1.4.0 + + - name: Run integration tests + uses: ansible-network/github_actions/.github/actions/ansible_test_integration@main + with: + collection_path: ${{ steps.install-src.outputs.collection_path }} + python_version: ${{ matrix.python-version }} + ansible_version: ${{ matrix.ansible-version }} + ansible_test_targets: ${{ steps.read-targets.outputs.ansible_test_targets }} + ansible_test_environment: | + ENABLE_TURBO_MODE=${{ matrix.enable-turbo-mode }} diff --git a/.github/workflows/linters.yaml b/.github/workflows/linters.yaml new file mode 100644 index 0000000000..9dd7f0e006 --- /dev/null +++ b/.github/workflows/linters.yaml @@ -0,0 +1,24 @@ +--- +name: Linters +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: + pull_request: + branches: + - main + - stable-* + tags: + - '*' + +jobs: + linters: + uses: ansible-network/github_actions/.github/workflows/tox-linters.yml@main + ansible-lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Run ansible-lint + uses: ansible/ansible-lint@v6.21.0 diff --git a/.github/workflows/sanity-tests.yaml b/.github/workflows/sanity-tests.yaml new file mode 100644 index 0000000000..044f6def12 --- /dev/null +++ b/.github/workflows/sanity-tests.yaml @@ -0,0 +1,15 @@ +--- +name: Sanity tests +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: + pull_request: + branches: + - main + - stable-* + +jobs: + sanity: + uses: ansible-network/github_actions/.github/workflows/sanity.yml@main diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml new file mode 100644 index 0000000000..19c8519b24 --- /dev/null +++ b/.github/workflows/unit-tests.yaml @@ -0,0 +1,14 @@ +name: Unit tests +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: + pull_request: + branches: + - main + - stable-* + +jobs: + unit-source: + uses: ansible-network/github_actions/.github/workflows/unit_source.yml@main diff --git a/README.md b/README.md index 89f520795f..490ee6dd4a 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ The collection includes a variety of Ansible content to help automate the manage ## Ansible version compatibility -This collection has been tested against following Ansible versions: **>=2.9.17**. +This collection has been tested against following Ansible versions: **>=2.14.0**. For collections that support Ansible 2.9, please ensure you update your `network_os` to use the fully qualified collection name (for example, `cisco.ios.ios`). diff --git a/changelogs/fragments/remove_ansible_2_13.yaml b/changelogs/fragments/remove_ansible_2_13.yaml new file mode 100644 index 0000000000..f2dc98bfa6 --- /dev/null +++ b/changelogs/fragments/remove_ansible_2_13.yaml @@ -0,0 +1,5 @@ +--- +trivial: + - organize the workflows and pin ansible-lint to 6.21.x. +breaking_changes: + - Remove support for ansible-core < 2.14 diff --git a/docs/kubernetes.core.kubectl_connection.rst b/docs/kubernetes.core.kubectl_connection.rst index 595ee40596..4acce17cb5 100644 --- a/docs/kubernetes.core.kubectl_connection.rst +++ b/docs/kubernetes.core.kubectl_connection.rst @@ -8,6 +8,7 @@ kubernetes.core.kubectl **Execute tasks in pods running on Kubernetes.** +Version added: 1.2.1 .. contents:: :local: diff --git a/galaxy.yml b/galaxy.yml index 3da7e4a773..dc52ea7883 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -9,8 +9,8 @@ authors: - mmazur (https://github.com/mmazur) - jamescassell (https://github.com/jamescassell) description: Kubernetes Collection for Ansible. -documentation: '' -homepage: '' +documentation: "" +homepage: "" issues: https://github.com/ansible-collections/kubernetes.core/issues license_file: LICENSE namespace: kubernetes @@ -28,4 +28,4 @@ tags: version: 2.4.0 build_ignore: - .DS_Store - - '*.tar.gz' + - "*.tar.gz" diff --git a/meta/runtime.yml b/meta/runtime.yml index 1eadc3f8dc..99134a2c37 100644 --- a/meta/runtime.yml +++ b/meta/runtime.yml @@ -1,5 +1,5 @@ --- -requires_ansible: '>=2.9.17' +requires_ansible: '>=2.14.0' action_groups: helm: diff --git a/plugins/action/k8s_info.py b/plugins/action/k8s_info.py index 181daca4e6..650e0beaf5 100644 --- a/plugins/action/k8s_info.py +++ b/plugins/action/k8s_info.py @@ -8,21 +8,21 @@ __metaclass__ = type import copy -import traceback import os -from contextlib import contextmanager import platform +import traceback +from contextlib import contextmanager from ansible.config.manager import ensure_type from ansible.errors import ( - AnsibleError, - AnsibleFileNotFound, AnsibleAction, AnsibleActionFail, + AnsibleError, + AnsibleFileNotFound, ) +from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.module_utils.parsing.convert_bool import boolean -from ansible.module_utils.six import string_types, iteritems -from ansible.module_utils._text import to_text, to_bytes, to_native +from ansible.module_utils.six import iteritems, string_types from ansible.plugins.action import ActionBase @@ -55,7 +55,6 @@ def output(self): class ActionModule(ActionBase): - TRANSFERS_FILES = True DEFAULT_NEWLINE_SEQUENCE = "\n" diff --git a/plugins/connection/kubectl.py b/plugins/connection/kubectl.py index d0c3baa8b4..8aff4efed7 100644 --- a/plugins/connection/kubectl.py +++ b/plugins/connection/kubectl.py @@ -172,18 +172,18 @@ aliases: [ kubectl_verify_ssl ] """ +import json import os import os.path import shutil import subprocess import tempfile -import json -from ansible.parsing.yaml.loader import AnsibleLoader from ansible.errors import AnsibleError, AnsibleFileNotFound -from ansible.module_utils.six.moves import shlex_quote from ansible.module_utils._text import to_bytes -from ansible.plugins.connection import ConnectionBase, BUFSIZE +from ansible.module_utils.six.moves import shlex_quote +from ansible.parsing.yaml.loader import AnsibleLoader +from ansible.plugins.connection import BUFSIZE, ConnectionBase from ansible.utils.display import Display display = Display() diff --git a/plugins/doc_fragments/helm_common_options.py b/plugins/doc_fragments/helm_common_options.py index dde91db12c..260ed94a63 100644 --- a/plugins/doc_fragments/helm_common_options.py +++ b/plugins/doc_fragments/helm_common_options.py @@ -12,7 +12,6 @@ class ModuleDocFragment(object): - DOCUMENTATION = r""" options: binary_path: diff --git a/plugins/doc_fragments/k8s_auth_options.py b/plugins/doc_fragments/k8s_auth_options.py index 516ef64fda..c2ef9a0655 100644 --- a/plugins/doc_fragments/k8s_auth_options.py +++ b/plugins/doc_fragments/k8s_auth_options.py @@ -11,7 +11,6 @@ class ModuleDocFragment(object): - DOCUMENTATION = r""" options: host: diff --git a/plugins/doc_fragments/k8s_delete_options.py b/plugins/doc_fragments/k8s_delete_options.py index a8f20cf9cf..5b3581f8b6 100644 --- a/plugins/doc_fragments/k8s_delete_options.py +++ b/plugins/doc_fragments/k8s_delete_options.py @@ -11,7 +11,6 @@ class ModuleDocFragment(object): - DOCUMENTATION = r""" options: delete_options: diff --git a/plugins/doc_fragments/k8s_name_options.py b/plugins/doc_fragments/k8s_name_options.py index e14658b035..07f7c2935f 100644 --- a/plugins/doc_fragments/k8s_name_options.py +++ b/plugins/doc_fragments/k8s_name_options.py @@ -11,7 +11,6 @@ class ModuleDocFragment(object): - DOCUMENTATION = r""" options: api_version: diff --git a/plugins/doc_fragments/k8s_resource_options.py b/plugins/doc_fragments/k8s_resource_options.py index 6920efa45c..0aa275af9c 100644 --- a/plugins/doc_fragments/k8s_resource_options.py +++ b/plugins/doc_fragments/k8s_resource_options.py @@ -11,7 +11,6 @@ class ModuleDocFragment(object): - DOCUMENTATION = r""" options: resource_definition: diff --git a/plugins/doc_fragments/k8s_scale_options.py b/plugins/doc_fragments/k8s_scale_options.py index ca0605fdf3..87fa17d310 100644 --- a/plugins/doc_fragments/k8s_scale_options.py +++ b/plugins/doc_fragments/k8s_scale_options.py @@ -11,7 +11,6 @@ class ModuleDocFragment(object): - DOCUMENTATION = r""" options: replicas: diff --git a/plugins/doc_fragments/k8s_state_options.py b/plugins/doc_fragments/k8s_state_options.py index 0333186676..e01a9aaff5 100644 --- a/plugins/doc_fragments/k8s_state_options.py +++ b/plugins/doc_fragments/k8s_state_options.py @@ -11,7 +11,6 @@ class ModuleDocFragment(object): - DOCUMENTATION = r""" options: state: diff --git a/plugins/doc_fragments/k8s_wait_options.py b/plugins/doc_fragments/k8s_wait_options.py index e498e3ac6f..acf8f8aea3 100644 --- a/plugins/doc_fragments/k8s_wait_options.py +++ b/plugins/doc_fragments/k8s_wait_options.py @@ -11,7 +11,6 @@ class ModuleDocFragment(object): - DOCUMENTATION = r""" options: wait: diff --git a/plugins/inventory/k8s.py b/plugins/inventory/k8s.py index 45a36ec840..277b464aa8 100644 --- a/plugins/inventory/k8s.py +++ b/plugins/inventory/k8s.py @@ -98,32 +98,33 @@ - "PyYAML >= 3.11" """ -EXAMPLES = """ +EXAMPLES = r""" # File must be named k8s.yaml or k8s.yml -# Authenticate with token, and return all pods and services for all namespaces -plugin: kubernetes.core.k8s -connections: - - host: https://192.168.64.4:8443 - api_key: xxxxxxxxxxxxxxxx - validate_certs: false - -# Use default config (~/.kube/config) file and active context, and return objects for a specific namespace -plugin: kubernetes.core.k8s -connections: - - namespaces: - - testing - -# Use a custom config file, and a specific context. -plugin: kubernetes.core.k8s -connections: - - kubeconfig: /path/to/config - context: 'awx/192-168-64-4:8443/developer' +- name: Authenticate with token, and return all pods and services for all namespaces + plugin: kubernetes.core.k8s + connections: + - host: https://192.168.64.4:8443 + api_key: xxxxxxxxxxxxxxxx + validate_certs: false + +- name: Use default config (~/.kube/config) file and active context, and return objects for a specific namespace + plugin: kubernetes.core.k8s + connections: + - namespaces: + - testing + +- name: Use a custom config file, and a specific context. + plugin: kubernetes.core.k8s + connections: + - kubeconfig: /path/to/config + context: 'awx/192-168-64-4:8443/developer' """ import json from ansible.errors import AnsibleError +from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable, Constructable from ansible_collections.kubernetes.core.plugins.module_utils.common import ( HAS_K8S_MODULE_HELPER, k8s_import_exception, @@ -131,7 +132,6 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( get_api_client, ) -from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable try: from kubernetes.dynamic.exceptions import DynamicApiError @@ -193,7 +193,6 @@ def setup(self, config_data, cache, cache_key): self.fetch_objects(connections) def fetch_objects(self, connections): - if connections: if not isinstance(connections, list): raise K8sInventoryException("Expecting connections to be a list.") diff --git a/plugins/lookup/k8s.py b/plugins/lookup/k8s.py index bd69a99238..3211fbb74b 100644 --- a/plugins/lookup/k8s.py +++ b/plugins/lookup/k8s.py @@ -179,7 +179,6 @@ from ansible.errors import AnsibleError from ansible.module_utils.common._collections_compat import KeysView from ansible.module_utils.common.validation import check_type_bool - from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( get_api_client, ) @@ -214,7 +213,6 @@ class KubernetesLookup(object): def __init__(self): - if not HAS_K8S_MODULE_HELPER: raise Exception( "Requires the Kubernetes Python client. Try `pip install kubernetes`. Detail: {0}".format( diff --git a/plugins/lookup/kustomize.py b/plugins/lookup/kustomize.py index ef7e50f067..dd580a2ff4 100644 --- a/plugins/lookup/kustomize.py +++ b/plugins/lookup/kustomize.py @@ -64,12 +64,11 @@ key1: val1 """ +import subprocess + from ansible.errors import AnsibleLookupError -from ansible.plugins.lookup import LookupBase from ansible.module_utils.common.process import get_bin_path - - -import subprocess +from ansible.plugins.lookup import LookupBase def get_binary_from_path(name, opt_dirs=None): diff --git a/plugins/module_utils/ansiblemodule.py b/plugins/module_utils/ansiblemodule.py index 8b17866dcc..d3eba8df74 100644 --- a/plugins/module_utils/ansiblemodule.py +++ b/plugins/module_utils/ansiblemodule.py @@ -14,9 +14,9 @@ if enable_turbo_mode: try: - from ansible_collections.cloud.common.plugins.module_utils.turbo.module import ( + from ansible_collections.cloud.common.plugins.module_utils.turbo.module import ( # noqa: F401 AnsibleTurboModule as AnsibleModule, - ) # noqa: F401 + ) AnsibleModule.collection_name = "kubernetes.core" except ImportError: diff --git a/plugins/module_utils/apply.py b/plugins/module_utils/apply.py index dea185ef3e..18b153a3be 100644 --- a/plugins/module_utils/apply.py +++ b/plugins/module_utils/apply.py @@ -17,8 +17,8 @@ __metaclass__ = type -from collections import OrderedDict import json +from collections import OrderedDict from ansible.module_utils.common.dict_transformations import dict_merge from ansible_collections.kubernetes.core.plugins.module_utils.exceptions import ( @@ -31,7 +31,6 @@ LooseVersion, ) - try: from kubernetes.dynamic.exceptions import NotFoundError except ImportError: diff --git a/plugins/module_utils/client/discovery.py b/plugins/module_utils/client/discovery.py index 898a82cef3..848d4bdbcd 100644 --- a/plugins/module_utils/client/discovery.py +++ b/plugins/module_utils/client/discovery.py @@ -13,15 +13,18 @@ # limitations under the License. +import hashlib import json import os -from collections import defaultdict -import hashlib import tempfile +from collections import defaultdict from functools import partial import kubernetes.dynamic import kubernetes.dynamic.discovery +from ansible_collections.kubernetes.core.plugins.module_utils.client.resource import ( + ResourceList, +) from kubernetes import __version__ from kubernetes.dynamic.exceptions import ( ResourceNotFoundError, @@ -29,10 +32,6 @@ ServiceUnavailableError, ) -from ansible_collections.kubernetes.core.plugins.module_utils.client.resource import ( - ResourceList, -) - class Discoverer(kubernetes.dynamic.discovery.Discoverer): def __init__(self, client, cache_file): diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index a46c813fe2..e297197276 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -20,17 +20,20 @@ __metaclass__ = type import base64 -import time +import hashlib import os -import traceback import sys -import hashlib +import time +import traceback from datetime import datetime from tempfile import NamedTemporaryFile -from ansible_collections.kubernetes.core.plugins.module_utils.version import ( - LooseVersion, -) +from ansible.module_utils._text import to_bytes, to_native, to_text +from ansible.module_utils.basic import missing_required_lib +from ansible.module_utils.common.dict_transformations import dict_merge +from ansible.module_utils.parsing.convert_bool import boolean +from ansible.module_utils.six import iteritems, string_types +from ansible.module_utils.urls import Request from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( AUTH_ARG_MAP, AUTH_ARG_SPEC, @@ -42,27 +45,23 @@ from ansible_collections.kubernetes.core.plugins.module_utils.selector import ( LabelSelectorFilter, ) - -from ansible.module_utils.basic import 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.module_utils.urls import Request +from ansible_collections.kubernetes.core.plugins.module_utils.version import ( + LooseVersion, +) K8S_IMP_ERR = None try: import kubernetes from kubernetes.dynamic.exceptions import ( - NotFoundError, - ResourceNotFoundError, - ResourceNotUniqueError, - DynamicApiError, + BadRequestError, ConflictError, + DynamicApiError, ForbiddenError, - MethodNotAllowedError, - BadRequestError, KubernetesValidateMissing, + MethodNotAllowedError, + NotFoundError, + ResourceNotFoundError, + ResourceNotUniqueError, ) HAS_K8S_MODULE_HELPER = True @@ -819,7 +818,7 @@ def execute_module(self): try: self.client = get_api_client(self.module) # Hopefully the kubernetes client will provide its own exception class one day - except (urllib3.exceptions.RequestError) as e: + except urllib3.exceptions.RequestError as e: self.fail_json(msg="Couldn't connect to Kubernetes: %s" % str(e)) flattened_definitions = [] @@ -837,7 +836,7 @@ def execute_module(self): resource = self.find_resource(kind, api_version, fail=True) flattened_definitions.append((resource, definition)) - for (resource, definition) in flattened_definitions: + for resource, definition in flattened_definitions: kind = definition.get("kind", self.kind) api_version = definition.get("apiVersion", self.api_version) definition = self.set_defaults(resource, definition) diff --git a/plugins/module_utils/copy.py b/plugins/module_utils/copy.py index c7e1b4e21a..c70eed1a4c 100644 --- a/plugins/module_utils/copy.py +++ b/plugins/module_utils/copy.py @@ -18,25 +18,26 @@ __metaclass__ = type import os -from tempfile import TemporaryFile, NamedTemporaryFile -from select import select -from abc import ABCMeta, abstractmethod import tarfile +from abc import ABCMeta, abstractmethod +from select import select +from tempfile import NamedTemporaryFile, TemporaryFile + +from ansible.module_utils._text import to_native # from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( CoreException, ) -from ansible.module_utils._text import to_native try: from kubernetes.client.api import core_v1_api from kubernetes.stream import stream from kubernetes.stream.ws_client import ( - STDOUT_CHANNEL, - STDERR_CHANNEL, - ERROR_CHANNEL, ABNF, + ERROR_CHANNEL, + STDERR_CHANNEL, + STDOUT_CHANNEL, ) except ImportError: pass diff --git a/plugins/module_utils/hashes.py b/plugins/module_utils/hashes.py index 3d44a7d906..5e0e70559b 100644 --- a/plugins/module_utils/hashes.py +++ b/plugins/module_utils/hashes.py @@ -19,8 +19,8 @@ __metaclass__ = type -import json import hashlib +import json try: import string @@ -37,7 +37,7 @@ def sorted_dict(unsorted_dict): result = OrderedDict() - for (k, v) in sorted(unsorted_dict.items()): + for k, v in sorted(unsorted_dict.items()): if isinstance(v, dict): v = sorted_dict(v) result[k] = v diff --git a/plugins/module_utils/helm.py b/plugins/module_utils/helm.py index a7a2fa7c01..8bd06eccde 100644 --- a/plugins/module_utils/helm.py +++ b/plugins/module_utils/helm.py @@ -7,19 +7,18 @@ __metaclass__ = type +import copy +import json import os +import re import tempfile import traceback -import re -import json -import copy -from ansible.module_utils.basic import missing_required_lib +from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.six import string_types from ansible_collections.kubernetes.core.plugins.module_utils.version import ( LooseVersion, ) -from ansible.module_utils.basic import AnsibleModule try: import yaml @@ -84,7 +83,6 @@ class AnsibleHelmModule(object): """ def __init__(self, **kwargs): - self._module = None if "module" in kwargs: self._module = kwargs.get("module") @@ -184,7 +182,6 @@ def get_helm_binary(self): ) def get_helm_version(self): - command = self.get_helm_binary() + " version" rc, out, err = self.run_command(command) m = re.match(r'version.BuildInfo{Version:"v([0-9\.]*)",', out) @@ -216,7 +213,6 @@ def get_values(self, release_name, get_all=False): return yaml.safe_load(out) def parse_yaml_content(self, content): - if not HAS_YAML: self.fail_json(msg=missing_required_lib("yaml"), exception=HAS_YAML) @@ -228,7 +224,6 @@ def parse_yaml_content(self, content): ) def get_manifest(self, release_name): - command = [ self.get_helm_binary(), "get", @@ -241,7 +236,6 @@ def get_manifest(self, release_name): return self.parse_yaml_content(out) def get_notes(self, release_name): - command = [ self.get_helm_binary(), "get", diff --git a/plugins/module_utils/k8s/client.py b/plugins/module_utils/k8s/client.py index 2589e56073..a0d8dcfa6b 100644 --- a/plugins/module_utils/k8s/client.py +++ b/plugins/module_utils/k8s/client.py @@ -1,12 +1,11 @@ # Copyright: (c) 2021, Red Hat | Ansible # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -import os import hashlib +import os from typing import Any, Dict, List, Optional from ansible.module_utils.six import iteritems, string_types - from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( AUTH_ARG_MAP, AUTH_ARG_SPEC, diff --git a/plugins/module_utils/k8s/core.py b/plugins/module_utils/k8s/core.py index 131e80e29e..1ecc182ece 100644 --- a/plugins/module_utils/k8s/core.py +++ b/plugins/module_utils/k8s/core.py @@ -1,15 +1,12 @@ import traceback - from typing import Optional +from ansible.module_utils.basic import AnsibleModule, missing_required_lib +from ansible.module_utils.common.text.converters import to_text from ansible_collections.kubernetes.core.plugins.module_utils.version import ( LooseVersion, ) -from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.basic import missing_required_lib -from ansible.module_utils.common.text.converters import to_text - class AnsibleK8SModule: """A base module class for K8S modules. diff --git a/plugins/module_utils/k8s/resource.py b/plugins/module_utils/k8s/resource.py index 4c9d3e1d12..314f7a38ac 100644 --- a/plugins/module_utils/k8s/resource.py +++ b/plugins/module_utils/k8s/resource.py @@ -2,7 +2,7 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) import os -from typing import cast, Dict, Iterable, List, Optional, Union +from typing import Dict, Iterable, List, Optional, Union, cast from ansible.module_utils.six import string_types from ansible.module_utils.urls import Request diff --git a/plugins/module_utils/k8s/runner.py b/plugins/module_utils/k8s/runner.py index 60904b4228..61ad1fb6ac 100644 --- a/plugins/module_utils/k8s/runner.py +++ b/plugins/module_utils/k8s/runner.py @@ -4,12 +4,12 @@ from typing import Dict from ansible.module_utils._text import to_native - from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( get_api_client, ) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( CoreException, + ResourceTimeout, ) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.resource import ( create_definitions, @@ -19,9 +19,6 @@ diff_objects, hide_fields, ) -from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( - ResourceTimeout, -) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.waiter import exists from ansible_collections.kubernetes.core.plugins.module_utils.selector import ( LabelSelectorFilter, diff --git a/plugins/module_utils/k8s/service.py b/plugins/module_utils/k8s/service.py index 95cf2136c9..dc91bc0c6c 100644 --- a/plugins/module_utils/k8s/service.py +++ b/plugins/module_utils/k8s/service.py @@ -4,36 +4,30 @@ import copy from typing import Any, Dict, List, Optional, Tuple +from ansible.module_utils.common.dict_transformations import dict_merge from ansible_collections.kubernetes.core.plugins.module_utils.hashes import ( generate_hash, ) - +from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import requires +from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( + CoreException, +) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.waiter import ( Waiter, exists, - resource_absent, get_waiter, + resource_absent, ) -from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( - requires, -) - -from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( - CoreException, -) - -from ansible.module_utils.common.dict_transformations import dict_merge - try: from kubernetes.dynamic.exceptions import ( - NotFoundError, - ResourceNotFoundError, - ResourceNotUniqueError, + BadRequestError, ConflictError, ForbiddenError, MethodNotAllowedError, - BadRequestError, + NotFoundError, + ResourceNotFoundError, + ResourceNotUniqueError, ) except ImportError: # Handled in module setup diff --git a/plugins/module_utils/k8s/waiter.py b/plugins/module_utils/k8s/waiter.py index 55afd80174..5328d63180 100644 --- a/plugins/module_utils/k8s/waiter.py +++ b/plugins/module_utils/k8s/waiter.py @@ -3,7 +3,6 @@ from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple, Union from ansible.module_utils.parsing.convert_bool import boolean - from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( CoreException, ) diff --git a/plugins/module_utils/k8sdynamicclient.py b/plugins/module_utils/k8sdynamicclient.py index b1beca4cd7..7a6e8e66a5 100644 --- a/plugins/module_utils/k8sdynamicclient.py +++ b/plugins/module_utils/k8sdynamicclient.py @@ -18,12 +18,11 @@ __metaclass__ = type -from kubernetes.dynamic import DynamicClient - from ansible_collections.kubernetes.core.plugins.module_utils.apply import k8s_apply from ansible_collections.kubernetes.core.plugins.module_utils.exceptions import ( ApplyException, ) +from kubernetes.dynamic import DynamicClient class K8SDynamicClient(DynamicClient): diff --git a/plugins/module_utils/selector.py b/plugins/module_utils/selector.py index 2a85d0bfde..08178602aa 100644 --- a/plugins/module_utils/selector.py +++ b/plugins/module_utils/selector.py @@ -16,7 +16,6 @@ class Selector(object): - equality_based_operators = ("==", "!=", "=") def __init__(self, data): diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index 87d5177be9..dc756e728d 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -393,10 +393,11 @@ sample: helm upgrade ... """ +import copy import re import tempfile import traceback -import copy + from ansible_collections.kubernetes.core.plugins.module_utils.version import ( LooseVersion, ) @@ -877,7 +878,6 @@ def main(): changed = True else: - helm_diff_version = get_plugin_version("diff") if helm_diff_version and ( not chart_repo_url diff --git a/plugins/modules/helm_info.py b/plugins/modules/helm_info.py index 5a7a561662..4f4a5e0451 100644 --- a/plugins/modules/helm_info.py +++ b/plugins/modules/helm_info.py @@ -137,8 +137,8 @@ version_added: "2.4.0" """ -import traceback import copy +import traceback try: import yaml diff --git a/plugins/modules/helm_plugin.py b/plugins/modules/helm_plugin.py index 795dbf29ec..e91d122e06 100644 --- a/plugins/modules/helm_plugin.py +++ b/plugins/modules/helm_plugin.py @@ -109,6 +109,7 @@ """ import copy + from ansible_collections.kubernetes.core.plugins.module_utils.helm import ( AnsibleHelmModule, parse_helm_plugin_list, diff --git a/plugins/modules/helm_plugin_info.py b/plugins/modules/helm_plugin_info.py index 3b9fcd1898..e39c61ed0c 100644 --- a/plugins/modules/helm_plugin_info.py +++ b/plugins/modules/helm_plugin_info.py @@ -71,9 +71,10 @@ """ import copy + from ansible_collections.kubernetes.core.plugins.module_utils.helm import ( - parse_helm_plugin_list, AnsibleHelmModule, + parse_helm_plugin_list, ) from ansible_collections.kubernetes.core.plugins.module_utils.helm_args_common import ( HELM_AUTH_ARG_SPEC, @@ -82,7 +83,6 @@ def main(): - argument_spec = copy.deepcopy(HELM_AUTH_ARG_SPEC) argument_spec.update( dict( diff --git a/plugins/modules/helm_repository.py b/plugins/modules/helm_repository.py index 34213addc2..b7f9815511 100644 --- a/plugins/modules/helm_repository.py +++ b/plugins/modules/helm_repository.py @@ -166,8 +166,8 @@ sample: 'Repository already have a repository named bitnami' """ -import traceback import copy +import traceback try: import yaml diff --git a/plugins/modules/k8s.py b/plugins/modules/k8s.py index baa28c0429..fb8e68003e 100644 --- a/plugins/modules/k8s.py +++ b/plugins/modules/k8s.py @@ -278,10 +278,10 @@ kubernetes.core.k8s: state: present template: - - path: '/testing/deployment_one.j2' - - path: '/testing/deployment_two.j2' - variable_start_string: '[[' - variable_end_string: ']]' + - path: '/testing/deployment_one.j2' + - path: '/testing/deployment_two.j2' + variable_start_string: '[[' + variable_end_string: ']]' - name: fail on validation errors kubernetes.core.k8s: @@ -423,10 +423,10 @@ ) from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( AUTH_ARG_SPEC, - WAIT_ARG_SPEC, + DELETE_OPTS_ARG_SPEC, NAME_ARG_SPEC, RESOURCE_ARG_SPEC, - DELETE_OPTS_ARG_SPEC, + WAIT_ARG_SPEC, ) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( AnsibleK8SModule, diff --git a/plugins/modules/k8s_cluster_info.py b/plugins/modules/k8s_cluster_info.py index 9cd2ac17be..916c30599e 100644 --- a/plugins/modules/k8s_cluster_info.py +++ b/plugins/modules/k8s_cluster_info.py @@ -154,15 +154,15 @@ from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( AnsibleModule, ) +from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( + AUTH_ARG_SPEC, +) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( AnsibleK8SModule, ) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( CoreException, ) -from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( - AUTH_ARG_SPEC, -) def execute_module(module, client): diff --git a/plugins/modules/k8s_cp.py b/plugins/modules/k8s_cp.py index e8f1dea72d..185c8e064c 100644 --- a/plugins/modules/k8s_cp.py +++ b/plugins/modules/k8s_cp.py @@ -139,9 +139,18 @@ import copy +from ansible.module_utils._text import to_native from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( AnsibleModule, ) +from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( + AUTH_ARG_SPEC, +) +from ansible_collections.kubernetes.core.plugins.module_utils.copy import ( + K8SCopyFromPod, + K8SCopyToPod, + check_pod, +) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( get_api_client, ) @@ -155,16 +164,6 @@ K8sService, ) -from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( - AUTH_ARG_SPEC, -) -from ansible_collections.kubernetes.core.plugins.module_utils.copy import ( - K8SCopyFromPod, - K8SCopyToPod, - check_pod, -) -from ansible.module_utils._text import to_native - def argspec(): argument_spec = copy.deepcopy(AUTH_ARG_SPEC) diff --git a/plugins/modules/k8s_drain.py b/plugins/modules/k8s_drain.py index a8c688e146..6b6e96482d 100644 --- a/plugins/modules/k8s_drain.py +++ b/plugins/modules/k8s_drain.py @@ -113,7 +113,7 @@ state: drain name: foo delete_options: - terminate_grace_period: 900 + terminate_grace_period: 900 - name: Mark node "foo" as schedulable. kubernetes.core.k8s_drain: @@ -132,7 +132,6 @@ pod_selectors: - 'app!=csi-attacher' - 'app!=csi-provisioner' - """ RETURN = r""" @@ -146,8 +145,9 @@ import copy import time import traceback - from datetime import datetime + +from ansible.module_utils._text import to_native from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( AnsibleModule, ) @@ -164,12 +164,10 @@ CoreException, ) -from ansible.module_utils._text import to_native - try: from kubernetes.client.api import core_v1_api - from kubernetes.client.models import V1DeleteOptions, V1ObjectMeta from kubernetes.client.exceptions import ApiException + from kubernetes.client.models import V1DeleteOptions, V1ObjectMeta except ImportError: # ImportError are managed by the common module already. pass @@ -430,7 +428,6 @@ def _revert_node_patch(): return dict(result=" ".join(result)) def patch_node(self, unschedulable): - body = {"spec": {"unschedulable": unschedulable}} try: self._api_instance.patch_node( @@ -442,7 +439,6 @@ def patch_node(self, unschedulable): ) def execute_module(self): - state = self._module.params.get("state") name = self._module.params.get("name") try: diff --git a/plugins/modules/k8s_exec.py b/plugins/modules/k8s_exec.py index c54c23c06f..17affe811a 100644 --- a/plugins/modules/k8s_exec.py +++ b/plugins/modules/k8s_exec.py @@ -131,27 +131,27 @@ # ImportError are managed by the common module already. pass +from ansible.module_utils._text import to_native from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( AnsibleModule, ) -from ansible.module_utils._text import to_native from ansible_collections.kubernetes.core.plugins.module_utils.common import ( AUTH_ARG_SPEC, ) -from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( - AnsibleK8SModule, -) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( get_api_client, ) +from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( + AnsibleK8SModule, +) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( CoreException, ) try: from kubernetes.client.apis import core_v1_api - from kubernetes.stream import stream from kubernetes.client.exceptions import ApiException + from kubernetes.stream import stream except ImportError: # ImportError are managed by the common module already. pass diff --git a/plugins/modules/k8s_info.py b/plugins/modules/k8s_info.py index 7f144243c2..e750d4cc57 100644 --- a/plugins/modules/k8s_info.py +++ b/plugins/modules/k8s_info.py @@ -165,12 +165,12 @@ AUTH_ARG_SPEC, WAIT_ARG_SPEC, ) -from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( - AnsibleK8SModule, -) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( get_api_client, ) +from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( + AnsibleK8SModule, +) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( CoreException, ) diff --git a/plugins/modules/k8s_json_patch.py b/plugins/modules/k8s_json_patch.py index 5ea8dbc956..efa62f484f 100644 --- a/plugins/modules/k8s_json_patch.py +++ b/plugins/modules/k8s_json_patch.py @@ -127,8 +127,8 @@ import copy import traceback -from ansible.module_utils.basic import missing_required_lib from ansible.module_utils._text import to_native +from ansible.module_utils.basic import missing_required_lib from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( AnsibleModule, ) @@ -152,7 +152,6 @@ get_waiter, ) - try: from kubernetes.dynamic.exceptions import DynamicApiError except ImportError: diff --git a/plugins/modules/k8s_scale.py b/plugins/modules/k8s_scale.py index 20d692900f..cccd35271e 100644 --- a/plugins/modules/k8s_scale.py +++ b/plugins/modules/k8s_scale.py @@ -6,7 +6,6 @@ from __future__ import absolute_import, division, print_function - __metaclass__ = type @@ -151,14 +150,13 @@ pass from ansible.module_utils._text import to_native - from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( AnsibleModule, ) from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( AUTH_ARG_SPEC, - RESOURCE_ARG_SPEC, NAME_ARG_SPEC, + RESOURCE_ARG_SPEC, ) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( get_api_client, @@ -170,15 +168,15 @@ CoreException, ResourceTimeout, ) +from ansible_collections.kubernetes.core.plugins.module_utils.k8s.resource import ( + create_definitions, +) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import ( diff_objects, ) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.waiter import ( get_waiter, ) -from ansible_collections.kubernetes.core.plugins.module_utils.k8s.resource import ( - create_definitions, -) SCALE_ARG_SPEC = { "replicas": {"type": "int", "required": True}, diff --git a/plugins/modules/k8s_service.py b/plugins/modules/k8s_service.py index 1eed29bd3c..745520d96a 100644 --- a/plugins/modules/k8s_service.py +++ b/plugins/modules/k8s_service.py @@ -143,7 +143,6 @@ """ import copy - from collections import defaultdict from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( @@ -154,25 +153,24 @@ COMMON_ARG_SPEC, RESOURCE_ARG_SPEC, ) -from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( - AnsibleK8SModule, -) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( get_api_client, ) +from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( + AnsibleK8SModule, +) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( CoreException, ) -from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import ( - K8sService, -) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.resource import ( create_definitions, ) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.runner import ( perform_action, ) - +from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import ( + K8sService, +) SERVICE_ARG_SPEC = { "apply": {"type": "bool", "default": False}, diff --git a/plugins/modules/k8s_taint.py b/plugins/modules/k8s_taint.py index bfa80db5ec..5f0355ca55 100644 --- a/plugins/modules/k8s_taint.py +++ b/plugins/modules/k8s_taint.py @@ -127,7 +127,6 @@ import copy from ansible.module_utils._text import to_native - from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( AnsibleModule, ) diff --git a/tests/integration/targets/helm/library/helm_test_pending.py b/tests/integration/targets/helm/library/helm_test_pending.py index c0f3935f34..cc8bde7f97 100644 --- a/tests/integration/targets/helm/library/helm_test_pending.py +++ b/tests/integration/targets/helm/library/helm_test_pending.py @@ -48,9 +48,10 @@ RETURN = r""" """ -import subprocess import json +import subprocess import time + from ansible.module_utils.basic import AnsibleModule diff --git a/tests/integration/targets/helm/library/helm_test_version.py b/tests/integration/targets/helm/library/helm_test_version.py index dfd9a08605..1ac12e0f65 100644 --- a/tests/integration/targets/helm/library/helm_test_version.py +++ b/tests/integration/targets/helm/library/helm_test_version.py @@ -52,12 +52,12 @@ """ import re + +from ansible.module_utils.basic import AnsibleModule from ansible_collections.kubernetes.core.plugins.module_utils.version import ( LooseVersion, ) -from ansible.module_utils.basic import AnsibleModule - def main(): module = AnsibleModule( diff --git a/tests/integration/targets/k8s_copy/library/k8s_create_file.py b/tests/integration/targets/k8s_copy/library/k8s_create_file.py deleted file mode 100644 index 6898c36a56..0000000000 --- a/tests/integration/targets/k8s_copy/library/k8s_create_file.py +++ /dev/null @@ -1,93 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- -# Copyright: (c) 2021, Aubin Bikouo <@abikouo> -# 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 - - -DOCUMENTATION = r""" - -module: k8s_create_file - -short_description: Create large file with a defined size. - -author: - - Aubin Bikouo (@abikouo) - -description: - - This module is used to validate k8s_cp module. - -options: - path: - description: - - The destination path for the file to create. - type: path - required: yes - size: - description: - - The size of the output file in MB. - type: int - default: 400 - binary: - description: - - If this flag is set to yes, the generated file content binary data. - type: bool - default: False -""" - -EXAMPLES = r""" -- name: create 150MB file - k8s_diff: - path: large_file.txt - size: 150 -""" - - -RETURN = r""" -""" - -import os - -from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils._text import to_native - - -def execute_module(module): - try: - size = module.params.get("size") * 1024 * 1024 - path = module.params.get("path") - write_mode = "w" - if module.params.get("binary"): - content = os.urandom(size) - write_mode = "wb" - else: - content = "" - count = 0 - while len(content) < size: - content += "This file has been generated using ansible: {0}\n".format( - count - ) - count += 1 - - with open(path, write_mode) as f: - f.write(content) - module.exit_json(changed=True, size=len(content)) - except Exception as e: - module.fail_json(msg="failed to create file due to: {0}".format(to_native(e))) - - -def main(): - argument_spec = {} - argument_spec["size"] = {"type": "int", "default": 400} - argument_spec["path"] = {"type": "path", "required": True} - argument_spec["binary"] = {"type": "bool", "default": False} - module = AnsibleModule(argument_spec=argument_spec) - - execute_module(module) - - -if __name__ == "__main__": - main() diff --git a/tests/integration/targets/k8s_copy/library/kubectl_file_compare.py b/tests/integration/targets/k8s_copy/library/kubectl_file_compare.py index bcf097839c..e84bbffd87 100644 --- a/tests/integration/targets/k8s_copy/library/kubectl_file_compare.py +++ b/tests/integration/targets/k8s_copy/library/kubectl_file_compare.py @@ -89,10 +89,10 @@ RETURN = r""" """ -import os import filecmp - +import os from tempfile import NamedTemporaryFile, TemporaryDirectory + from ansible.module_utils.basic import AnsibleModule @@ -157,7 +157,6 @@ def compare_directories(dir1, dir2): def execute_module(module): - args = module.params.get("args") local_path = module.params.get("local_path") namespace = module.params.get("namespace") diff --git a/tests/integration/targets/k8s_copy/tasks/test_copy_large_file.yml b/tests/integration/targets/k8s_copy/tasks/test_copy_large_file.yml index 6758202eb2..27d4050ac2 100644 --- a/tests/integration/targets/k8s_copy/tasks/test_copy_large_file.yml +++ b/tests/integration/targets/k8s_copy/tasks/test_copy_large_file.yml @@ -10,16 +10,13 @@ path: "{{ test_directory }}" state: directory - - name: create large text file - k8s_create_file: - path: "{{ test_directory }}/large_text_file.txt" - size: 150 + - name: Create a large text file + ansible.builtin.shell: + cmd: base64 /dev/random | head -c 150M > {{ test_directory }}/large_text_file.txt - - name: create large binary file - k8s_create_file: - path: "{{ test_directory }}/large_bin_file.bin" - size: 200 - binary: true + - name: Create a large binary file + ansible.builtin.command: + cmd: dd if=/dev/random of={{ test_directory }}/large_bin_file.bin bs=1M count=200 # Copy large text file from/to local filesystem to Pod - name: copy large file into remote Pod diff --git a/tests/integration/targets/k8s_validate/tasks/main.yml b/tests/integration/targets/k8s_validate/tasks/main.yml index 900e6f70b1..b7b99c18a2 100644 --- a/tests/integration/targets/k8s_validate/tasks/main.yml +++ b/tests/integration/targets/k8s_validate/tasks/main.yml @@ -52,6 +52,7 @@ name: - kubernetes - kubernetes-validate + - setuptools virtualenv: "{{ virtualenv }}" virtualenv_command: "{{ virtualenv_command }}" virtualenv_site_packages: false diff --git a/tests/integration/targets/setup_kubeconfig/library/test_inventory_read_credentials.py b/tests/integration/targets/setup_kubeconfig/library/test_inventory_read_credentials.py index 9c9e879660..1e0d45ec3e 100644 --- a/tests/integration/targets/setup_kubeconfig/library/test_inventory_read_credentials.py +++ b/tests/integration/targets/setup_kubeconfig/library/test_inventory_read_credentials.py @@ -78,7 +78,6 @@ class K8SInventoryTestModule(AnsibleModule): def __init__(self): - argument_spec = dict( kube_config=dict(required=True, type="path"), dest_dir=dict(required=True, type="path"), @@ -88,7 +87,6 @@ def __init__(self): self.execute_module() def execute_module(self): - dest_dir = os.path.abspath(self.params.get("dest_dir")) kubeconfig_path = self.params.get("kube_config") if not os.path.isdir(dest_dir): diff --git a/tests/sanity/ignore-2.14.txt b/tests/sanity/ignore-2.14.txt index c0d1d58963..e71702746f 100644 --- a/tests/sanity/ignore-2.14.txt +++ b/tests/sanity/ignore-2.14.txt @@ -3,16 +3,19 @@ plugins/module_utils/client/discovery.py import-3.7!skip plugins/module_utils/client/discovery.py import-3.8!skip plugins/module_utils/client/discovery.py import-3.9!skip plugins/module_utils/client/discovery.py import-3.10!skip +plugins/module_utils/client/discovery.py import-3.11!skip plugins/module_utils/client/resource.py import-3.6!skip plugins/module_utils/client/resource.py import-3.7!skip plugins/module_utils/client/resource.py import-3.8!skip plugins/module_utils/client/resource.py import-3.9!skip plugins/module_utils/client/resource.py import-3.10!skip +plugins/module_utils/client/resource.py import-3.11!skip plugins/module_utils/k8sdynamicclient.py import-3.6!skip plugins/module_utils/k8sdynamicclient.py import-3.7!skip plugins/module_utils/k8sdynamicclient.py import-3.8!skip plugins/module_utils/k8sdynamicclient.py import-3.9!skip plugins/module_utils/k8sdynamicclient.py import-3.10!skip +plugins/module_utils/k8sdynamicclient.py import-3.11!skip plugins/modules/k8s.py validate-modules:parameter-type-not-in-doc plugins/modules/k8s_scale.py validate-modules:parameter-type-not-in-doc plugins/modules/k8s_service.py validate-modules:parameter-type-not-in-doc diff --git a/tests/sanity/ignore-2.16.txt b/tests/sanity/ignore-2.16.txt new file mode 100644 index 0000000000..6a2d4d0e77 --- /dev/null +++ b/tests/sanity/ignore-2.16.txt @@ -0,0 +1,40 @@ +plugins/module_utils/client/discovery.py import-3.6!skip +plugins/module_utils/client/discovery.py import-3.7!skip +plugins/module_utils/client/discovery.py import-3.8!skip +plugins/module_utils/client/discovery.py import-3.9!skip +plugins/module_utils/client/discovery.py import-3.10!skip +plugins/module_utils/client/discovery.py import-3.11!skip +plugins/module_utils/client/discovery.py import-3.12!skip +plugins/module_utils/client/resource.py import-3.6!skip +plugins/module_utils/client/resource.py import-3.7!skip +plugins/module_utils/client/resource.py import-3.8!skip +plugins/module_utils/client/resource.py import-3.9!skip +plugins/module_utils/client/resource.py import-3.10!skip +plugins/module_utils/client/resource.py import-3.11!skip +plugins/module_utils/client/resource.py import-3.12!skip +plugins/module_utils/k8sdynamicclient.py import-3.6!skip +plugins/module_utils/k8sdynamicclient.py import-3.7!skip +plugins/module_utils/k8sdynamicclient.py import-3.8!skip +plugins/module_utils/k8sdynamicclient.py import-3.9!skip +plugins/module_utils/k8sdynamicclient.py import-3.10!skip +plugins/module_utils/k8sdynamicclient.py import-3.11!skip +plugins/module_utils/k8sdynamicclient.py import-3.12!skip +plugins/module_utils/version.py pylint!skip +plugins/modules/k8s.py validate-modules:parameter-type-not-in-doc +plugins/modules/k8s_scale.py validate-modules:parameter-type-not-in-doc +plugins/modules/k8s_service.py validate-modules:parameter-type-not-in-doc +tests/unit/module_utils/fixtures/definitions.yml yamllint!skip +tests/unit/module_utils/fixtures/deployments.yml yamllint!skip +tests/integration/targets/k8s_delete/files/deployments.yaml yamllint!skip +tests/unit/module_utils/fixtures/pods.yml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm_diff/files/test-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/k8s_scale/files/deployment.yaml yamllint!skip +tests/sanity/refresh_ignore_files shebang!skip +plugins/modules/k8s.py validate-modules:return-syntax-error +plugins/modules/k8s_scale.py validate-modules:return-syntax-error +plugins/modules/k8s_service.py validate-modules:return-syntax-error +plugins/modules/k8s_taint.py validate-modules:return-syntax-error diff --git a/tests/sanity/ignore-2.9.txt b/tests/sanity/ignore-2.9.txt index 7828a0c087..5016d54c47 100644 --- a/tests/sanity/ignore-2.9.txt +++ b/tests/sanity/ignore-2.9.txt @@ -523,9 +523,6 @@ tests/unit/modules/test_module_helm.py compile-3.5!skip tests/unit/action/test_remove_omit.py compile-2.6!skip tests/unit/action/test_remove_omit.py compile-2.7!skip tests/unit/action/test_remove_omit.py compile-3.5!skip -tests/integration/targets/k8s_copy/library/k8s_create_file.py compile-2.6!skip -tests/integration/targets/k8s_copy/library/k8s_create_file.py compile-2.7!skip -tests/integration/targets/k8s_copy/library/k8s_create_file.py compile-3.5!skip tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-2.6!skip tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-2.7!skip tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-3.5!skip @@ -577,7 +574,6 @@ plugins/module_utils/k8s/client.py pylint!skip plugins/module_utils/k8s/runner.py pylint!skip plugins/module_utils/k8s/service.py pylint!skip plugins/module_utils/k8s/exceptions.py pylint!skip -tests/integration/targets/k8s_copy/library/k8s_create_file.py pylint!skip tests/integration/targets/k8s_copy/library/kubectl_file_compare.py pylint!skip tests/integration/targets/setup_kubeconfig/library/test_inventory_read_credentials.py pylint!skip tests/integration/targets/helm/library/helm_test_version.py pylint!skip diff --git a/tests/sanity/refresh_ignore_files b/tests/sanity/refresh_ignore_files index d06fb31e2a..2f94f12ee8 100644 --- a/tests/sanity/refresh_ignore_files +++ b/tests/sanity/refresh_ignore_files @@ -2,10 +2,8 @@ import itertools - from pathlib import Path - # Mapping of Ansible versions to supported Python versions ANSIBLE_VERSIONS = { "2.9": ["3.6", "3.7", "3.8"], diff --git a/tests/unit/action/test_remove_omit.py b/tests/unit/action/test_remove_omit.py index 3432c19f99..e8206a4407 100644 --- a/tests/unit/action/test_remove_omit.py +++ b/tests/unit/action/test_remove_omit.py @@ -7,6 +7,7 @@ __metaclass__ = type from datetime import datetime + from ansible_collections.kubernetes.core.plugins.action.k8s_info import RemoveOmit diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 20615adb09..63eca8b575 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -6,12 +6,11 @@ import sys from io import BytesIO -import pytest - import ansible.module_utils.basic -from ansible.module_utils.six import string_types +import pytest from ansible.module_utils._text import to_bytes from ansible.module_utils.common._collections_compat import MutableMapping +from ansible.module_utils.six import string_types @pytest.fixture diff --git a/tests/unit/module_utils/test_apply.py b/tests/unit/module_utils/test_apply.py index 07986007bf..3dd1161082 100644 --- a/tests/unit/module_utils/test_apply.py +++ b/tests/unit/module_utils/test_apply.py @@ -18,8 +18,8 @@ __metaclass__ = type from ansible_collections.kubernetes.core.plugins.module_utils.apply import ( - merge, apply_patch, + merge, ) tests = [ diff --git a/tests/unit/module_utils/test_client.py b/tests/unit/module_utils/test_client.py index bba03589e7..d3fec2299c 100644 --- a/tests/unit/module_utils/test_client.py +++ b/tests/unit/module_utils/test_client.py @@ -1,14 +1,14 @@ -import os import base64 +import os import tempfile -import yaml -import mock -from mock import MagicMock +import mock +import yaml from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( _create_auth_spec, _create_configuration, ) +from mock import MagicMock TEST_HOST = "test-host" TEST_SSL_HOST = "https://test-host" diff --git a/tests/unit/module_utils/test_core.py b/tests/unit/module_utils/test_core.py index 189f1f9508..a50833d6e7 100644 --- a/tests/unit/module_utils/test_core.py +++ b/tests/unit/module_utils/test_core.py @@ -6,7 +6,6 @@ import kubernetes import pytest - from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( AnsibleK8SModule, ) diff --git a/tests/unit/module_utils/test_discoverer.py b/tests/unit/module_utils/test_discoverer.py index b23a7a9aa6..618b0cb08b 100644 --- a/tests/unit/module_utils/test_discoverer.py +++ b/tests/unit/module_utils/test_discoverer.py @@ -14,19 +14,17 @@ import pytest - -from kubernetes.client import ApiClient -from kubernetes.dynamic import Resource - -from ansible_collections.kubernetes.core.plugins.module_utils.k8sdynamicclient import ( - K8SDynamicClient, -) from ansible_collections.kubernetes.core.plugins.module_utils.client.discovery import ( LazyDiscoverer, ) from ansible_collections.kubernetes.core.plugins.module_utils.client.resource import ( ResourceList, ) +from ansible_collections.kubernetes.core.plugins.module_utils.k8sdynamicclient import ( + K8SDynamicClient, +) +from kubernetes.client import ApiClient +from kubernetes.dynamic import Resource @pytest.fixture(scope="module") diff --git a/tests/unit/module_utils/test_helm.py b/tests/unit/module_utils/test_helm.py index 0bb9a4b71a..5db0a017c8 100644 --- a/tests/unit/module_utils/test_helm.py +++ b/tests/unit/module_utils/test_helm.py @@ -7,18 +7,17 @@ __metaclass__ = type import os.path -import yaml +import random +import string import tempfile -import pytest - +from unittest.mock import MagicMock +import pytest +import yaml from ansible_collections.kubernetes.core.plugins.module_utils.helm import ( AnsibleHelmModule, write_temp_kubeconfig, ) -from unittest.mock import MagicMock -import random -import string @pytest.fixture() @@ -114,7 +113,6 @@ def test_write_temp_kubeconfig_with_kubeconfig(): def test_module_get_helm_binary_from_params(): - helm_binary_path = MagicMock() helm_sys_binary_path = MagicMock() @@ -129,7 +127,6 @@ def test_module_get_helm_binary_from_params(): def test_module_get_helm_binary_from_system(): - helm_sys_binary_path = MagicMock() module = MagicMock() module.params = {} @@ -140,7 +137,6 @@ def test_module_get_helm_binary_from_system(): def test_module_get_helm_plugin_list(_ansible_helm_module): - _ansible_helm_module.run_helm_command = MagicMock() _ansible_helm_module.run_helm_command.return_value = (0, "output", "error") @@ -156,7 +152,6 @@ def test_module_get_helm_plugin_list(_ansible_helm_module): def test_module_get_helm_plugin_list_failure(_ansible_helm_module): - _ansible_helm_module.run_helm_command = MagicMock() _ansible_helm_module.run_helm_command.return_value = (-1, "output", "error") @@ -175,7 +170,6 @@ def test_module_get_helm_plugin_list_failure(_ansible_helm_module): @pytest.mark.parametrize("no_values", [True, False]) @pytest.mark.parametrize("get_all", [True, False]) def test_module_get_values(_ansible_helm_module, no_values, get_all): - expected = {"test": "units"} output = "---\ntest: units\n" @@ -211,7 +205,6 @@ def test_module_get_values(_ansible_helm_module, no_values, get_all): ], ) def test_module_get_helm_version(_ansible_helm_module, output, expected): - _ansible_helm_module.run_command = MagicMock() _ansible_helm_module.run_command.return_value = (0, output, "error") @@ -224,7 +217,6 @@ def test_module_get_helm_version(_ansible_helm_module, output, expected): def test_module_run_helm_command(_ansible_helm_module): - error = "".join( random.choice(string.ascii_letters + string.digits) for x in range(10) ) @@ -252,7 +244,6 @@ def test_module_run_helm_command(_ansible_helm_module): @pytest.mark.parametrize("fails_on_error", [True, False]) def test_module_run_helm_command_failure(_ansible_helm_module, fails_on_error): - error = "".join( random.choice(string.ascii_letters + string.digits) for x in range(10) ) @@ -311,7 +302,6 @@ def test_module_run_helm_command_failure(_ansible_helm_module, fails_on_error): ], ) def test_module_prepare_helm_environment(params, env_update, kubeconfig): - module = MagicMock() module.params = params @@ -355,7 +345,6 @@ def test_module_prepare_helm_environment(params, env_update, kubeconfig): def test_module_prepare_helm_environment_with_validate_certs( helm_version, is_env_var_set ): - module = MagicMock() module.params = {"validate_certs": False} @@ -387,7 +376,6 @@ def test_module_prepare_helm_environment_with_validate_certs( ], ) def test_module_prepare_helm_environment_with_ca_cert(helm_version, is_env_var_set): - ca_cert = "".join( random.choice(string.ascii_letters + string.digits) for i in range(50) ) @@ -441,7 +429,6 @@ def test_module_prepare_helm_environment_with_ca_cert(helm_version, is_env_var_s ], ) def test_module_get_helm_set_values_args(set_values, expected): - module = MagicMock() module.params = {} module.fail_json.side_effect = SystemExit(1) diff --git a/tests/unit/module_utils/test_runner.py b/tests/unit/module_utils/test_runner.py index 45c6f29aa7..a0555c411f 100644 --- a/tests/unit/module_utils/test_runner.py +++ b/tests/unit/module_utils/test_runner.py @@ -1,12 +1,11 @@ -import pytest from copy import deepcopy from unittest.mock import Mock -from kubernetes.dynamic.resource import ResourceInstance - +import pytest from ansible_collections.kubernetes.core.plugins.module_utils.k8s.runner import ( perform_action, ) +from kubernetes.dynamic.resource import ResourceInstance definition = { "apiVersion": "v1", diff --git a/tests/unit/module_utils/test_service.py b/tests/unit/module_utils/test_service.py index a1822de61a..9803361763 100644 --- a/tests/unit/module_utils/test_service.py +++ b/tests/unit/module_utils/test_service.py @@ -1,14 +1,12 @@ from unittest.mock import Mock import pytest -from kubernetes.dynamic.resource import ResourceInstance, Resource - from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import ( K8sService, diff_objects, ) - from kubernetes.dynamic.exceptions import NotFoundError +from kubernetes.dynamic.resource import Resource, ResourceInstance pod_definition = { "apiVersion": "v1", diff --git a/tests/unit/module_utils/test_waiter.py b/tests/unit/module_utils/test_waiter.py index b5ce10a51f..e63019ec73 100644 --- a/tests/unit/module_utils/test_waiter.py +++ b/tests/unit/module_utils/test_waiter.py @@ -5,20 +5,19 @@ import pytest import yaml -from kubernetes.dynamic.resource import ResourceInstance -from kubernetes.dynamic.exceptions import NotFoundError - from ansible_collections.kubernetes.core.plugins.module_utils.k8s.waiter import ( + DummyWaiter, + Waiter, clock, custom_condition, deployment_ready, - DummyWaiter, exists, get_waiter, pod_ready, resource_absent, - Waiter, ) +from kubernetes.dynamic.exceptions import NotFoundError +from kubernetes.dynamic.resource import ResourceInstance def resources(filepath): diff --git a/tests/unit/modules/test_helm_template_module.py b/tests/unit/modules/test_helm_template_module.py index 9fd98e4c8b..0ff6ad016a 100644 --- a/tests/unit/modules/test_helm_template_module.py +++ b/tests/unit/modules/test_helm_template_module.py @@ -7,14 +7,13 @@ __metaclass__ = type import unittest - from unittest.mock import patch from ansible.module_utils import basic from ansible_collections.kubernetes.core.plugins.modules import helm_template from ansible_collections.kubernetes.core.tests.unit.utils.ansible_module_mock import ( - AnsibleFailJson, AnsibleExitJson, + AnsibleFailJson, exit_json, fail_json, get_bin_path, diff --git a/tests/unit/modules/test_module_helm.py b/tests/unit/modules/test_module_helm.py index ca61cf3ef4..50ce8a4fd8 100644 --- a/tests/unit/modules/test_module_helm.py +++ b/tests/unit/modules/test_module_helm.py @@ -7,14 +7,13 @@ __metaclass__ = type import unittest - -from unittest.mock import MagicMock, patch, call +from unittest.mock import MagicMock, call, patch from ansible.module_utils import basic from ansible_collections.kubernetes.core.plugins.modules import helm from ansible_collections.kubernetes.core.tests.unit.utils.ansible_module_mock import ( - AnsibleFailJson, AnsibleExitJson, + AnsibleFailJson, exit_json, fail_json, get_bin_path, diff --git a/tox.ini b/tox.ini index c307fbd186..e2d15b071f 100644 --- a/tox.ini +++ b/tox.ini @@ -2,6 +2,9 @@ minversion = 1.4.2 skipsdist = True +[common] +format_dirs = {toxinidir}/plugins {toxinidir}/tests + [testenv:integration] install_command = pip install {opts} {packages} @@ -27,19 +30,33 @@ commands = black -v --check --diff {toxinidir}/plugins {toxinidir}/tests [testenv:black] +depends = + flynt, isort deps = - {[testenv:black_check]deps} + black >=23.0, <24.0 +commands = + black -v {[common]format_dirs} +[testenv:isort] +deps = + isort commands = - black -v {posargs:{toxinidir}/plugins {toxinidir}/tests} + isort --profile black {[common]format_dirs} + +[testenv:flynt] +deps = + flynt +commands = + flynt {[common]format_dirs} [testenv:linters] deps = - yamllint + {[testenv:black]deps} + {[testenv:isort]deps} flake8 - {[testenv:black_check]deps} - + yamllint commands = - {[testenv:black_check]commands} + black -v --check {toxinidir}/plugins {toxinidir}/tests + isort --profile black --check-only --diff {toxinidir}/plugins {toxinidir}/tests + flake8 {posargs} {toxinidir}/plugins {toxinidir}/tests yamllint -s {toxinidir} - flake8 {toxinidir}