Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

helm: 'lookup' function always returns empty map

The relevant docs: https://helm.sh/docs/chart_template_guide/functions_and_pipelines/#using-the-lookup-function

My helm version:

$ helm version
version.BuildInfo{Version:"v3.4.1", 
GitCommit:"c4e74854886b2efe3321e185578e6db9be0a6e29", 
GitTreeState:"dirty", GoVersion:"go1.15.4"}

Minimal example to reproduce:

  1. Create a new helm chart and install it.
    $ helm create my-chart
    $ helm install my-chart ./my-chart
    
  2. Create a simple ConfigMap.
    # my-chart/templates/configmap.yaml
    
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-configmap
    data:
      someKey: someValue
    
    Upgrade the existing chart so that the ConfigMap is applied.
    $ helm upgrade my-chart ./my-chart
    
  3. Confirm that the ConfigMap exists.
    $ kubectl -n default get configmap my-configmap
    
    Which returns as expected:
    NAME           DATA   AGE
    my-configmap   1      12m
    
  4. Try to use the lookup function to reference the existing ConfigMap.
    # my-chart/templates/configmap.yaml
    
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-configmap
    data:
      someKey: someValue
      someOtherKey: {{ (lookup "v1" "ConfigMap" "default" "my-configmap").data.someValue }}
    
    Then do a dry-run of the upgrade.
    $ helm upgrade my-chart ./my-chart --dry-run
    
    You will be met with a nil pointer error:
    Error: UPGRADE FAILED: template: my-chart/templates/configmap.yaml:9:54: executing "my-
    chart/templates/configmap.yaml" at <"my-configmap">: nil pointer evaluating interface 
    {}.someValue
    

What am I doing wrong?

like image 345
Cameron Hudson Avatar asked Mar 18 '26 17:03

Cameron Hudson


1 Answers

This is an expected behavior if you are using --dry-run flag.

From documentation

Keep in mind that Helm is not supposed to contact the Kubernetes API Server during a helm template or a helm install|update|delete|rollback --dry-run, so the lookup function will return an empty list (i.e. dict) in such a case.

like image 86
edbighead Avatar answered Mar 22 '26 08:03

edbighead