Helm Charts create secrets in different namespace

11/25/2020

I have the following secrets.yaml in templetes in Helm Charts:

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  USER_NAME: YWRtaW4=
  PASSWORD: MWYyZDFlMmU2N2Rm 

I need to create the same secret in different namespace, for example, namespace test1, test2, test3, test4, how to specify the different namespace with the same secrets so the same secret can be created in different namespace?

-- ratzip
kubernetes
kubernetes-helm

1 Answer

11/26/2020

You can set the namespace name in the metadata section like

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
  namespace: test1
type: Opaque
data:
  USER_NAME: YWRtaW4=
  PASSWORD: MWYyZDFlMmU2N2Rm

You can set a for loop with helm to create one Secret definition in each namespace.

Update.

# values.yaml
namespaces:
  - test1
  - test2

# templates.secrets.tpl
{{- range .Values.namespaces }}
---
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
  namespace: {{ . | quote }}
type: Opaque
data:
  USER_NAME: YWRtaW4=
  PASSWORD: MWYyZDFlMmU2N2Rm

{{- end }}


### output ###
---
# Source: base/templates/secrets.tpl

---
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
  namespace: "test1"
type: Opaque
data:
  USER_NAME: YWRtaW4=
  PASSWORD: MWYyZDFlMmU2N2Rm
---
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
  namespace: "test2"
type: Opaque
data:
  USER_NAME: YWRtaW4=
  PASSWORD: MWYyZDFlMmU2N2Rm
-- Christian Daniel Avila Sanchez
Source: StackOverflow