{{- $namespaces := dict "_0" .Release.Namespace -}}
{{- if and .Values.grafana.enabled .Values.grafana.defaultDashboardsEnabled (not .Values.grafana.defaultDashboards.useExistingNamespace) -}}
{{- $_ := set $namespaces "_1" .Values.grafana.defaultDashboards.namespace -}}
{{- end -}}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .Chart.Name }}-patch-sa
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ .Chart.Name }}-patch-sa
  annotations:
    "helm.sh/hook": post-install, post-upgrade
    "helm.sh/hook-delete-policy": hook-succeeded, before-hook-creation
spec:
  template:
    metadata:
      name: {{ .Chart.Name }}-patch-sa
      labels:
        app: {{ .Chart.Name }}-patch-sa
    spec:
      serviceAccountName: {{ .Chart.Name }}-patch-sa
      securityContext:
        runAsNonRoot: true
        runAsUser: 1000
      restartPolicy: Never
      nodeSelector: {{ include "linux-node-selector" . | nindent 8 }}
      tolerations: {{ include "linux-node-tolerations" . | nindent 8 }}
      containers:
      {{- range $_, $ns := $namespaces }}
      - name: patch-sa-{{ $ns }}
        image: {{ template "system_default_registry" $ }}{{ $.Values.global.kubectl.repository }}:{{ $.Values.global.kubectl.tag }}
        imagePullPolicy: {{ $.Values.global.kubectl.pullPolicy }}
        command: ["kubectl", "patch", "serviceaccount", "default", "-p", "{\"automountServiceAccountToken\": false}"]
        args: ["-n", "{{ $ns }}"]
      {{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: {{ .Chart.Name }}-patch-sa
  labels:
    app: {{ .Chart.Name }}-patch-sa
rules:
- apiGroups:
  - ""
  resources:
  - serviceaccounts
  verbs: ['get', 'patch']
{{- if .Values.global.cattle.psp.enabled }}
- apiGroups: ['policy']
  resources: ['podsecuritypolicies']
  verbs:     ['use']
  resourceNames:
  - {{ .Chart.Name }}-patch-sa
{{- end }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: {{ .Chart.Name }}-patch-sa
  labels:
    app: {{ .Chart.Name }}-patch-sa
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: {{ .Chart.Name }}-patch-sa
subjects:
- kind: ServiceAccount
  name: {{ .Chart.Name }}-patch-sa
  namespace: {{ .Release.Namespace }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ .Chart.Name }}-patch-sa
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ .Chart.Name }}-patch-sa
---
{{- if .Values.global.cattle.psp.enabled }}
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: {{ .Chart.Name }}-patch-sa
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ .Chart.Name }}-patch-sa
spec:
  privileged: false
  hostNetwork: false
  hostIPC: false
  hostPID: false
  runAsUser:
    rule: 'MustRunAsNonRoot'
  seLinux:
    rule: 'RunAsAny'
  supplementalGroups:
    rule: 'MustRunAs'
    ranges:
      - min: 1
        max: 65535
  fsGroup:
    rule: 'MustRunAs'
    ranges:
      - min: 1
        max: 65535
  readOnlyRootFilesystem: false
  volumes:
    - 'secret'
{{- range $_, $ns := $namespaces }}
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-allow-all
  namespace: {{ $ns }}
spec:
  podSelector: {}
  ingress:
  - {}
  egress:
  - {}
  policyTypes:
  - Ingress
  - Egress
{{- end }}
{{- end }}
---
{{- if .Values.hardened.k3s.networkPolicy.enabled }}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: rancher-monitoring-coredns-allow-all
  namespace: kube-system
spec:
  ingress:
  - {}
  egress:
  - {}
  policyTypes:
  - Ingress
  - Egress 
  podSelector:
    matchLabels:
      k8s-app: kube-dns
{{- end }}