_podSecurityContext.tpl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. {{/* Returns Pod Security Context */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.lib.pod.securityContext" (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.securityContext" -}}
  8. {{- $rootCtx := .rootCtx -}}
  9. {{- $objectData := .objectData -}}
  10. {{- if not $rootCtx.Values.securityContext.pod -}}
  11. {{- fail "Pod - Expected non-empty <.Values.securityContext.pod>" -}}
  12. {{- end -}}
  13. {{/* Initialize from the "global" option */}}
  14. {{- $secContext := mustDeepCopy $rootCtx.Values.securityContext.pod -}}
  15. {{/* Override with pod's option */}}
  16. {{- with $objectData.podSpec.securityContext -}}
  17. {{- $secContext = mustMergeOverwrite $secContext . -}}
  18. {{- end -}}
  19. {{/* TODO: Add supplemental groups
  20. devices (5, 10, 20, 24) (Only when devices is assigned on the pod's containers)
  21. TODO: Unit Test the above cases
  22. */}}
  23. {{- $gpuAdded := false -}}
  24. {{- range $GPUValues := $rootCtx.Values.scaleGPU -}}
  25. {{/* If there is a selector and pod is selected */}}
  26. {{- if $GPUValues.targetSelector -}}
  27. {{- if mustHas $objectData.shortName ($GPUValues.targetSelector | keys) -}}
  28. {{- $gpuAdded = true -}}
  29. {{- end -}}
  30. {{/* If there isn't a selector, but pod is primary */}}
  31. {{- else if $objectData.primary -}}
  32. {{- $gpuAdded = true -}}
  33. {{- end -}}
  34. {{- end -}}
  35. {{- if $gpuAdded -}}
  36. {{- $_ := set $secContext "supplementalGroups" (concat $secContext.supplementalGroups (list 44)) -}}
  37. {{- end -}}
  38. {{- $portRange := fromJson (include "ix.v1.common.lib.helpers.securityContext.getPortRange" (dict "rootCtx" $rootCtx "objectData" $objectData)) -}}
  39. {{- if and $portRange.low (le (int $portRange.low) 1024) -}} {{/* If a container wants to bind a port <= 1024 change the unprivileged_port_start */}}
  40. {{- $_ := set $secContext "sysctls" (mustAppend $secContext.sysctls (dict "name" "net.ipv4.ip_unprivileged_port_start" "value" (printf "%v" $portRange.low))) -}}
  41. {{- end -}}
  42. {{- if not $secContext.fsGroup -}}
  43. {{- fail "Pod - Expected non-empty <fsGroup>" -}}
  44. {{- end -}}
  45. {{/* Used by the fixedEnv template */}}
  46. {{- $_ := set $objectData.podSpec "calculatedFSGroup" $secContext.fsGroup -}}
  47. {{- if not $secContext.fsGroupChangePolicy -}}
  48. {{- fail "Pod - Expected non-empty <fsGroupChangePolicy>" -}}
  49. {{- end -}}
  50. {{- $policies := (list "Always" "OnRootMismatch") -}}
  51. {{- if not (mustHas $secContext.fsGroupChangePolicy $policies) -}}
  52. {{- fail (printf "Pod - Expected <fsGroupChangePolicy> to be one of [%s], but got [%s]" (join ", " $policies) $secContext.fsGroupChangePolicy) -}}
  53. {{- end }}
  54. fsGroup: {{ $secContext.fsGroup }}
  55. fsGroupChangePolicy: {{ $secContext.fsGroupChangePolicy }}
  56. {{- with $secContext.supplementalGroups }}
  57. supplementalGroups:
  58. {{- range . }}
  59. - {{ . }}
  60. {{- end -}}
  61. {{- else }}
  62. supplementalGroups: []
  63. {{- end -}}
  64. {{- with $secContext.sysctls }}
  65. sysctls:
  66. {{- range . }}
  67. {{- if not .name -}}
  68. {{- fail "Pod - Expected non-empty <name> in <sysctls>" -}}
  69. {{- end -}}
  70. {{- if not .value -}}
  71. {{- fail "Pod - Expected non-empty <value> in <sysctls>" -}}
  72. {{- end }}
  73. - name: {{ tpl .name $rootCtx | quote }}
  74. value: {{ tpl .value $rootCtx | quote }}
  75. {{- end -}}
  76. {{- else }}
  77. sysctls: []
  78. {{- end -}}
  79. {{- end -}}