From e98ef31f0189560e1da7ebe2df6c86b9f8c09037 Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Fri, 15 Jan 2021 13:20:23 -0500 Subject: [PATCH 1/3] Add skip_crds option to helm This adds an option to skip the installation of CRDs when installing or upgrading a chart. Closes #296. --- plugins/modules/helm.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index 993def91..e712227a 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -133,6 +133,12 @@ type: bool default: False version_added: "1.11.0" + skip_crds: + description: + - Skip custom resource definitions when installing or upgrading. + type: bool + default: False + version_added: "1.2.0" extends_documentation_fragment: - community.kubernetes.helm_common_options ''' @@ -321,7 +327,7 @@ def fetch_chart_info(module, command, chart_ref): def deploy(command, release_name, release_values, chart_name, wait, wait_timeout, disable_hook, force, values_files, atomic=False, - create_namespace=False, replace=False): + create_namespace=False, replace=False, skip_crds=False): """ Install/upgrade/rollback release chart """ @@ -364,6 +370,9 @@ def deploy(command, release_name, release_values, chart_name, wait, yaml.dump(release_values, yaml_file, default_flow_style=False) deploy_command += " -f=" + path + if skip_crds: + deploy_command += " --skip-crds" + deploy_command += " " + release_name + " " + chart_name return deploy_command @@ -489,6 +498,7 @@ def main(): atomic=dict(type='bool', default=False), create_namespace=dict(type='bool', default=False), replace=dict(type='bool', default=False), + skip_crds=dict(type='bool', default=False), # Generic auth key host=dict(type='str', fallback=(env_fallback, ['K8S_AUTH_HOST'])), @@ -533,6 +543,7 @@ def main(): atomic = module.params.get('atomic') create_namespace = module.params.get('create_namespace') replace = module.params.get('replace') + skip_crds = module.params.get('skip_crds') if bin_path is not None: helm_cmd_common = bin_path @@ -567,7 +578,8 @@ def main(): if release_status is None: # Not installed helm_cmd = deploy(helm_cmd, release_name, release_values, chart_ref, wait, wait_timeout, disable_hook, False, values_files=values_files, atomic=atomic, - create_namespace=create_namespace, replace=replace) + create_namespace=create_namespace, replace=replace, + skip_crds=skip_crds) changed = True else: @@ -583,7 +595,8 @@ def main(): if force or would_change: helm_cmd = deploy(helm_cmd, release_name, release_values, chart_ref, wait, wait_timeout, disable_hook, force, values_files=values_files, atomic=atomic, - create_namespace=create_namespace, replace=replace) + create_namespace=create_namespace, replace=replace, + skip_crds=skip_crds) changed = True if module.check_mode: From 71ca0ada11f9e88356187d207d77e408c8b4fccd Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Fri, 15 Jan 2021 13:29:08 -0500 Subject: [PATCH 2/3] Add changelog fragment --- changelogs/fragments/349-skip-crds.yaml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/349-skip-crds.yaml diff --git a/changelogs/fragments/349-skip-crds.yaml b/changelogs/fragments/349-skip-crds.yaml new file mode 100644 index 00000000..37ea0b55 --- /dev/null +++ b/changelogs/fragments/349-skip-crds.yaml @@ -0,0 +1,2 @@ +minor_changes: + - helm - add a ``skip_crds`` option to skip the installation of CRDs when installing or upgrading a chart (https://github.com/ansible-collections/community.kubernetes/issues/296). From 601c863c57d30fda1cd3879d13225d3a7af3b5c5 Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Thu, 28 Jan 2021 11:25:09 -0500 Subject: [PATCH 3/3] Add CRD tests Signed-off-by: Mike Graves Signed-off-by: Abhijeet Kasurde --- .../roles/helm/files/test-crds/Chart.yaml | 5 + .../roles/helm/files/test-crds/crds/crd.yaml | 21 ++++ .../default/roles/helm/tasks/run_test.yml | 4 + .../default/roles/helm/tasks/test_crds.yml | 97 +++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 molecule/default/roles/helm/files/test-crds/Chart.yaml create mode 100644 molecule/default/roles/helm/files/test-crds/crds/crd.yaml create mode 100644 molecule/default/roles/helm/tasks/test_crds.yml diff --git a/molecule/default/roles/helm/files/test-crds/Chart.yaml b/molecule/default/roles/helm/files/test-crds/Chart.yaml new file mode 100644 index 00000000..05c096f9 --- /dev/null +++ b/molecule/default/roles/helm/files/test-crds/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v2 +name: test-crds +description: A chart with CRDs +type: application +version: 0.1.0 diff --git a/molecule/default/roles/helm/files/test-crds/crds/crd.yaml b/molecule/default/roles/helm/files/test-crds/crds/crd.yaml new file mode 100644 index 00000000..b63ddc9d --- /dev/null +++ b/molecule/default/roles/helm/files/test-crds/crds/crd.yaml @@ -0,0 +1,21 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: foos.example.com +spec: + group: example.com + versions: + - name: v1 + served: true + storage: true + schema: + openAPIV3Schema: + type: object + properties: + foobar: + type: string + scope: Namespaced + names: + plural: foos + singular: foo + kind: Foo diff --git a/molecule/default/roles/helm/tasks/run_test.yml b/molecule/default/roles/helm/tasks/run_test.yml index 2c3b009a..108bd24b 100644 --- a/molecule/default/roles/helm/tasks/run_test.yml +++ b/molecule/default/roles/helm/tasks/run_test.yml @@ -33,6 +33,10 @@ - name: Test helm diff include_tasks: tests_helm_diff.yml +# https://github.com/ansible-collections/community.kubernetes/issues/296 +- name: Test Skip CRDS feature in helm chart install + include_tasks: test_crds.yml + - name: Clean helm install file: path: "{{ item }}" diff --git a/molecule/default/roles/helm/tasks/test_crds.yml b/molecule/default/roles/helm/tasks/test_crds.yml new file mode 100644 index 00000000..071ebc71 --- /dev/null +++ b/molecule/default/roles/helm/tasks/test_crds.yml @@ -0,0 +1,97 @@ +--- +- name: Test CRDs + vars: + test_chart: "test-crds" + block: + - name: Create namespace + k8s: + kind: Namespace + name: "{{ helm_namespace }}" + + - name: Copy test chart + copy: + src: "{{ test_chart }}" + dest: "/tmp/helm_test_crds/" + + - name: Install chart while skipping CRDs + helm: + chart_ref: "/tmp/helm_test_crds/{{ test_chart }}" + namespace: "{{ helm_namespace }}" + name: test-crds + skip_crds: true + register: install + + - assert: + that: + - install is changed + - install.status.name == "test-crds" + + - name: Fail to create custom resource + k8s: + definition: + apiVersion: example.com/v1 + kind: Foo + metadata: + namespace: "{{ helm_namespace }}" + name: test-foo + foobar: footest + ignore_errors: true + register: result + + - assert: + that: + - result is failed + - "result.msg.startswith('Failed to find exact match for example.com/v1.Foo')" + + # Helm won't install CRDs into an existing release, so we need to delete this, first + - name: Uninstall chart + helm: + namespace: "{{ helm_namespace }}" + name: test-crds + state: absent + + - name: Install chart with CRDs + helm: + chart_ref: "/tmp/helm_test_crds/{{ test_chart }}" + namespace: "{{ helm_namespace }}" + name: test-crds + + - name: Create custom resource + k8s: + definition: + apiVersion: example.com/v1 + kind: Foo + metadata: + namespace: "{{ helm_namespace }}" + name: test-foo + foobar: footest + register: result + + - assert: + that: + - result is changed + - result.result.foobar == "footest" + + always: + - name: Remove chart + file: + path: "/tmp/helm_test_crds" + state: absent + ignore_errors: true + + - name: Remove namespace + k8s: + kind: Namespace + name: "{{ helm_namespace }}" + state: absent + wait: true + wait_timeout: 180 + ignore_errors: true + + # CRDs aren't deleted with a namespace, so we need to manually delete it + - name: Remove CRD + k8s: + kind: CustomResourceDefinition + name: foos.example.com + state: absent + ignore_errors: true