I have a secret resource templates that upon installation will generate random values for the data.
I run into a problem after removing that chart (which did not delete the secret resource, because those stay), and reinstalling it later.
That caused it to run over the previous secret with newly generated data, which is not desired in my case.
I'm trying to find what is the if condition I need to include in the template so that it will not create or modify the secret if it already exists. How do I do that?
Using the 'tpl' Function The tpl function allows developers to evaluate strings as templates inside a template. This is useful to pass a template string as a value to a chart or render external configuration files.
tpl? Helm allows for the use of Go templating in resource files for Kubernetes. A file named _helpers.tpl is usually used to define Go template helpers with this syntax: {{- define "yourFnName" -}} {{- printf "%s-%s" .Values.name .Values.version | trunc 63 -}} {{- end -}}
You can use a --values flag in your Helm commands to override the values in a chart and pass in a new file. Specify the name of the new file after the --values flag in the Helm command. Example: helm upgrade --install <service> -f values.
From helm 3.1 version onwards, there is a lookup function available to check if the given resource exists or not. Using this function you can skip the upgrading or installing a resource if it already exists.
Lookup function
For example, see this sample-chart. I have created a function named gen.secret
in _helpers.tpl and used it in secret.yaml
Sorry, but you can't at the moment.
According to the issue #4824 (Ignore existing resource on install if resource-policy is keep), the helm team neither support it yet nor have any plans.
UPD: there is a workaround for secrets:
generate secrets with Ansible k8s module, and pass them to Helm charts. A bit ugly, but works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With