{{- if and (ne .Values.adminService.scheme "http") (ne .Values.adminService.scheme "https") -}}
{{ fail "invalid scheme: must be http or https" }}
{{- end -}}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "hydra-maester.fullname" . }}
  labels:
    {{- include "hydra-maester.labels" . | nindent 4 }}
    {{- with .Values.deployment.extraLabels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  selector:
    matchLabels:
      control-plane: controller-manager
      app.kubernetes.io/name: {{ include "hydra-maester.fullname" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        control-plane: controller-manager
        app.kubernetes.io/name: {{ include "hydra-maester.fullname" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        {{- with .Values.deployment.extraLabels }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.deployment.podMetadata.labels }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
      annotations:
        {{- with .Values.deployment.extraAnnotations }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.deployment.podMetadata.annotations }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        {{- if .Values.deployment.extraVolumes }}
          {{- toYaml .Values.deployment.extraVolumes | nindent 8 }}
        {{- end }}
      terminationGracePeriodSeconds: {{ .Values.deployment.terminationGracePeriodSeconds }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /manager
          args:
            - --metrics-addr=127.0.0.1:8080
            - --hydra-url={{ required "scheme is required" .Values.adminService.scheme }}://{{ .Values.adminService.name | default ( include "hydra-maester.adminService" . ) }}
            - --hydra-port={{ required "port must be set and non-empty" .Values.adminService.port }}
            {{- with .Values.adminService.endpoint }}
            - --endpoint={{ . }}
            {{- end }}
            {{- if .Values.forwardedProto }}
            - --forwarded-proto={{ .Values.forwardedProto }}
            {{- end }}
            {{- if .Values.singleNamespaceMode }}
            - --namespace={{ .Release.Namespace }}
            {{- end }}
            {{- if .Values.deployment.args.syncPeriod }}
            - --sync-period={{ .Values.deployment.args.syncPeriod }}
            {{- end }}
            {{- if .Values.adminService.insecureSkipVerify }}
            - --insecure-skip-verify={{ .Values.adminService.insecureSkipVerify }}
            {{- end}}
            {{- if .Values.adminService.tlsTrustStorePath }}
            - --tls-trust-store={{ .Values.adminService.tlsTrustStorePath }}
            {{- end }}
          volumeMounts:
            {{- if .Values.deployment.extraVolumeMounts }}
              {{- toYaml .Values.deployment.extraVolumeMounts | nindent 12 }}
            {{- end }}
          {{- if .Values.deployment.extraEnv }}
          env:
            {{- tpl (toYaml .Values.deployment.extraEnv) . | nindent 12 }}
          {{- end }}
          resources:
            {{- toYaml .Values.deployment.resources | nindent 12 }}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          {{- if .Values.deployment.securityContext }}
          securityContext:
            {{- toYaml .Values.deployment.securityContext | nindent 12 }}
          {{- end }}
      serviceAccountName: {{ include "hydra-maester.fullname" . }}-account
      automountServiceAccountToken: {{ .Values.deployment.automountServiceAccountToken }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
      {{- end }}
      nodeSelector:
      {{- with .Values.deployment.nodeSelector }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.deployment.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.deployment.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.deployment.dnsConfig }}
      dnsConfig:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.deployment.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
    {{- end }}
