2

I don't know Golang at all but need to implement Go template syntax in my kubernetes config (where hishicorp vault is configured). What I'm trying to do is to modify file in order to change its format. So source looks like this:

data: map[key1:value1]
metadata: map[created_time:2021-10-06T21:02:18.41643371Z deletion_time: destroyed:false version:1]

The Kubernetes config part with go template is used in order to format file is here:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  replicas: ${REPLICAS}
  selector:
    matchLabels:
      component: test
  template:
    metadata:
      labels:
        component: test
      annotations:
        vault.hashicorp.com/agent-inject: 'true'
        vault.hashicorp.com/agent-inject-status: 'update'
        vault.hashicorp.com/role: 'test'
        vault.hashicorp.com/agent-inject-secret-config: 'secret/data/test/config'
        vault.hashicorp.com/agent-inject-template-config: |
          {{- with secret "secret/data/test/config" -}}
          {{ range $k, $v := .Data.data }}
          export {{ $k }}={{ $v | quote }}
          {{ end }}
          {{- end}}
    spec:
      serviceAccountName: test
      containers:
        - name: test
          image: ${IMAGE}
          ports:
            - containerPort: 3000

But the error I'm getting is:

runtime error encountered: error="template server: (dynamic): parse: template: :2: unexpected "," in range"

EDIT:

  • To deploy vault on k8s I'm using vault helm chart
Murakami
  • 3,474
  • 7
  • 35
  • 89

1 Answers1

0

For what I can see you have env variables in your yaml file (${REPLICAS}, ${IMAGE}), which makes me think that you are using something like cat file.yml | envsubst | kubectl apply --wait=true -f - in order to replace those env vars for the real values.

The issue with this is that $k and $v are also being replaced for '' (since you do not have that env var in your system).

One ugly but effective solution is to export v="$v" and export k="$k" which whill generate your yaml file correctly.