_podSecurityContext.tpl 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 pods 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 pods 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 isnt 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 107)) -}}
  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. {{- if ne (include "ix.v1.common.lib.pod.hostNetwork" (dict "rootCtx" $rootCtx "objectData" $objectData)) "true" -}}
  41. {{- $_ := set $secContext "sysctls" (mustAppend $secContext.sysctls (dict "name" "net.ipv4.ip_unprivileged_port_start" "value" (printf "%v" $portRange.low))) -}}
  42. {{- end -}}
  43. {{- end -}}
  44. {{- if or (kindIs "invalid" $secContext.fsGroup) (eq (toString $secContext.fsGroup) "") -}}
  45. {{- fail "Pod - Expected non-empty <fsGroup>" -}}
  46. {{- end -}}
  47. {{/* Used by the fixedEnv template */}}
  48. {{- $_ := set $objectData.podSpec "calculatedFSGroup" $secContext.fsGroup -}}
  49. {{- if not $secContext.fsGroupChangePolicy -}}
  50. {{- fail "Pod - Expected non-empty <fsGroupChangePolicy>" -}}
  51. {{- end -}}
  52. {{- $policies := (list "Always" "OnRootMismatch") -}}
  53. {{- if not (mustHas $secContext.fsGroupChangePolicy $policies) -}}
  54. {{- fail (printf "Pod - Expected <fsGroupChangePolicy> to be one of [%s], but got [%s]" (join ", " $policies) $secContext.fsGroupChangePolicy) -}}
  55. {{- end }}
  56. fsGroup: {{ include "ix.v1.common.helper.makeIntOrNoop" $secContext.fsGroup }}
  57. fsGroupChangePolicy: {{ $secContext.fsGroupChangePolicy }}
  58. {{- with $secContext.supplementalGroups }}
  59. supplementalGroups:
  60. {{- range . }}
  61. - {{ include "ix.v1.common.helper.makeIntOrNoop" . }}
  62. {{- end -}}
  63. {{- else }}
  64. supplementalGroups: []
  65. {{- end -}}
  66. {{- with $secContext.sysctls }}
  67. sysctls:
  68. {{- range . }}
  69. {{- if not .name -}}
  70. {{- fail "Pod - Expected non-empty <name> in <sysctls>" -}}
  71. {{- end -}}
  72. {{- if not .value -}}
  73. {{- fail "Pod - Expected non-empty <value> in <sysctls>" -}}
  74. {{- end }}
  75. - name: {{ tpl .name $rootCtx | quote }}
  76. value: {{ tpl .value $rootCtx | quote }}
  77. {{- end -}}
  78. {{- else }}
  79. sysctls: []
  80. {{- end -}}
  81. {{- end -}}