Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell helm to not create/change resource if it already exists?

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?

like image 912
Tom Klino Avatar asked Sep 12 '19 15:09

Tom Klino


People also ask

What is TPL function in Helm?

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.

What is _helpers TPL in Helm?

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 -}}

How do you override a Helm chart?

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.


2 Answers

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

like image 197
sankalp Avatar answered Sep 17 '22 12:09

sankalp


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.

like image 45
Yasen Avatar answered Sep 17 '22 12:09

Yasen