0

I have the following values.yaml file

# Default region and repository
aws_region: us-east-1
repository: 01234567890.dkr.ecr.us-east-1.amazonaws.com

repositories:
  us-east-1: 01234567890.dkr.ecr.us-east-1.amazonaws.com
  eu-north-1: 98765432109.dkr.ecr.eu-north-1.amazonaws.com

image:
  name: "ms-0"
  ...

I wrote a function to return the value from the repositories dictionary based on the key, which is an AWS region.

{{/*
Get the repository from the AWS region
*/}}
{{- define "microservice-base-chart.reponame" -}}
{{- $repo := default .Values.repository }}
{{- range $key, $value := .Values.repositories }}
{{- if .eq $key .Values.aws_region }}
{{- $repo = $value }}
{{- end }}
{{- end }}
{{- printf "%s" $repo }}
{{- end }}

And then I want to use the function in, say my deployment.yaml template

apiVersion: apps/v1
kind: Deployment
...
spec:
  ...
  template:
  ...
    spec:
    ...
    containers:
    - name: {{ .Values.image.name }}
      image: {{ include "microservice-base-chart.reponame" . }}/{{ .Values.image.name }}:{{ .Values.image.version }}

But when I do

$ helm install ms-0 ./microservice-chart/ --dry-run --debug

I get

install.go:178: [debug] Original chart version: ""
install.go:195: [debug] CHART PATH: /path/helm/microservice-chart

Error: INSTALLATION FAILED: template: microservice-chart/charts/microservice-0/templates/deployment.yaml:36:20: executing "microservice-chart/charts/microservice-0/templates/deployment.yaml" at <include "microservice-base-chart.reponame" .>: error calling include: template: microservice-chart/charts/microservice-0/templates/_helpers.tpl:70:7: executing "microservice-base-chart.reponame" at <.eq>: can't evaluate field eq in type interface {}
helm.go:84: [debug] template: microservice-chart/charts/microservice-0/templates/deployment.yaml:36:20: executing "microservice-chart/charts/microservice-0/templates/deployment.yaml" at <include "microservice-base-chart.reponame" .>: error calling include: template: microservice-chart/charts/microservice-0/templates/_helpers.tpl:70:7: executing "microservice-base-chart.reponame" at <.eq>: can't evaluate field eq in type interface {}
INSTALLATION FAILED
main.newInstallCmd.func2
    helm.sh/helm/v3/cmd/helm/install.go:127
github.com/spf13/cobra.(*Command).execute
    github.com/spf13/cobra@v1.4.0/command.go:856
github.com/spf13/cobra.(*Command).ExecuteC
    github.com/spf13/cobra@v1.4.0/command.go:974
github.com/spf13/cobra.(*Command).Execute
    github.com/spf13/cobra@v1.4.0/command.go:902
main.main
    helm.sh/helm/v3/cmd/helm/helm.go:83
runtime.main
    runtime/proc.go:250
runtime.goexit
    runtime/asm_amd64.s:1594

What am I doing wrong? TIA!

Chris F
  • 14,337
  • 30
  • 94
  • 192
  • 1
    Are you trying to [Access a map value using a variable key in a Go template](https://stackoverflow.com/questions/26152088/access-a-map-value-using-a-variable-key-in-a-go-template); `index .Values.repositories .Values.aws_region`? – David Maze Feb 13 '23 at 01:08
  • @DavidMaze, if you put this in an answer I'll accept it. Thanks! – Chris F Feb 14 '23 at 17:10

0 Answers0