Search code examples
kuberneteskubernetes-helm

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.


Solution

  • 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