Kubernetes: Putting JSON value in YAML file

10,671

Solution 1

You can fix this by changing {{ .Values.dbCred | b64enc }} to {{ toJson .Values.dbCred | b64enc }}

Solution 2

Alternatively, you can keep your credentials.json file inside chart directory, and access file inside your template

data:
  credentials.json: {{ .Files.Get "credentials.json" | b64enc }}
Share:
10,671

Related videos on Youtube

Kamil Kamili
Author by

Kamil Kamili

Updated on June 04, 2022

Comments

  • Kamil Kamili
    Kamil Kamili almost 2 years

    I am trying to create a secret from a JSON value but I keep getting this error executing "secrets.yaml" at <b64enc>: wrong type for value; expected string; got map[string]interface {} when I do helm install.

    secrets.yaml

    apiVersion: v1
    kind: Secret
    metadata:
      name: cloudsql-instance-credentials
      namespace: wp-{{ .Values.name }}
      labels:
        app: wp-{{ .Values.name }}
    type: Opaque
    data:
      credentials.json: {{ .Values.dbCred | b64enc }}
    

    values.yaml

    dbCred: {
      'type': '',
      'project_id': '',
      'private_key_id': '',
      'private_key': '-----BEGIN PRIVATE KEY-----\n\n-----END PRIVATE KEY-----\n',
      'client_email': '',
      'client_id': '',
      'auth_uri': '',
      'token_uri': '',
      'auth_provider_x509_cert_url': '',
      'client_x509_cert_url': ''
    }
    
    • Ryan Dawson
      Ryan Dawson over 5 years
      Try toJson .Values.dbCred | b64enc. Or you could even try putting the values in json structure in the values.yaml as yaml and letting helm convert to json for you - stackoverflow.com/a/53342487/9705485
  • Ryan Dawson
    Ryan Dawson over 5 years
    I tried putting that secrets.yaml and values entry into an example chart generated with helm create and was able to recreate the error. I then made this change and found I then don't get the error
  • gmaghera
    gmaghera over 4 years
    So slick! Exactly what I was looking for.
  • Ryan Dawson
    Ryan Dawson over 3 years
    @O’Dane Brissett Bit confused by the question. This example discussed is a secrets.yaml
  • O'Dane Brissett
    O'Dane Brissett over 3 years
    I'm using octopus deploy to set the variable then substitute the values in my helm. However using the example above i get the error did not find expected key
  • Ryan Dawson
    Ryan Dawson over 3 years
    @O’Dane Brissett I'd suggest trying it outside of octopus, If that works then you know it's a problem at the octopus level. If it doesn't work then I'd suggest a new question on SO or an issue on the helm repo.