{{- if .Values.webhook.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "snapshot-validation-webhook.fullname" . }}
  labels:
    {{- include "snapshot-validation-webhook.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.webhook.replicaCount }}
  revisionHistoryLimit: {{ .Values.webhook.revisionHistoryLimit }}
  selector:
    matchLabels:
      {{- include "snapshot-validation-webhook.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.webhook.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "snapshot-validation-webhook.selectorLabels" . | nindent 8 }}
        {{- with .Values.webhook.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.webhook.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "snapshot-validation-webhook.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.webhook.podSecurityContext | nindent 8 }}
      {{- with .Values.webhook.priorityClassName }}
      priorityClassName: {{ . }}
      {{- end }}
      containers:
        - name: {{ include "snapshot-validation-webhook.name" . }}
          securityContext:
            {{- toYaml .Values.webhook.securityContext | nindent 12 }}
          image: "{{ .Values.webhook.image.repository }}:{{ .Values.webhook.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.webhook.image.pullPolicy }}
          args:
            {{- range $flag, $val := .Values.webhook.args }}
            - --{{ $flag | kebabcase }}={{ $val }}
            {{- end }}
          ports:
            - name: https
              containerPort: {{ .Values.webhook.args.port | default 8443 }}
              protocol: TCP
          volumeMounts:
            - mountPath: /etc/snapshot-validation
              name: tls-config
          livenessProbe:
            httpGet:
              path: /readyz
              port: https
              scheme: HTTPS
          readinessProbe:
            httpGet:
              path: /readyz
              port: https
              scheme: HTTPS
          resources:
            {{- toYaml .Values.webhook.resources | nindent 12 }}
      volumes:
        - name: tls-config
          secret:
            secretName: {{ include "snapshot-validation-webhook.certifcateName" . }}
      {{- with .Values.webhook.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.webhook.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.webhook.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml .Values.webhook.topologySpreadConstraints | nindent 8 }}
      {{- end }}
      {{- with .Values.webhook.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      hostNetwork: {{ .Values.webhook.hostNetwork }}
      {{- with .Values.webhook.dnsConfig }}
      dnsConfig:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      dnsPolicy: {{ .Values.webhook.dnsPolicy }}
{{- end }}
