# helm_charts_enterprise_feeds_deployment.yaml
{{- if and .Values.anchoreEnterpriseGlobal.enabled .Values.anchoreEnterpriseFeeds.enabled -}}
{{- $component := "enterprise-feeds" -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "anchore-engine.enterprise-feeds.fullname" . }}
  labels:
    app: {{ template "anchore-engine.fullname" . }}
    component: {{ $component }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    {{- with .Values.anchoreEnterpriseFeeds.labels }}
    {{ toYaml . | nindent 4 }}
    {{- end }}
    {{- with .Values.anchoreGlobal.labels }}
    {{ toYaml . | nindent 4 }}
    {{- end }}
spec:
  selector:
    matchLabels:
      app: {{ template "anchore-engine.fullname" . }}
      component: {{ $component }}
  replicas: 1
  strategy:
    type: Recreate
    rollingUpdate: null
  template:
    metadata:
      labels:
        app: {{ template "anchore-engine.fullname" . }}
        component: {{ $component }}
        {{- with .Values.anchoreEnterpriseFeeds.labels }}
        {{ toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.anchoreGlobal.labels }}
        {{ toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.anchoreEnterpriseFeeds.annotations }}
      annotations:
        {{ toYaml . | nindent 8 }}
      {{- end }}
    spec:
      securityContext:
        runAsUser: 1000
        runAsGroup: 1000
      imagePullSecrets:
      - name: {{ .Values.anchoreEnterpriseGlobal.imagePullSecretName }}
      containers:
      {{- if .Values.cloudsql.enabled  }}
      - name: cloudsql-proxy
        image: {{ .Values.cloudsql.image.repository }}:{{ .Values.cloudsql.image.tag }}
        imagePullPolicy: {{ .Values.cloudsql.image.pullPolicy }}
        command: ["/cloud_sql_proxy"]
        args:
        - "-instances={{ .Values.cloudsql.instance }}=tcp:5432"
        {{- if .Values.cloudsql.useExistingServiceAcc }}
        - "-credential_file=/var/{{ .Values.cloudsql.serviceAccSecretName }}/{{ .Values.cloudsql.serviceAccJsonName }}"
        volumeMounts:
        - mountPath: /var/{{ .Values.cloudsql.serviceAccSecretName }}
          name: {{ .Values.cloudsql.serviceAccSecretName }}
          readOnly: true
        {{- end }}
      {{- end }}
      - name: "{{ .Chart.Name }}-{{ $component }}"
        image: {{ .Values.anchoreEnterpriseGlobal.image }}
        imagePullPolicy: {{ .Values.anchoreEnterpriseGlobal.imagePullPolicy }}
        args: ["anchore-enterprise-manager", "service", "start", "--no-auto-upgrade", "feeds"]
        ports:
        - containerPort: {{ .Values.anchoreEnterpriseFeeds.service.port }}
          name: feeds-api
        envFrom:
        - secretRef:
            name: {{ default (include "anchore-engine.fullname" .) .Values.anchoreGlobal.existingSecret }}
        - configMapRef:
            name: {{ template "anchore-engine.enterprise-feeds.fullname" . }}-env
        env:
        {{- with .Values.anchoreGlobal.extraEnv }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.anchoreEnterpriseFeeds.extraEnv }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        - name: ANCHORE_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "anchore-engine.fullname" . }}
              key: .feedsDbPassword
        - name: ANCHORE_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        volumeMounts:
        - name: config-volume
          mountPath: /config/config.yaml
          subPath: config.yaml
        {{- if .Values.anchoreGlobal.openShiftDeployment }}
        - name: service-config-volume
          mountPath: /anchore_service_config
        - name: logs
          mountPath: /var/log/anchore
        - name: run
          mountPath: /var/run/anchore
        {{- end }}
        - name: {{ $component }}-scratch
          mountPath: {{ .Values.anchoreGlobal.scratchVolume.mountPath }}
        - name: anchore-license
          mountPath: /home/anchore/license.yaml
          subPath: license.yaml
        {{- if (.Values.anchoreGlobal.certStoreSecretName) }}
        - name: certs
          mountPath: /home/anchore/certs/
          readOnly: true
        {{- end }}
        livenessProbe:
          httpGet:
            path: /health
            port: feeds-api
            {{- if .Values.anchoreGlobal.internalServicesSsl.enabled }}
            scheme: HTTPS
            {{- end }}
          initialDelaySeconds: 120
          timeoutSeconds: 10
          periodSeconds: 10
          failureThreshold: 6
          successThreshold: 1
        readinessProbe:
          httpGet:
            path: /health
            port: feeds-api
            {{- if .Values.anchoreGlobal.internalServicesSsl.enabled }}
            scheme: HTTPS
            {{- end }}
          timeoutSeconds: 10
          periodSeconds: 10
          failureThreshold: 3
          successThreshold: 1
        resources:
          {{ toYaml .Values.anchoreEnterpriseFeeds.resources | nindent 10 }}
      volumes:
        - name: config-volume
          configMap:
            name: {{ template "anchore-engine.enterprise-feeds.fullname" . }}
        - name: {{ $component}}-scratch
          {{ toYaml .Values.anchoreGlobal.scratchVolume.details | nindent 10 }}
        {{- if .Values.anchoreGlobal.openShiftDeployment }}
        - name: service-config-volume
          emptyDir: {}
        - name: logs
          emptyDir: {}
        - name: run
          emptyDir: {}
        {{- end }}
        - name: anchore-license
          secret:
            secretName: {{ .Values.anchoreEnterpriseGlobal.licenseSecretName }}
        {{- if .Values.cloudsql.useExistingServiceAcc }}
        - name: {{ .Values.cloudsql.serviceAccSecretName }}
          secret:
            secretName: {{ .Values.cloudsql.serviceAccSecretName }}
        {{- end }}
        {{- with .Values.anchoreGlobal.certStoreSecretName }}
        - name: certs
          secret:
            secretName: {{ . }}
        {{- end }}
      {{- with .Values.anchoreEnterpriseFeeds.nodeSelector }}
      nodeSelector:
        {{ toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.anchoreEnterpriseFeeds.affinity }}
      affinity:
        {{ toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.anchoreEnterpriseFeeds.tolerations }}
      tolerations:
        {{ toYaml . | nindent 8 }}
      {{- end }}

---
apiVersion: v1
kind: Service
metadata:
  name: {{ template "anchore-engine.enterprise-feeds.fullname" . }}
  labels:
    app: {{ template "anchore-engine.fullname" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    component: {{ $component }}
    {{- with .Values.anchoreEnterpriseFeeds.service.labels }}
    {{ toYaml . | nindent 4 }}
    {{- end }}
    {{- with .Values.anchoreGlobal.labels }}
    {{ toYaml . | nindent 4 }}
    {{- end }}
  {{- with .Values.anchoreEnterpriseFeeds.service.annotations }}
  annotations:
    {{ toYaml . | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.anchoreEnterpriseFeeds.service.type }}
  ports:
    - name: feeds-api
      port: {{ .Values.anchoreEnterpriseFeeds.service.port }}
      targetPort: {{ .Values.anchoreEnterpriseFeeds.service.port }}
      protocol: TCP
  selector:
    app: {{ template "anchore-engine.fullname" . }}
    component: {{ $component }}

{{- end -}}