_serviceAccountName.tpl 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. {{/* Returns Service Account Name */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.lib.pod.serviceAccountName" (dict "rootCtx" $ "objectData" $objectData) }}
  4. rootCtx: The root context of the chart.
  5. objectData: The object data to be used to render the Pod.
  6. */}}
  7. {{- define "ix.v1.common.lib.pod.serviceAccountName" -}}
  8. {{- $rootCtx := .rootCtx -}}
  9. {{- $objectData := .objectData -}}
  10. {{- $saName := "default" -}}
  11. {{- $saNameCount := 0 -}}
  12. {{- range $name, $serviceAccount := $rootCtx.Values.serviceAccount -}}
  13. {{- $tempName := include "ix.v1.common.lib.chart.names.fullname" $rootCtx -}}
  14. {{- if not $serviceAccount.primary -}}
  15. {{- $tempName = (printf "%s-%s" $tempName $name) -}}
  16. {{- end -}}
  17. {{- if $serviceAccount.enabled -}}
  18. {{/* If targetSelectAll is true */}}
  19. {{- if $serviceAccount.targetSelectAll -}}
  20. {{- $saName = $tempName -}}
  21. {{- $saNameCount = add1 $saNameCount -}}
  22. {{/* Else if targetSelector is a list */}}
  23. {{- else if (kindIs "slice" $serviceAccount.targetSelector) -}}
  24. {{- if (mustHas $objectData.shortName $serviceAccount.targetSelector) -}}
  25. {{- $saName = $tempName -}}
  26. {{- $saNameCount = add1 $saNameCount -}}
  27. {{- end -}}
  28. {{/* If not targetSelectAll or targetSelector, but is the primary pod */}}
  29. {{- else if $objectData.primary -}}
  30. {{- $saName = $tempName -}}
  31. {{- $saNameCount = add1 $saNameCount -}}
  32. {{- end -}}
  33. {{- end -}}
  34. {{- end -}}
  35. {{- if gt $saNameCount 1 -}}
  36. {{- fail (printf "Expected at most 1 ServiceAccount to be assigned on a pod [%s]. But [%v] were assigned" $objectData.shortName $saNameCount) -}}
  37. {{- end -}}
  38. {{- $saName -}}
  39. {{- end -}}