_compatibility.tpl 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. {{/*
  2. Copyright Broadcom, Inc. All Rights Reserved.
  3. SPDX-License-Identifier: APACHE-2.0
  4. */}}
  5. {{/* vim: set filetype=mustache: */}}
  6. {{/*
  7. Return true if the detected platform is Openshift
  8. Usage:
  9. {{- include "common.compatibility.isOpenshift" . -}}
  10. */}}
  11. {{- define "common.compatibility.isOpenshift" -}}
  12. {{- if .Capabilities.APIVersions.Has "security.openshift.io/v1" -}}
  13. {{- true -}}
  14. {{- end -}}
  15. {{- end -}}
  16. {{/*
  17. Render a compatible securityContext depending on the platform. By default it is maintained as it is. In other platforms like Openshift we remove default user/group values that do not work out of the box with the restricted-v1 SCC
  18. Usage:
  19. {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) -}}
  20. */}}
  21. {{- define "common.compatibility.renderSecurityContext" -}}
  22. {{- $adaptedContext := .secContext -}}
  23. {{- if (((.context.Values.global).compatibility).openshift) -}}
  24. {{- if or (eq .context.Values.global.compatibility.openshift.adaptSecurityContext "force") (and (eq .context.Values.global.compatibility.openshift.adaptSecurityContext "auto") (include "common.compatibility.isOpenshift" .context)) -}}
  25. {{/* Remove incompatible user/group values that do not work in Openshift out of the box */}}
  26. {{- $adaptedContext = omit $adaptedContext "fsGroup" "runAsUser" "runAsGroup" -}}
  27. {{- if not .secContext.seLinuxOptions -}}
  28. {{/* If it is an empty object, we remove it from the resulting context because it causes validation issues */}}
  29. {{- $adaptedContext = omit $adaptedContext "seLinuxOptions" -}}
  30. {{- end -}}
  31. {{- end -}}
  32. {{- end -}}
  33. {{/* Remove empty seLinuxOptions object if global.compatibility.omitEmptySeLinuxOptions is set to true */}}
  34. {{- if and (((.context.Values.global).compatibility).omitEmptySeLinuxOptions) (not .secContext.seLinuxOptions) -}}
  35. {{- $adaptedContext = omit $adaptedContext "seLinuxOptions" -}}
  36. {{- end -}}
  37. {{/* Remove fields that are disregarded when running the container in privileged mode */}}
  38. {{- if $adaptedContext.privileged -}}
  39. {{- $adaptedContext = omit $adaptedContext "capabilities" "seLinuxOptions" -}}
  40. {{- end -}}
  41. {{- omit $adaptedContext "enabled" | toYaml -}}
  42. {{- end -}}