{{- if .Values.watcher.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "hydra.fullname" . }}-watcher
  {{- if .Release.Namespace }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  labels:
    app.kubernetes.io/name: {{ include "hydra.name" . }}-watcher
    app.kubernetes.io/instance: {{ .Release.Name }}
    {{- with .Values.deployment.labels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
  annotations:
    {{- with .Values.deployment.annotations }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  revisionHistoryLimit: {{ .Values.watcher.revisionHistoryLimit }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "hydra.name" . }}-watcher
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "hydra.name" . }}-watcher
        app.kubernetes.io/instance: {{ .Release.Name }}
        {{- with .Values.deployment.labels }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.watcher.podMetadata.labels }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
      annotations:
        {{- with .Values.watcher.podMetadata.annotations }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      automountServiceAccountToken: {{ .Values.watcher.automountServiceAccountToken }}
      serviceAccountName: {{ include "hydra.serviceAccountName" . }}-watcher
      terminationGracePeriodSeconds: {{ .Values.deployment.terminationGracePeriodSeconds }}
      containers:
        - name: watcher
          {{- with .Values.watcher.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          image: {{ .Values.watcher.image }}
          command:
            - /bin/bash
            - -c
            - |
              {{- .Files.Get "files/watch.sh" | printf "%s" | nindent 14 }}
          env:
            - name: NAMESPACE
              value: {{ .Release.Namespace | quote }}
            - name: WATCH_FILE
              value: {{ .Values.watcher.mountFile | quote }}
            - name: LABEL_SELECTOR
              value: '{{ $.Values.watcher.watchLabelKey }}={{ include "hydra.name" . }}'
          resources:
            {{- toYaml .Values.watcher.resources | nindent 12 }}
          volumeMounts:
          {{- with .Values.deployment.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.watcher.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        {{- if .Values.deployment.extraVolumes }}
          {{- toYaml .Values.deployment.extraVolumes | nindent 8 }}
        {{- end }}
{{- end }}
