{{- if .Values.podSecurityPolicy.create -}}
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: {{ include "aws-calico.fullname" . }}-node
  labels:
    app.kubernetes.io/name: {{ include "aws-calico.fullname" . }}-node
{{ include "aws-calico.labels" . | indent 4 }}
spec:
  privileged: true
  allowPrivilegeEscalation: true
  requiredDropCapabilities:
    - ALL
  hostNetwork: true
  hostIPC: false
  hostPID: false
  volumes:
    - 'configMap'
    - 'emptyDir'
    - 'projected'
    - 'secret'
    - 'downwardAPI'
    - 'persistentVolumeClaim'
    - 'hostPath'
  allowedHostPaths:
    - pathPrefix: "/lib/modules"
      readOnly: false
    - pathPrefix: "/var/run/calico"
      readOnly: false
    - pathPrefix: "/var/lib/calico"
      readOnly: false
    - pathPrefix: "/run/xtables.lock"
      readOnly: false
  runAsUser:
    rule: 'RunAsAny'
  runAsGroup:
    rule: 'RunAsAny'
  seLinux:
    rule: 'RunAsAny'
  supplementalGroups:
    rule: 'RunAsAny'
  fsGroup:
    rule: 'MustRunAs'
    ranges:
      - min: 1
        max: 65535
  readOnlyRootFilesystem: false
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: {{ include "aws-calico.fullname" . }}-typha
  labels:
    app.kubernetes.io/name: {{ include "aws-calico.fullname" . }}-typha
{{ include "aws-calico.labels" . | indent 4 }}
spec:
  privileged: false
  allowPrivilegeEscalation: false
  requiredDropCapabilities:
    - ALL
  hostNetwork: true
  hostPorts:
  - max: 5473
    min: 5473
  hostIPC: false
  hostPID: false
  volumes:
    - 'configMap'
    - 'emptyDir'
    - 'projected'
    - 'secret'
    - 'downwardAPI'
    - 'persistentVolumeClaim'
  runAsUser:
    rule: 'RunAsAny'
  seLinux:
    rule: 'RunAsAny'
  supplementalGroups:
    rule: 'RunAsAny'
  fsGroup:
    rule: 'MustRunAs'
    ranges:
      - min: 1
        max: 65535
  readOnlyRootFilesystem: false
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: {{ include "aws-calico.fullname" . }}-typha-horizontal-autoscaler
  labels:
    app.kubernetes.io/name: {{ include "aws-calico.fullname" . }}-typha-autoscaler
{{ include "aws-calico.labels" . | indent 4 }}
spec:
  privileged: false
  allowPrivilegeEscalation: false
  requiredDropCapabilities:
    - ALL
  hostNetwork: false
  hostIPC: false
  hostPID: false
  volumes:
    - 'configMap'
    - 'emptyDir'
    - 'projected'
    - 'secret'
    - 'downwardAPI'
    - 'persistentVolumeClaim'
  runAsUser:
    rule: 'RunAsAny'
  seLinux:
    rule: 'RunAsAny'
  supplementalGroups:
    rule: 'RunAsAny'
  fsGroup:
    rule: 'MustRunAs'
    ranges:
      - min: 1
        max: 65535
  readOnlyRootFilesystem: false
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: {{ include "aws-calico.fullname" . }}-node-psp
  labels:
    app.kubernetes.io/name: {{ include "aws-calico.fullname" . }}-node
{{ include "aws-calico.labels" . | indent 4 }}
rules:
- apiGroups: ['policy']
  resources: ['podsecuritypolicies']
  verbs:     ['use']
  resourceNames:
  - {{ include "aws-calico.fullname" . }}-node
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: {{ include "aws-calico.fullname" . }}-typha-psp
  labels:
    app.kubernetes.io/name: {{ include "aws-calico.fullname" . }}-typha
{{ include "aws-calico.labels" . | indent 4 }}
rules:
- apiGroups: ['policy']
  resources: ['podsecuritypolicies']
  verbs:     ['use']
  resourceNames:
  - {{ include "aws-calico.fullname" . }}-typha
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: {{ include "aws-calico.fullname" . }}-typha-horizontal-autoscaler-psp
  labels:
    app.kubernetes.io/name: {{ include "aws-calico.fullname" . }}-typha-autoscaler
{{ include "aws-calico.labels" . | indent 4 }}
rules:
- apiGroups: ['policy']
  resources: ['podsecuritypolicies']
  verbs:     ['use']
  resourceNames:
  - {{ include "aws-calico.fullname" . }}-typha-horizontal-autoscaler
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ include "aws-calico.fullname" . }}-node-psp
  labels:
    app.kubernetes.io/name: {{ include "aws-calico.fullname" . }}-node
{{ include "aws-calico.labels" . | indent 4 }}
roleRef:
  kind: Role
  name: {{ include "aws-calico.fullname" . }}-node-psp
  apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
  name: {{ include "aws-calico.serviceAccountName" . }}-node
  namespace: {{ .Release.Namespace }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ include "aws-calico.fullname" . }}-typha-psp
  labels:
    app.kubernetes.io/name: {{ include "aws-calico.fullname" . }}-typha
{{ include "aws-calico.labels" . | indent 4 }}
roleRef:
  kind: Role
  name: {{ include "aws-calico.fullname" . }}-typha-psp
  apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
  name: {{ include "aws-calico.serviceAccountName" . }}-node
  namespace: {{ .Release.Namespace }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: {{ include "aws-calico.fullname" . }}-typha-horizontal-autoscaler-psp
  labels:
    app.kubernetes.io/name: {{ include "aws-calico.fullname" . }}-typha-autoscaler
{{ include "aws-calico.labels" . | indent 4 }}
roleRef:
  kind: Role
  name: {{ include "aws-calico.fullname" . }}-typha-horizontal-autoscaler-psp
  apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
  name: {{ include "aws-calico.serviceAccountName" . }}-typha-cpha
  namespace: {{ .Release.Namespace }}
{{- end }}