apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "opa.fullname" . }}
  labels:
{{ include "opa.labels.standard" . | indent 4 }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ template "opa.fullname" . }}
  template:
    metadata:
      labels:
        app: {{ template "opa.fullname" . }}
      name: {{ template "opa.fullname" . }}
    spec:
{{- if .Values.authz.enabled }}
      initContainers:
        - name: initpolicy
          image: {{ .Values.mgmt.image }}:{{ .Values.mgmt.imageTag }}
          imagePullPolicy: {{ .Values.mgmt.imagePullPolicy }}
          command:
          - /bin/sh
          - -c
          - |
            tr -dc 'A-F0-9' < /dev/urandom | dd bs=1 count=32 2>/dev/null > /authz/mgmt-token
            TOKEN=`cat /authz/mgmt-token`
            cat > /authz/authz.rego <<EOF
            package system.authz
            default allow = false
            allow { input.path = [""]; input.method = "POST" }
            allow { input.path = [""]; input.method = "GET" }
            allow { input.identity = "$TOKEN" }
            EOF
          volumeMounts:
            - name: authz
              mountPath: /authz
{{- end }}
      containers:
        - name: opa
          image: {{ .Values.image }}:{{ .Values.imageTag }}
          imagePullPolicy: {{ .Values.imagePullPolicy }}
          args:
            - "run"
            - "--server"
            - "--config-file=/config/config.yaml"
            - "--tls-cert-file=/certs/tls.crt"
            - "--tls-private-key-file=/certs/tls.key"
            - "--addr=0.0.0.0:443"
{{- if .Values.authz.enabled }}
            - "--authentication=token"
            - "--authorization=basic"
            - "/authz/authz.rego"
            - "--ignore=.*"
{{- end }}
{{- if .Values.mgmt.enabled }}
            - "--insecure-addr=127.0.0.1:8181"
{{- end }}
          volumeMounts:
            - name: certs
              readOnly: true
              mountPath: /certs
            - name: config
              readOnly: true
              mountPath: /config
{{- if .Values.authz.enabled }}
            - name: authz
              readOnly: true
              mountPath: /authz
{{- end }}
{{- if .Values.mgmt.enabled }}
        - name: mgmt
          image: {{ .Values.mgmt.image }}:{{ .Values.mgmt.imageTag }}
          imagePullPolicy: {{ .Values.mgmt.imagePullPolicy }}
          args:
{{- if .Values.authz.enabled }}
            - --opa-auth-token-file=/authz/mgmt-token
{{- end }}
            - --opa-url=http://127.0.0.1:8181/v1
            - --replicate-path={{ .Values.mgmt.replicate.path }}
            - --enable-policies={{ .Values.mgmt.configmapPolicies.enabled }}
{{- if .Values.mgmt.configmapPolicies.enabled }}
            - --policies={{ .Values.mgmt.configmapPolicies.namespaces | join "," }}
            - --require-policy-label={{ .Values.mgmt.configmapPolicies.requireLabel }}
{{- end }}
{{- if gt (len .Values.mgmt.replicate.namespace) 0 }}
            - --replicate={{ .Values.mgmt.replicate.namespace | join ","}}
{{- end }}
{{- if gt (len .Values.mgmt.replicate.cluster) 0 }}
            - --replicate-cluster={{ .Values.mgmt.replicate.cluster | join ","}}
{{- end }}
{{- if .Values.authz.enabled }}
          volumeMounts:
            - name: authz
              readOnly: true
              mountPath: /authz
{{- end }}
{{- end }}
{{- if .Values.sar.enabled }}
        - name: sarproxy
          image: {{ .Values.sar.image }}:{{ .Values.sar.imageTag }}
          imagePullPolicy: {{ .Values.sar.imagePullPolicy }}
          command:
            - kubectl
            - proxy
            - --accept-paths=^/apis/authorization.k8s.io/v1/subjectaccessreviews$
{{- end }}
          readinessProbe:
{{ toYaml .Values.readinessProbe | indent 12 }}
          livenessProbe:
{{ toYaml .Values.livenessProbe | indent 12 }}
      serviceAccountName: {{ template "opa.serviceAccountName" .}}
      volumes:
        - name: certs
          secret:
            secretName: {{ template "opa.fullname" . }}-cert
        - name: config
          secret:
            secretName: {{ template "opa.fullname" . }}-config
{{- if .Values.authz.enabled }}
        - name: authz
          emptyDir: {}
{{- end }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
      resources:
{{ toYaml .Values.resources | indent 8 }}
