Skip to content

Commit

Permalink
helm_info: add release_state argument
Browse files Browse the repository at this point in the history
Specify release state in helm list command as per helm cmdline flags.

Fixes: ansible-collections#377

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde committed Feb 14, 2022
1 parent bf3fe91 commit ed3a201
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/377-helm-info-state.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- helm_info - add release state as a module argument (https://github.com/ansible-collections/kubernetes.core/issues/377).
10 changes: 10 additions & 0 deletions molecule/default/roles/helm/tasks/tests_chart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,21 @@
namespace: "{{ helm_namespace }}"
register: content_info

- name: Check helm_info content using release_state
helm_info:
binary_path: "{{ helm_binary }}"
name: test
namespace: "{{ helm_namespace }}"
release_state:
- deployed
register: release_state_content_info

- name: "Assert that {{ chart_test }} is installed from {{ source }} with helm_info"
assert:
that:
- content_info.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
- content_info.status.status | lower == 'deployed'
- release_state_content_info.status.status | lower == 'deployed'

- name: Check idempotency
helm:
Expand Down
40 changes: 36 additions & 4 deletions plugins/modules/helm_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,35 @@
required: true
type: str
aliases: [ namespace ]
release_state:
description:
- Show releases as per their states.
- Default value is C(deployed).
- If set to C(all), show all releases without any filter applied.
- If set to C(deployed), show deployed releases.
- If set to C(failed), show failed releases.
- If set to C(pending), show pending releases.
- If set to C(superseded), show superseded releases.
- If set to C(uninstalled), show uninstalled releases, if C(helm uninstall --keep-history) was used.
- If set to C(uninstalling), show releases that are currently being uninstalled.
type: list
version_added: "2.3.0"
extends_documentation_fragment:
- kubernetes.core.helm_common_options
"""

EXAMPLES = r"""
- name: Deploy latest version of Grafana chart inside monitoring namespace
- name: Gather information of Grafana chart inside monitoring namespace
kubernetes.core.helm_info:
name: test
release_namespace: monitoring
- name: Gather information about test-chart with pending state
kubernetes.core.helm_info:
name: test-chart
release_namespace: testenv
release_state:
- pending
"""

RETURN = r"""
Expand Down Expand Up @@ -117,9 +137,17 @@ def get_release(state, release_name):


# Get Release state from deployed release
def get_release_status(module, command, release_name):
list_command = command + " list --output=yaml --filter " + release_name
def get_release_status(module, command, release_name, release_state=[]):
list_command = command + " list --output=yaml"

valid_release_states = ['all', 'deployed', 'failed', 'pending',
'superseded', 'uninstalled', 'uninstalling']

for local_release_state in release_state:
if local_release_state in valid_release_states:
list_command += " --%s" % local_release_state

list_command += " --filter " + release_name
rc, out, err = run_helm(module, list_command)

if rc != 0:
Expand Down Expand Up @@ -175,6 +203,9 @@ def main():
api_key=dict(
type="str", no_log=True, fallback=(env_fallback, ["K8S_AUTH_API_KEY"])
),
release_state=dict(
type="list",
)
),
mutually_exclusive=[
("context", "ca_cert"),
Expand All @@ -190,13 +221,14 @@ def main():

bin_path = module.params.get("binary_path")
release_name = module.params.get("release_name")
release_state = module.params.get("release_state")

if bin_path is not None:
helm_cmd_common = bin_path
else:
helm_cmd_common = module.get_bin_path("helm", required=True)

release_status = get_release_status(module, helm_cmd_common, release_name)
release_status = get_release_status(module, helm_cmd_common, release_name, release_state)

if release_status is not None:
module.exit_json(changed=False, status=release_status)
Expand Down

0 comments on commit ed3a201

Please sign in to comment.