Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Lookup function in Helm Chart

While deploying a Kubernetes application, I want to check if a particular PodSecurityPolicy exists, and if it does then skip installing it again. I came across the helm lookup function, which allows us to check the existing K8 resources. While I understand how to use this function to get all the resources of same kind, how do I use this function to check if a PodSecurityPolicy named "myPodSecurityPolicy" exists.

I tried something like this:

{{- if ne (lookup "v1" "PodSecurityPolicy" "" "") "myPodSecurityPolicy"}}
<do my stuff>

{{- end }}

But it doesn't look like I can compare it this way, seeing an error -

error calling ne: invalid type for comparison

Any inputs? Thanks in advance.

like image 928
user4202236 Avatar asked Aug 17 '20 00:08

user4202236


People also ask

What is $_ in Helm?

The $_ is used to suppress undesired output as "set" returns the new dictionary. The above returns: - name: mongod-none. Any values added to the dictionary will live beyond the call. If you want to avoid polluting an existing dictionary you can force a deep copy with: {{- $d := merge (dict) . -}}

What is toYaml function in Helm?

The above includes a template called toYaml , passes it $value , and then passes the output of that template to the indent function. Because YAML ascribes significance to indentation levels and whitespace, this is one great way to include snippets of code, but handle indentation in a relevant context.

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


1 Answers

Please check your API version and PSP name. Lookup is returning a map or nil not a string and that's why you are getting that error. The following is working for me. For negative expression, just add not after if.

{{- if (lookup "policy/v1beta1" "PodSecurityPolicy" "" "example") }}
<found: do your stuff>

{{- end }}

HTH

like image 102
Faheem Avatar answered Oct 14 '22 06:10

Faheem