{{- $pspAvailable := (semverCompare "<1.25-0" (include "common.capabilities.kubeVersion" .)) -}}
{{- if and $pspAvailable .Values.forwarder.enabled .Values.forwarder.rbac.create .Values.forwarder.rbac.pspEnabled }}
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: {{ printf "%s-forwarder" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: forwarder
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  privileged: false
  allowPrivilegeEscalation: false
  requiredDropCapabilities:
    - ALL
  allowedHostPaths:
    - pathPrefix: '/var/lib/docker/containers'
      readOnly: true
    - pathPrefix: '/var/log'
  volumes:
    - 'configMap'
    - 'emptyDir'
    - 'projected'
    - 'secret'
    - 'downwardAPI'
    - 'hostPath'
    - 'persistentVolumeClaim'
  hostNetwork: {{ .Values.forwarder.hostNetwork }}
  hostIPC: false
  hostPID: false
  runAsUser:
    {{- if eq (int .Values.forwarder.podSecurityContext.runAsUser) 0 }}
    rule: 'RunAsAny'
    {{- else }}
    rule: 'MustRunAs'
    ranges:
      - min: {{ .Values.forwarder.podSecurityContext.runAsUser }}
        max: {{ .Values.forwarder.podSecurityContext.runAsUser }}
    {{- end }}
  runAsGroup:
    {{- if eq (int .Values.forwarder.podSecurityContext.runAsGroup) 0 }}
    rule: 'RunAsAny'
    {{- else }}
    rule: 'MustRunAs'
    ranges:
      - min: {{ .Values.forwarder.podSecurityContext.runAsGroup }}
        max: {{ .Values.forwarder.podSecurityContext.runAsGroup }}
    {{- end }}
  seLinux:
    rule: 'RunAsAny'
  supplementalGroups:
    rule: 'MustRunAs'
    ranges:
      - min: 1
        max: 65535
  fsGroup:
    {{- if eq (int .Values.forwarder.podSecurityContext.fsGroup) 0 }}
    rule: 'RunAsAny'
    {{- else }}
    rule: 'MustRunAs'
    ranges:
      - min: {{ .Values.forwarder.podSecurityContext.fsGroup }}
        max: {{ .Values.forwarder.podSecurityContext.fsGroup }}
    {{- end }}
  readOnlyRootFilesystem: {{ .Values.forwarder.containerSecurityContext.readOnlyRootFilesystem }}
  {{- end }}
