_getServiceAccounts.tpl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. {{/* Returns Service Account List for rbac */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.lib.rbac.serviceAccount" (dict "rootCtx" $ "objectData" $objectData) }}
  4. rootCtx: The root context of the chart.
  5. objectData: The object data to be used to render the RBAC.
  6. */}}
  7. {{/* Parses service accounts, and checks if RBAC have selected any of them */}}
  8. {{- define "ix.v1.common.lib.rbac.serviceAccount" -}}
  9. {{- $rootCtx := .rootCtx -}}
  10. {{- $objectData := .objectData -}}
  11. {{- $serviceAccounts := list -}}
  12. {{- range $name, $serviceAccount := $rootCtx.Values.serviceAccount -}}
  13. {{- $saName := include "ix.v1.common.lib.chart.names.fullname" $rootCtx -}}
  14. {{- if $serviceAccount.enabled -}}
  15. {{- if not $serviceAccount.primary -}}
  16. {{- $saName = (printf "%s-%s" (include "ix.v1.common.lib.chart.names.fullname" $rootCtx) $name) -}}
  17. {{- end -}}
  18. {{/* If allServiceAccounts is true */}}
  19. {{- if $objectData.allServiceAccounts -}}
  20. {{- $serviceAccounts = mustAppend $serviceAccounts $saName -}}
  21. {{/* Else if serviceAccounts is a list */}}
  22. {{- else if (kindIs "slice" $objectData.serviceAccounts) -}}
  23. {{- if (mustHas $name $objectData.serviceAccounts) -}}
  24. {{- $serviceAccounts = mustAppend $serviceAccounts $saName -}}
  25. {{- end -}}
  26. {{/* If not "allServiceAccounts" or "serviceAccounts", assign the primary service account to rbac */}}
  27. {{- else if $serviceAccount.primary -}}
  28. {{- if $objectData.primary -}}
  29. {{- $serviceAccounts = mustAppend $serviceAccounts $saName -}}
  30. {{- end -}}
  31. {{- end -}}
  32. {{- end -}}
  33. {{- end -}}
  34. {{- if not $serviceAccounts -}}
  35. {{- fail "RBAC - Expected at least one serviceAccount to be assigned. Assign one using [allServiceAccounts (boolean), serviceAccounts (list)]" -}}
  36. {{- end -}}
  37. {{- range $serviceAccounts }}
  38. - kind: ServiceAccount
  39. name: {{ . }}
  40. namespace: {{ $rootCtx.Release.Namespace }}
  41. {{- end -}}
  42. {{- end -}}