Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Add skip_crds option to helm #349

Merged
merged 3 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/349-skip-crds.yaml
Original file line number Diff line number Diff line change
@@ -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).
Akasurde marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions molecule/default/roles/helm/files/test-crds/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v2
name: test-crds
description: A chart with CRDs
type: application
version: 0.1.0
21 changes: 21 additions & 0 deletions molecule/default/roles/helm/files/test-crds/crds/crd.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions molecule/default/roles/helm/tasks/run_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
97 changes: 97 additions & 0 deletions molecule/default/roles/helm/tasks/test_crds.yml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 16 additions & 3 deletions plugins/modules/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''
Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'])),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down