apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "jenkins-operator.fullname" . }}
  labels:
{{ include "jenkins-operator.labels" . | indent 4 }}
spec:
  replicas: {{ .Values.operator.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "jenkins-operator.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "jenkins-operator.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      serviceAccountName: jenkins-operator
    {{- with .Values.operator.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: {{ .Values.operator.image }}
          imagePullPolicy: {{ .Values.operator.imagePullPolicy }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          command:
            - /manager
          args:
          {{- if .Values.webhook.enabled }}
          - --validate-security-warnings
          {{- end }}
          {{- if .Values.webhook.enabled }}
          volumeMounts:
          - mountPath: /tmp/k8s-webhook-server/serving-certs
            name: webhook-certs
            readOnly: true
          {{- end }}
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8081
            initialDelaySeconds: 15
            periodSeconds: 20
          readinessProbe:
            httpGet:
              path: /readyz
              port: 8081
            initialDelaySeconds: 5
            periodSeconds: 10
          env:
            - name: WATCH_NAMESPACE
              {{- if .Values.jenkins.enabled }}
              value: {{ .Values.jenkins.namespace }}
              {{- else if .Values.operator.watchNamespace }}
              value: {{ .Values.operator.watchNamespace }}
              {{- else }}
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
              {{- end }}
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: OPERATOR_NAME
              value: "jenkins-operator"
          resources:
            {{- toYaml .Values.operator.resources | nindent 12 }}
      {{- with .Values.operator.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.operator.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.operator.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- if .Values.webhook.enabled }}
      volumes:
      - name: webhook-certs
        secret:
          defaultMode: 420
          secretName: jenkins-{{ .Values.webhook.certificate.name }}
      terminationGracePeriodSeconds: 10
    {{- end }}
