apiVersion: apps/v1
kind: Deployment
metadata:
  name: "{{ template "harbor.jobservice" . }}"
  labels:
{{ include "harbor.labels" . | indent 4 }}
    component: jobservice
spec:
  replicas: {{ .Values.jobservice.replicas }}
  selector:
    matchLabels:
{{ include "harbor.matchLabels" . | indent 6 }}
      component: jobservice
  template:
    metadata:
      labels:
{{ include "harbor.labels" . | indent 8 }}
        component: jobservice
      annotations:
        checksum/configmap: {{ include (print $.Template.BasePath "/jobservice/jobservice-cm.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/jobservice/jobservice-secrets.yaml") . | sha256sum }}
        checksum/secret-core: {{ include (print $.Template.BasePath "/core/core-secret.yaml") . | sha256sum }}
{{- if .Values.jobservice.podAnnotations }}
{{ toYaml .Values.jobservice.podAnnotations | indent 8 }}
{{- end }}
    spec:
      securityContext:
        fsGroup: 10000
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - name: jobservice
        image: {{ .Values.jobservice.image.repository }}:{{ .Values.jobservice.image.tag }}
        imagePullPolicy: {{ .Values.imagePullPolicy }}
        livenessProbe:
          httpGet:
            path: /api/v1/stats
            port: 8080
          initialDelaySeconds: 20
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /api/v1/stats
            port: 8080
          initialDelaySeconds: 20
          periodSeconds: 10
{{- if .Values.jobservice.resources }}
        resources:
{{ toYaml .Values.jobservice.resources | indent 10 }}
{{- end }}
        env:
          - name: CORE_SECRET
            valueFrom:
              secretKeyRef:
                name: {{ template "harbor.core" . }}
                key: secret
          - name: JOBSERVICE_SECRET
            valueFrom:
              secretKeyRef:
                name: "{{ template "harbor.jobservice" . }}"
                key: secret
          - name: CORE_URL
            value: "http://{{ template "harbor.core" . }}"
          - name: REGISTRY_CONTROLLER_URL
            value: "http://{{ template "harbor.registry" . }}:8080"
          - name: LOG_LEVEL
            value: debug
          {{- if has "jobservice" .Values.proxy.components }}
          - name: HTTP_PROXY
            value: "{{ .Values.proxy.httpProxy }}"
          - name: HTTPS_PROXY
            value: "{{ .Values.proxy.httpsProxy }}"
          - name: NO_PROXY
            value: "{{ template "harbor.noProxy" . }}"
          {{- end }}
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: jobservice-config
          mountPath: /etc/jobservice/config.yml
          subPath: config.yml
        - name: job-logs
          mountPath: /var/log/jobs
          subPath: {{ .Values.persistence.persistentVolumeClaim.jobservice.subPath }}
      volumes:
      - name: jobservice-config
        configMap:
          name: "{{ template "harbor.jobservice" . }}"
      - name: job-logs
        {{- if and .Values.persistence.enabled (eq .Values.jobservice.jobLogger "file") }}
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.persistentVolumeClaim.jobservice.existingClaim | default (include "harbor.jobservice" .) }}  
        {{- else }}
        emptyDir: {}
        {{- end }}
    {{- with .Values.jobservice.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.jobservice.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.jobservice.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
