_fixedEnv.tpl 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. {{/* Returns Fixed Env */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.lib.container.fixedEnv" (dict "rootCtx" $ "objectData" $objectData) }}
  4. rootCtx: The root context of the chart.
  5. objectData: The object data to be used to render the container.
  6. */}}
  7. {{- define "ix.v1.common.lib.container.fixedEnv" -}}
  8. {{- $rootCtx := .rootCtx -}}
  9. {{- $objectData := .objectData -}}
  10. {{/* Avoid nil pointers */}}
  11. {{- if not (hasKey $objectData "fixedEnv") -}}
  12. {{- $_ := set $objectData "fixedEnv" dict -}}
  13. {{- end -}}
  14. {{- $nvidiaCaps := $rootCtx.Values.resources.NVIDIA_CAPS -}}
  15. {{- if $objectData.fixedEnv.NVIDIA_CAPS -}}
  16. {{- $nvidiaCaps = $objectData.fixedEnv.NVIDIA_CAPS -}}
  17. {{- end -}}
  18. {{- if not (deepEqual $nvidiaCaps (mustUniq $nvidiaCaps)) -}}
  19. {{- fail (printf "Container - Expected <fixedEnv.NVIDIA_CAPS> to have only unique values, but got [%s]" (join ", " $nvidiaCaps)) -}}
  20. {{- end -}}
  21. {{- $caps := (list "all" "compute" "utility" "graphics" "video") -}}
  22. {{- range $cap := $nvidiaCaps -}}
  23. {{- if not (mustHas $cap $caps) -}}
  24. {{- fail (printf "Container - Expected <fixedEnv.NVIDIA_CAPS> entry to be one of [%s], but got [%s]" (join ", " $caps) $cap) -}}
  25. {{- end -}}
  26. {{- end -}}
  27. {{- $secContext := fromJson (include "ix.v1.common.lib.container.securityContext.calculate" (dict "rootCtx" $rootCtx "objectData" $objectData)) -}}
  28. {{- $fixed := list -}}
  29. {{- $TZ := $objectData.fixedEnv.TZ | default $rootCtx.Values.TZ -}}
  30. {{- $UMASK := $objectData.fixedEnv.UMASK | default $rootCtx.Values.securityContext.container.UMASK -}}
  31. {{- $PUID := $objectData.fixedEnv.PUID | default $rootCtx.Values.securityContext.container.PUID -}}
  32. {{- if and (not (kindIs "invalid" $objectData.fixedEnv.PUID)) (eq (int $objectData.fixedEnv.PUID) 0) -}}
  33. {{- $PUID = $objectData.fixedEnv.PUID -}}
  34. {{- end -}}
  35. {{/* calculatedFSGroup is passed from the pod */}}
  36. {{- $PGID := $objectData.calculatedFSGroup -}}
  37. {{- $fixed = mustAppend $fixed (dict "k" "TZ" "v" $TZ) -}}
  38. {{- $fixed = mustAppend $fixed (dict "k" "UMASK" "v" $UMASK) -}}
  39. {{- $fixed = mustAppend $fixed (dict "k" "UMASK_SET" "v" $UMASK) -}}
  40. {{- if eq (include "ix.v1.common.lib.container.resources.gpu" (dict "rootCtx" $rootCtx "objectData" $objectData "returnBool" true)) "true" -}}
  41. {{- $fixed = mustAppend $fixed (dict "k" "NVIDIA_DRIVER_CAPABILITIES" "v" (join "," $nvidiaCaps)) -}}
  42. {{- else -}}
  43. {{- $fixed = mustAppend $fixed (dict "k" "NVIDIA_VISIBLE_DEVICES" "v" "void") -}}
  44. {{- end -}}
  45. {{/* If running as root and PUID is set (0 or greater), set related envs */}}
  46. {{- if and (or (eq (int $secContext.runAsUser) 0) (eq (int $secContext.runAsGroup) 0)) (ge (int $PUID) 0) -}}
  47. {{- $fixed = mustAppend $fixed (dict "k" "PUID" "v" $PUID) -}}
  48. {{- $fixed = mustAppend $fixed (dict "k" "USER_ID" "v" $PUID) -}}
  49. {{- $fixed = mustAppend $fixed (dict "k" "UID" "v" $PUID) -}}
  50. {{- $fixed = mustAppend $fixed (dict "k" "PGID" "v" $PGID) -}}
  51. {{- $fixed = mustAppend $fixed (dict "k" "GROUP_ID" "v" $PGID) -}}
  52. {{- $fixed = mustAppend $fixed (dict "k" "GID" "v" $PGID) -}}
  53. {{- end -}}
  54. {{/* If rootFS is readOnly OR does not as root, let s6 containers to know that fs is readonly */}}
  55. {{- if or $secContext.readOnlyRootFilesystem $secContext.runAsNonRoot -}}
  56. {{- $fixed = mustAppend $fixed (dict "k" "S6_READ_ONLY_ROOT" "v" "1") -}}
  57. {{- end -}}
  58. {{- range $env := $fixed -}}
  59. {{- include "ix.v1.common.helper.container.envDupeCheck" (dict "rootCtx" $rootCtx "objectData" $objectData "source" "fixedEnv" "key" $env.k) }}
  60. - name: {{ $env.k | quote }}
  61. value: {{ $env.v | quote }}
  62. {{- end -}}
  63. {{- end -}}