kuberneteskubernetes-helm

Helm3 Using Lookup Function to Load a Variable


I am currently attempting to use the lookup function via Helm 3.1 to load a variable during installation.

{{ $ingress := (lookup "v1" "Ingress" "mynamespace" "ingressname").status.loadBalancer.ingress[0].hostname }}

Of course, this returns, "bad character [." If I remove it, it returns "nil pointer evaluating interface {}.loadBalancer".

Is what I am attempting to do even possible?

Thanks


Solution

  • You are attempting to use "normal" array indexing syntax, but helm charts use "golang templates" and thus array indexing is done via the index function

    {{ $ingress := (index (lookup "v1" "Ingress" "mynamespace" "ingressname").status.loadBalancer.ingress 0).hostname }}
    

    after further thought, I can easily imagine that nil pointer error happening during helm template runs, since lookup returns map[] when running offline

    In that case, you'd want to use the index function for every path navigation:

    {{ $ingress := (index (index (index (index (index (lookup "v1" "Ingress" "mynamespace" "ingressname") "status") "loadBalancer")  "ingress") 0) "hostname") }}
    

    or, assert the lookup is in "offline" mode and work around it:

          {{ $ingress := "fake.example.com" }}
          {{ $maybeLookup := (lookup "v1" "Ingress" "mynamespace" "ingressname") }}
          {{ if $maybeLookup }}
          {{   $ingress = (index $maybeLookup.status.loadBalancer.ingress 0).hostname }}
          {{ end }}