From 1fb38fa98295b35967ba85e7e28ca09011533a6b Mon Sep 17 00:00:00 2001 From: Will Thames Date: Tue, 9 Jun 2020 18:50:37 +1000 Subject: [PATCH 1/5] Add setup.cfg Start imposing some coding standards --- setup.cfg | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 setup.cfg diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 00000000..29c924b8 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,3 @@ +[flake8] +max-line-length = 160 +ignore = W503,E402 From 59348066a02a848b8d302906f5a9f47b2ce50877 Mon Sep 17 00:00:00 2001 From: Will Thames Date: Tue, 9 Jun 2020 18:53:36 +1000 Subject: [PATCH 2/5] F401: Remove unused imports --- plugins/connection/kubectl.py | 1 - plugins/lookup/k8s.py | 12 ------------ plugins/module_utils/raw.py | 5 ----- plugins/modules/k8s_service.py | 2 +- 4 files changed, 1 insertion(+), 19 deletions(-) diff --git a/plugins/connection/kubectl.py b/plugins/connection/kubectl.py index b320c494..71f21e2f 100644 --- a/plugins/connection/kubectl.py +++ b/plugins/connection/kubectl.py @@ -173,7 +173,6 @@ import os import os.path import subprocess -import ansible.constants as C from ansible.parsing.yaml.loader import AnsibleLoader from ansible.errors import AnsibleError, AnsibleFileNotFound from ansible.module_utils.six.moves import shlex_quote diff --git a/plugins/lookup/k8s.py b/plugins/lookup/k8s.py index bcedc976..68fa9df5 100644 --- a/plugins/lookup/k8s.py +++ b/plugins/lookup/k8s.py @@ -202,7 +202,6 @@ from ansible_collections.community.kubernetes.plugins.module_utils.common import try: - from openshift.dynamic import DynamicClient from openshift.dynamic.exceptions import NotFoundError HAS_K8S_MODULE_HELPER = True k8s_import_exception = None @@ -210,12 +209,6 @@ except ImportError as e: HAS_K8S_MODULE_HELPER = False k8s_import_exception = e -try: - import yaml - HAS_YAML = True -except ImportError: - HAS_YAML = False - class KubernetesLookup(K8sAnsibleMixin): @@ -226,11 +219,6 @@ class KubernetesLookup(K8sAnsibleMixin): "Requires the OpenShift Python client. Try `pip install openshift`. Detail: {0}".format(k8s_import_exception) ) - if not HAS_YAML: - raise Exception( - "Requires PyYAML. Try `pip install PyYAML`" - ) - self.kind = None self.name = None self.namespace = None diff --git a/plugins/module_utils/raw.py b/plugins/module_utils/raw.py index 775deef7..59dbdf3b 100644 --- a/plugins/module_utils/raw.py +++ b/plugins/module_utils/raw.py @@ -39,11 +39,6 @@ except ImportError: # Exceptions handled in common pass -try: - import kubernetes_validate - HAS_KUBERNETES_VALIDATE = True -except ImportError: - HAS_KUBERNETES_VALIDATE = False K8S_CONFIG_HASH_IMP_ERR = None try: diff --git a/plugins/modules/k8s_service.py b/plugins/modules/k8s_service.py index dcfd714b..6df0ab5e 100644 --- a/plugins/modules/k8s_service.py +++ b/plugins/modules/k8s_service.py @@ -181,7 +181,7 @@ import traceback from collections import defaultdict -from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC, COMMON_ARG_SPEC +from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC from ansible_collections.community.kubernetes.plugins.module_utils.raw import KubernetesRawModule From 43ae9664fc0bcdb1c147b271d9786d944bd2af55 Mon Sep 17 00:00:00 2001 From: Will Thames Date: Tue, 9 Jun 2020 18:54:48 +1000 Subject: [PATCH 3/5] W504 Move binary operator to start of line from end of line PEP8 prefers binary operators at the start of a line https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator --- plugins/module_utils/common.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index f9562011..9120116d 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -325,21 +325,21 @@ class KubernetesAnsibleModule(AnsibleModule, K8sAnsibleMixin): # Scaling up means that we also need to check that we're not in a # situation where status.replicas == status.availableReplicas # but spec.replicas != status.replicas - return (deployment.status and - deployment.spec.replicas == (deployment.status.replicas or 0) and - deployment.status.availableReplicas == deployment.status.replicas and - deployment.status.observedGeneration == deployment.metadata.generation and - not deployment.status.unavailableReplicas) + return (deployment.status + and deployment.spec.replicas == (deployment.status.replicas or 0) + and deployment.status.availableReplicas == deployment.status.replicas + and deployment.status.observedGeneration == deployment.metadata.generation + and not deployment.status.unavailableReplicas) def _pod_ready(pod): - return (pod.status and pod.status.containerStatuses is not None and - all([container.ready for container in pod.status.containerStatuses])) + return (pod.status and pod.status.containerStatuses is not None + and all([container.ready for container in pod.status.containerStatuses])) def _daemonset_ready(daemonset): - return (daemonset.status and daemonset.status.desiredNumberScheduled is not None and - daemonset.status.numberReady == daemonset.status.desiredNumberScheduled and - daemonset.status.observedGeneration == daemonset.metadata.generation and - not daemonset.status.unavailableReplicas) + return (daemonset.status and daemonset.status.desiredNumberScheduled is not None + and daemonset.status.numberReady == daemonset.status.desiredNumberScheduled + and daemonset.status.observedGeneration == daemonset.metadata.generation + and not daemonset.status.unavailableReplicas) def _custom_condition(resource): if not resource.status or not resource.status.conditions: From 510a069de948e515e690ec866c55204ab0822e63 Mon Sep 17 00:00:00 2001 From: Will Thames Date: Tue, 9 Jun 2020 18:59:00 +1000 Subject: [PATCH 4/5] F841 remove unused variables --- plugins/inventory/k8s.py | 2 +- plugins/modules/k8s_auth.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/inventory/k8s.py b/plugins/inventory/k8s.py index 0c663b76..9c314562 100644 --- a/plugins/inventory/k8s.py +++ b/plugins/inventory/k8s.py @@ -317,7 +317,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable, K8sAnsibleM try: self.inventory.add_child(namespace_services_group, service_name) - except AnsibleError as e: + except AnsibleError: raise ports = [{'name': port.name, diff --git a/plugins/modules/k8s_auth.py b/plugins/modules/k8s_auth.py index d225132c..3af297ba 100644 --- a/plugins/modules/k8s_auth.py +++ b/plugins/modules/k8s_auth.py @@ -251,7 +251,7 @@ class KubernetesAuthModule(AnsibleModule): self.openshift_auth_endpoint = oauth_info['authorization_endpoint'] self.openshift_token_endpoint = oauth_info['token_endpoint'] - except Exception as e: + except Exception: self.fail_json(msg="Something went wrong discovering OpenShift OAuth details.", exception=traceback.format_exc()) @@ -311,7 +311,7 @@ class KubernetesAuthModule(AnsibleModule): "kind": "DeleteOptions" } - ret = requests.delete(url, headers=headers, json=json, verify=self.con_verify_ca) + requests.delete(url, headers=headers, json=json, verify=self.con_verify_ca) # Ignore errors, the token will time out eventually anyway def fail(self, msg=None): From 61c33724f335c76ec05108b81f457e58720cff34 Mon Sep 17 00:00:00 2001 From: Will Thames Date: Tue, 9 Jun 2020 19:24:37 +1000 Subject: [PATCH 5/5] Turn on flake8 checks in molecule tests --- .github/workflows/ci.yml | 2 +- molecule/default/molecule.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9c47af11..acb83259 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -84,7 +84,7 @@ jobs: python-version: ${{ matrix.python_version }} - name: Install molecule and openshift dependencies - run: pip install molecule yamllint openshift + run: pip install molecule yamllint openshift flake8 - name: Install ansible base (devel branch) run: pip install https://github.com/ansible/ansible/archive/devel.tar.gz --disable-pip-version-check diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 37f7c3bc..37cb0028 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -9,6 +9,7 @@ driver: lint: | set -e yamllint . + flake8 platforms: - name: instance-kind provisioner: