0

I am writing a helm template for external secrets.

The secrets are in AWS secrets manager, and they're prefixed with "dev" or "prod".

Namespaces are used to differentiate environments, but there are many more than "dev" or "prod" -- "prod" and "prod-phi" should use "prod" secrets, while everything else should use "dev" secrets.

To accomplish that, I have a pretty long expression, which you can see prefixing a secret name here:

    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ or (eq .Release.Namespace "prod") (eq .Release.Namespace "prod-phi") | ternary "prod" "dev" }}/rds/data/primary
        property: username

I am repeating that expression "{{ or (eq .Release.Namespace "prod") (eq .Release.Namespace "prod-phi") | ternary "prod" "dev" }}" many times throughout this file, and it's affecting readability.

Is there a way to set a variable one time in the file using this expression, then reference that instead?

Something like:

variables:
  prefix: {{ or (eq .Release.Namespace "prod") (eq .Release.Namespace "prod-phi") | ternary "prod" "dev" }}

spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ $prefix }}/rds/data/primary
        property: username

1 Answer 1

1

You have couple options two that come to my mind:

  • use variable
  • use helper function

Variable

On top of the file declare a variable and then just use it:

{{- $prefix := (or (eq .Release.Namespace "prod") (eq .Release.Namespace "prod-phi") | ternary "prod" "dev") -}}
...
spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ $prefix }}/rds/data/primary

Helper function

make a file _helpers.tpl

{{/*
Environment prefix.
*/}}
{{- define "CHART-NAME.prefix" -}}
{{- (or (eq .Release.Namespace "prod") (eq .Release.Namespace "prod-phi") | ternary "prod" "dev") -}}
{{- end -}}

and use it:

spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ include "CHART-NAME.prefix" . }}/rds/data/primary

Note: If you are using the prefix in multiple files, you can use both "methods" in combination.

{{- $prefix := include "CHART-NAME.prefix" . -}}
... 
spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ $prefix }}/rds/data/primary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.