_utils.tpl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. {{/*
  2. Copyright Broadcom, Inc. All Rights Reserved.
  3. SPDX-License-Identifier: APACHE-2.0
  4. */}}
  5. {{/* vim: set filetype=mustache: */}}
  6. {{/*
  7. Print instructions to get a secret value.
  8. Usage:
  9. {{ include "common.utils.secret.getvalue" (dict "secret" "secret-name" "field" "secret-value-field" "context" $) }}
  10. */}}
  11. {{- define "common.utils.secret.getvalue" -}}
  12. {{- $varname := include "common.utils.fieldToEnvVar" . -}}
  13. export {{ $varname }}=$(kubectl get secret --namespace {{ include "common.names.namespace" .context | quote }} {{ .secret }} -o jsonpath="{.data.{{ .field }}}" | base64 -d)
  14. {{- end -}}
  15. {{/*
  16. Build env var name given a field
  17. Usage:
  18. {{ include "common.utils.fieldToEnvVar" dict "field" "my-password" }}
  19. */}}
  20. {{- define "common.utils.fieldToEnvVar" -}}
  21. {{- $fieldNameSplit := splitList "-" .field -}}
  22. {{- $upperCaseFieldNameSplit := list -}}
  23. {{- range $fieldNameSplit -}}
  24. {{- $upperCaseFieldNameSplit = append $upperCaseFieldNameSplit ( upper . ) -}}
  25. {{- end -}}
  26. {{ join "_" $upperCaseFieldNameSplit }}
  27. {{- end -}}
  28. {{/*
  29. Gets a value from .Values given
  30. Usage:
  31. {{ include "common.utils.getValueFromKey" (dict "key" "path.to.key" "context" $) }}
  32. */}}
  33. {{- define "common.utils.getValueFromKey" -}}
  34. {{- $splitKey := splitList "." .key -}}
  35. {{- $value := "" -}}
  36. {{- $latestObj := $.context.Values -}}
  37. {{- range $splitKey -}}
  38. {{- if not $latestObj -}}
  39. {{- printf "please review the entire path of '%s' exists in values" $.key | fail -}}
  40. {{- end -}}
  41. {{- $value = ( index $latestObj . ) -}}
  42. {{- $latestObj = $value -}}
  43. {{- end -}}
  44. {{- printf "%v" (default "" $value) -}}
  45. {{- end -}}
  46. {{/*
  47. Returns first .Values key with a defined value or first of the list if all non-defined
  48. Usage:
  49. {{ include "common.utils.getKeyFromList" (dict "keys" (list "path.to.key1" "path.to.key2") "context" $) }}
  50. */}}
  51. {{- define "common.utils.getKeyFromList" -}}
  52. {{- $key := first .keys -}}
  53. {{- $reverseKeys := reverse .keys }}
  54. {{- range $reverseKeys }}
  55. {{- $value := include "common.utils.getValueFromKey" (dict "key" . "context" $.context ) }}
  56. {{- if $value -}}
  57. {{- $key = . }}
  58. {{- end -}}
  59. {{- end -}}
  60. {{- printf "%s" $key -}}
  61. {{- end -}}
  62. {{/*
  63. Checksum a template at "path" containing a *single* resource (ConfigMap,Secret) for use in pod annotations, excluding the metadata (see #18376).
  64. Usage:
  65. {{ include "common.utils.checksumTemplate" (dict "path" "/configmap.yaml" "context" $) }}
  66. */}}
  67. {{- define "common.utils.checksumTemplate" -}}
  68. {{- $obj := include (print .context.Template.BasePath .path) .context | fromYaml -}}
  69. {{ omit $obj "apiVersion" "kind" "metadata" | toYaml | sha256sum }}
  70. {{- end -}}