Skip to content

feat: execute templates against postRendererHooks#1839

Merged
zhaque44 merged 1 commit into
helmfile:mainfrom
allanger:feat-add-templates-to-post-renderer-args
Jan 28, 2025
Merged

feat: execute templates against postRendererHooks#1839
zhaque44 merged 1 commit into
helmfile:mainfrom
allanger:feat-add-templates-to-post-renderer-args

Conversation

@allanger

@allanger allanger commented Dec 18, 2024

Copy link
Copy Markdown
Contributor

This commit is supposed to add template support to post renderer args. Also, to make it possible to template arguments that are added to helm defaults, during the load, I'm removing default post renderer args from the state and putting them to each release, unless custom args are defined for the release.

Why do I need it?

I'm trying to implement some kind of audit log to the helmfile. The idea is pretty simple, I'm adding a configmap that contains an information that can be used to identify a person that has sync the release via helmfile. In the first implementation, I was using subcharts with some .Release.Set fields, added via templates, so I could just add a release like that

templates:
  apply-log:
    dependencies:
      - chart: ./charts/apply-log
        version: '0.1.0'
        alias: apply-log
    set:
      - name: apply-log.author
        value: '{{ env "USER" }}'

releases:
  - name: test
    inherit:
      - template: apply-log 

But from time to time, it's violating chart's values schemas, and also it needs to be enabled per-release, that I consider error-prone.

So I had an idea to use postRendererHooks for that, but I couldn't find a way to access a Release Name from the hook, and that's why I came up with this PR.

Example:

#!/bin/bash

WORKDIR=$(mktemp -d)
cat <&0 > "${WORKDIR}/result.yaml"

echo "---" >> "${WORKDIR}/result.yaml"

kubectl create configmap --dry-run=client "${1}-apply-log" -o yaml --from-literal author="${USER}" >> "${WORKDIR}/result.yaml"

cat "${WORKDIR}/result.yaml"

This script is supposed to take the first argument and use it for the resource name.

I have helmDefaults set like this:

helmDefaults:
  postRenderer: ./scripts/post_render_apply_log.sh
  postRendererArgs:
    - "{{ `{{ .Release.Name }}` }}"
 releases:
  - name: namespaces
    chart: ./charts/namespaces
    namespace: kube-public
    createNamespace: false
    inherit:
      - template: default-env-values

And then the result is

❯ hf diff -l name=namespaces

Comparing release=namespaces, chart=charts/namespaces, namespace=kube-public
kube-public, namespaces-apply-log, ConfigMap (v1) has been added:
-
+ apiVersion: v1
+ data:
+   author: allanger
+ kind: ConfigMap
+ metadata:
+   creationTimestamp: null
+   name: namespaces-apply-log

Why am I merging .HelmDefaults.PostRendererArgs to the .Release.PostRendererArgs. To avoid adding the args entry to each release, I need to merge all the args before templating, so I can access the release Data in the Args that are defined globally.

@allanger allanger force-pushed the feat-add-templates-to-post-renderer-args branch from 0638e23 to f8242eb Compare December 18, 2024 18:11
@allanger

Copy link
Copy Markdown
Contributor Author

I haven't added tests to the merging part, cause I wasn't sure, whether it's the right way to handle it at all

@yxxhero

yxxhero commented Dec 18, 2024

Copy link
Copy Markdown
Member

@allanger please fix lint issue.

@allanger

allanger commented Dec 19, 2024

Copy link
Copy Markdown
Contributor Author

Also, if the merging of the postRendererHooks args seems fine for you, it makes sense to clean up the addPostRendererArgs functions, because at the point of executing them, there will be no helmDefaults.postRendererArgs set at all. But again, I haven't done it, because I'm not sure about the change itself.

Another problem would be the args that are coming from the CLI, they are not templated at all, because I couldn't find a place where I can access both State and Args before templating, as it was the first time I've seen the code, and most probably, I just couldn't follow it.

@zhaque44

Copy link
Copy Markdown
Contributor

@yxxhero @mumoshu are you sure this change won't break anything downstream? at a first glance it looks ok

@allanger allanger force-pushed the feat-add-templates-to-post-renderer-args branch from f636b67 to b202cd9 Compare December 21, 2024 09:49
@zhaque44 zhaque44 force-pushed the feat-add-templates-to-post-renderer-args branch from b202cd9 to 974c4b1 Compare December 23, 2024 15:12
@zhaque44 zhaque44 force-pushed the feat-add-templates-to-post-renderer-args branch from 974c4b1 to 475705f Compare January 8, 2025 17:21
@stale

stale Bot commented Jan 24, 2025

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the wontfix This will not be worked on label Jan 24, 2025
@allanger

Copy link
Copy Markdown
Contributor Author

Is there a chance that we can get it going?

@yxxhero

yxxhero commented Jan 24, 2025

Copy link
Copy Markdown
Member

@allanger please fix ci issue.

@stale stale Bot removed the wontfix This will not be worked on label Jan 24, 2025
@allanger

Copy link
Copy Markdown
Contributor Author

@yxxhero This is the failed job: https://github.com/helmfile/helmfile/actions/runs/12675728241/job/35327605145#step:9:2044. and there is nothing I can do about it, I believe, we only can re-run

And the next runs are failing because the artifact is not available anymore. May I ask you to trigger the whole pipeline again, including the successful jobs?

This commit is supposed to add template support to post renderer args.
Also, to make it possible to template arguments that are added to helm
defaults, during the load, I'm removing default post renderer args from
the state and putting them to each release, unless custom args are
defined for the release.

Signed-off-by: Nikolai Rodionov <[email protected]>
@zhaque44 zhaque44 force-pushed the feat-add-templates-to-post-renderer-args branch from 475705f to c5d4441 Compare January 28, 2025 13:11
@zhaque44 zhaque44 merged commit b5eb879 into helmfile:main Jan 28, 2025
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jan 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants