_utils.tpl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. {{/* vim: set filetype=mustache: */}}
  2. {{/*
  3. Print instructions to get a secret value.
  4. Usage:
  5. {{ include "common.utils.secret.getvalue" (dict "secret" "secret-name" "field" "secret-value-field" "context" $) }}
  6. */}}
  7. {{- define "common.utils.secret.getvalue" -}}
  8. {{- $varname := include "common.utils.fieldToEnvVar" . -}}
  9. export {{ $varname }}=$(kubectl get secret --namespace {{ include "common.names.namespace" .context | quote }} {{ .secret }} -o jsonpath="{.data.{{ .field }}}" | base64 -d)
  10. {{- end -}}
  11. {{/*
  12. Build env var name given a field
  13. Usage:
  14. {{ include "common.utils.fieldToEnvVar" dict "field" "my-password" }}
  15. */}}
  16. {{- define "common.utils.fieldToEnvVar" -}}
  17. {{- $fieldNameSplit := splitList "-" .field -}}
  18. {{- $upperCaseFieldNameSplit := list -}}
  19. {{- range $fieldNameSplit -}}
  20. {{- $upperCaseFieldNameSplit = append $upperCaseFieldNameSplit ( upper . ) -}}
  21. {{- end -}}
  22. {{ join "_" $upperCaseFieldNameSplit }}
  23. {{- end -}}
  24. {{/*
  25. Gets a value from .Values given
  26. Usage:
  27. {{ include "common.utils.getValueFromKey" (dict "key" "path.to.key" "context" $) }}
  28. */}}
  29. {{- define "common.utils.getValueFromKey" -}}
  30. {{- $splitKey := splitList "." .key -}}
  31. {{- $value := "" -}}
  32. {{- $latestObj := $.context.Values -}}
  33. {{- range $splitKey -}}
  34. {{- if not $latestObj -}}
  35. {{- printf "please review the entire path of '%s' exists in values" $.key | fail -}}
  36. {{- end -}}
  37. {{- $value = ( index $latestObj . ) -}}
  38. {{- $latestObj = $value -}}
  39. {{- end -}}
  40. {{- printf "%v" (default "" $value) -}}
  41. {{- end -}}
  42. {{/*
  43. Returns first .Values key with a defined value or first of the list if all non-defined
  44. Usage:
  45. {{ include "common.utils.getKeyFromList" (dict "keys" (list "path.to.key1" "path.to.key2") "context" $) }}
  46. */}}
  47. {{- define "common.utils.getKeyFromList" -}}
  48. {{- $key := first .keys -}}
  49. {{- $reverseKeys := reverse .keys }}
  50. {{- range $reverseKeys }}
  51. {{- $value := include "common.utils.getValueFromKey" (dict "key" . "context" $.context ) }}
  52. {{- if $value -}}
  53. {{- $key = . }}
  54. {{- end -}}
  55. {{- end -}}
  56. {{- printf "%s" $key -}}
  57. {{- end -}}