Skip to content

Commit

Permalink
helm_info: add release_state argument (#379)
Browse files Browse the repository at this point in the history
helm_info: add release_state argument

SUMMARY
Specify release state in helm list command as per helm cmdline flags.
Fixes: #377
Signed-off-by: Abhijeet Kasurde akasurde@redhat.com
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
changelogs/fragments/377-helm-info-state.yml
molecule/default/roles/helm/tasks/tests_chart.yml
plugins/modules/helm_info.py

Reviewed-by: None <None>
Reviewed-by: Abhijeet Kasurde <None>
Reviewed-by: Mike Graves <mgraves@redhat.com>
  • Loading branch information
Akasurde authored Feb 17, 2022
1 parent 691f0cb commit aae5960
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 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
50 changes: 45 additions & 5 deletions plugins/modules/helm_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,36 @@
required: true
type: str
aliases: [ namespace ]
release_state:
description:
- Show releases as per their states.
- Default value is C(deployed) and C(failed).
- 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
elements: str
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 +138,24 @@ 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 +211,7 @@ def main():
api_key=dict(
type="str", no_log=True, fallback=(env_fallback, ["K8S_AUTH_API_KEY"])
),
release_state=dict(type="list", default=[], elements="str"),
),
mutually_exclusive=[
("context", "ca_cert"),
Expand All @@ -190,13 +227,16 @@ 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 aae5960

Please sign in to comment.