apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: "{{ template "worker.fullname" . }}"
  labels:
    app: "{{ template "worker.fullname" . }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
    component: worker
spec:
  replicas: {{ .Values.workerConfig.replicaCount }}
  template:
    metadata:
      labels:
        app: "{{ template "worker.fullname" . }}"
        component: worker
    spec:
      containers:
      - name: {{ .Chart.Name }}-worker
        image: {{ .Values.image.tag }}
        env:
        - name: ANCHORE_ENGINE_SERVICES
          value: anchore-worker
        - name: ANCHORE_DB
          value: {{ .Values.postgresql.postgresDatabase }}
        - name: ANCHORE_DB_HOST
          value: {{ template "postgres.fullname" . }}
        - name: ANCHORE_DB_USER
          valueFrom:
            secretKeyRef:
              name: {{ template "fullname" . }}
              key: dbUser
        - name: ANCHORE_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "fullname" . }}
              key: dbPassword
        - name: ANCHORE_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "fullname" . }}
              key: adminPassword
        - name: ANCHORE_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: ANCHORE_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: ANCHORE_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        ports:
        - name: analyzer-api
          containerPort: {{ .Values.workerConfig.port }}
        volumeMounts:
        - name: config-volume
          mountPath: /config
        {{- if .Values.globalConfig.internalServicesSslEnabled }}
        - name: certs
          mountPath: {{ default "/certs" .Values.workerConfig.ssl.CertDir }}
          readOnly: true
        {{- end }}
        - name: analysis-scratch
          mountPath: {{ .Values.workerConfig.analysisScratchVolume.mountPath }}
        livenessProbe:
          exec:
            command:
            - pgrep
            - twistd
          initialDelaySeconds: 10
          periodSeconds: 60
          failureThreshold: 5
        readinessProbe:
          httpGet:
            path: /health
            port: analyzer-api
          initialDelaySeconds: 5
          periodSeconds: 10
          timeoutSeconds: 10
          failureThreshold: 10
        resources:
{{ toYaml .Values.workerConfig.resources | indent 10 }}
      volumes:
        - name: config-volume
          configMap:
            name: {{ template "worker.fullname" .}}
        {{- if .Values.globalConfig.internalServicesSslEnabled }}
        - name: certs
          secret:
            secretName: {{ .Values.workerConfig.ssl.certSecret }}
        {{- end }}
        - name: analysis-scratch
          {{ toYaml .Values.workerConfig.analysisScratchVolume.details -}}
    {{- if .Values.workerConfig.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.workerConfig.nodeSelector | indent 8 }}
    {{- end }}
    {{- with .Values.workerConfig.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.workerConfig.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
