Skip to content

Add api_versions to helm_template #881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

benbz
Copy link

@benbz benbz commented Feb 24, 2025

SUMMARY

Allows passing of additional Kubernetes API versions to helm template such that chart templates that are gated on Capabilities.APIVersions having e.g. CRDs can render those templates rather than them being blank.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

helm_template

ADDITIONAL INFORMATION

Inspired by #587 (comment)

Setup:

$ cat helm_template_test/Chart.yaml 
apiVersion: 2
name: ansible-test
version: 0.0.0-dev

$ cat helm_template_test/templates/capabilities.yaml 
{{- if $.Capabilities.APIVersions.Has "monitoring.coreos.com/v1/ServiceMonitor" }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ $.Release.Name }}-haproxy
  namespace: {{ $.Release.Namespace }}
spec:
  endpoints:
  - interval: 30s
    port: haproxy-metrics
  selector:
    matchLabels:
      app.kubernetes.io/instance: haproxy
{{- end }}

Before:

$ cat helm_template_test.yaml 
---
- hosts: localhost
  connection: local
  tasks:
  - kubernetes.core.helm_template:
      release_name: ansible-test
      release_namespace: ansible
      chart_ref: helm_template_test
    register: helm_templates

  - debug:
      msg: "{{ helm_templates.stdout }}"

$ ansible-playbook helm_template_test.yaml    
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] *********************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [kubernetes.core.helm_template] *************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [debug] *************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "\n"
}

PLAY RECAP ***************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

After:

$ cat helm_template_test.yaml 
---
- hosts: localhost
  connection: local
  tasks:
  - kubernetes.core.helm_template:
      release_name: ansible-test
      release_namespace: ansible
      chart_ref: helm_template_test
      api_versions:
      - monitoring.coreos.com/v1/ServiceMonitor
    register: helm_templates

  - debug:
      msg: "{{ helm_templates.stdout }}"

$ ansible-playbook helm_template_test.yaml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] *********************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [kubernetes.core.helm_template] *************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [debug] *************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "---\n# Source: ansible-test/templates/capabilities.yaml\napiVersion: monitoring.coreos.com/v1\nkind: ServiceMonitor\nmetadata:\n  name: ansible-test-haproxy\n  namespace: ansible\nspec:\n  endpoints:\n  - interval: 30s\n    port: haproxy-metrics\n  selector:\n    matchLabels:\n      app.kubernetes.io/instance: haproxy\n"
}

PLAY RECAP ***************************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant