{{- $component := "api" -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "anchore-engine.api.fullname" . }}
  labels:
    app: {{ template "anchore-engine.fullname" . }}
    component: {{ $component }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  selector:
    matchLabels:
      app: {{ template "anchore-engine.fullname" . }}
      component: {{ $component }}
  replicas: {{ .Values.anchoreApi.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ template "anchore-engine.fullname" . }}
        component: {{ $component }}
{{- if .Values.anchoreApi.annotations }}
      annotations:
{{ toYaml .Values.anchoreApi.annotations | indent 8 }}
{{- end }}
    spec:
      volumes:
        - name: config-volume
          configMap:
            name: {{ template "anchore-engine.fullname" . }}
        {{- if and .Values.anchoreEnterpriseGlobal.enabled .Values.anchoreEnterpriseRbac.enabled }}
        - name: anchore-license
          secret:
            secretName: {{ .Values.anchoreEnterpriseGlobal.licenseSecretName }}
        - name: rbac-config-volume
          configMap:
            name: {{ template "anchore-engine.enterprise.fullname" . }}
        {{- end}}
        {{- if .Values.anchoreGlobal.internalServicesSslEnabled }}
        - name: certs
          secret:
            secretName: {{ .Values.anchoreGlobal.internalServicesSsl.certSecret }}
        {{- end }}
      containers:
      - name: "{{ .Chart.Name }}-{{ $component }}"
        image: {{ .Values.anchoreGlobal.image }}
        imagePullPolicy: {{ .Values.anchoreGlobal.imagePullPolicy }}
        command: ["/usr/local/bin/anchore-manager"]
        args: ["service", "start", "apiext"]
        envFrom:
        - secretRef:
            name: {{ template "anchore-engine.fullname" . }}
        - configMapRef:
            name: {{ template "anchore-engine.fullname" . }}
        env:
        - name: ANCHORE_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        ports:
        - containerPort: {{ .Values.anchoreApi.service.port }}
          name: external-api
        volumeMounts:
        - name: config-volume
          mountPath: /config/config.yaml
          subPath: config.yaml
        {{- if .Values.anchoreGlobal.internalServicesSslEnabled }}
        - name: certs
          mountPath: {{ default "/certs" .Values.anchoreGlobal.internalServicesSsl.certDir }}
          readOnly: true
        {{- end }}
        livenessProbe:
          httpGet:
            path: /health
            port: external-api
          initialDelaySeconds: 120
          timeoutSeconds: 10
          periodSeconds: 10
          failureThreshold: 6
          successThreshold: 1
        readinessProbe:
          httpGet:
            path: /health
            port: external-api
          timeoutSeconds: 10
          periodSeconds: 10
          failureThreshold: 3
          successThreshold: 1
        resources:
{{ toYaml .Values.anchoreApi.resources | indent 10 }}

      {{- if and .Values.anchoreEnterpriseGlobal.enabled .Values.anchoreEnterpriseRbac.enabled }}
      - name: {{ .Chart.Name }}-rbac-manager
        image: {{ .Values.anchoreEnterpriseGlobal.image }}
        imagePullPolicy: {{ .Values.anchoreEnterpriseGlobal.imagePullPolicy }}
        command: ["/usr/local/bin/anchore-enterprise-manager"]
        args: ["service", "start", "rbac_manager"]
        envFrom:
        - secretRef:
            name: {{ template "anchore-engine.fullname" . }}
        - configMapRef:
            name: {{ template "anchore-engine.fullname" . }}
        env:
        - name: ANCHORE_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        ports:
        - containerPort: {{ .Values.anchoreEnterpriseRbac.service.apiPort }}
          name: rbac-manager
        volumeMounts:
        - name: anchore-license
          mountPath: /license.yaml
          subPath: license.yaml
        - name: rbac-config-volume
          mountPath: /config/config.yaml
          subPath: config.yaml
        {{- if .Values.anchoreGlobal.internalServicesSslEnabled }}
        - name: certs
          mountPath: {{ default "/certs" .Values.anchoreGlobal.internalServicesSsl.certDir }}
          readOnly: true
        {{- end }}
        livenessProbe:
          httpGet:
            path: /health
            port: rbac-manager
          initialDelaySeconds: 120
          timeoutSeconds: 10
          periodSeconds: 10
          failureThreshold: 6
          successThreshold: 1
        readinessProbe:
          httpGet:
            path: /health
            port: rbac-manager
          timeoutSeconds: 10
          periodSeconds: 10
          failureThreshold: 3
          successThreshold: 1
        resources:
{{ toYaml .Values.anchoreEnterpriseRbac.managerResources | indent 10 }}

      - name: {{ .Chart.Name }}-rbac-authorizer
        image: {{ .Values.anchoreEnterpriseGlobal.image }}
        imagePullPolicy: {{ .Values.anchoreEnterpriseGlobal.imagePullPolicy }}
        command: ["/usr/local/bin/anchore-enterprise-manager"]
        args: ["service", "start", "rbac_authorizer"]
        envFrom:
        - secretRef:
            name: {{ template "anchore-engine.fullname" . }}
        - configMapRef:
            name: {{ template "anchore-engine.fullname" . }}
        env:
        - name: ANCHORE_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        ports:
        - containerPort: {{ .Values.anchoreEnterpriseRbac.service.authPort }}
          name: rbac-auth
        volumeMounts:
        - name: anchore-license
          mountPath: /license.yaml
          subPath: license.yaml
        - name: rbac-config-volume
          mountPath: /config/config.yaml
          subPath: config.yaml
        {{- if .Values.anchoreGlobal.internalServicesSslEnabled }}
        - name: certs
          mountPath: {{ default "/certs" .Values.anchoreGlobal.internalServicesSsl.certDir }}
          readOnly: true
        {{- end }}
        livenessProbe:
          exec:
            command:
              - curl
              - -f
              - 'localhost:{{ .Values.anchoreEnterpriseRbac.service.authPort }}/health'
          initialDelaySeconds: 120
          timeoutSeconds: 10
          periodSeconds: 10
          failureThreshold: 6
          successThreshold: 1
        readinessProbe:
          exec:
            command:
              - curl
              - -f
              - 'localhost:{{ .Values.anchoreEnterpriseRbac.service.authPort }}/health'
          timeoutSeconds: 10
          periodSeconds: 10
          failureThreshold: 3
          successThreshold: 1
        resources:
{{ toYaml .Values.anchoreEnterpriseRbac.authResources | indent 10 }}

      imagePullSecrets:
      - name: {{ .Values.anchoreEnterpriseGlobal.imagePullSecretName }}
      {{- end }}

      {{- if .Values.anchoreApi.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.anchoreApi.nodeSelector | indent 8 }}
      {{- end }}
      {{- with .Values.anchoreApi.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- with .Values.anchoreApi.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
      {{- end }}

---
apiVersion: v1
kind: Service
metadata:
  name: {{ template "anchore-engine.api.fullname" . }}
  labels:
    app: {{ template "anchore-engine.fullname" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    component: {{ $component }}
  {{- if .Values.anchoreApi.service.annotations }}
  annotations:
{{ toYaml .Values.anchoreApi.service.annotations | indent 4 }}
  {{- end }}
spec:
  type: {{ .Values.anchoreApi.service.type }}
  ports:
    - name: anchore-external-api
      port: {{ .Values.anchoreApi.service.port }}
      targetPort: {{ .Values.anchoreApi.service.port }}
      protocol: TCP
  {{- if and .Values.anchoreEnterpriseGlobal.enabled .Values.anchoreEnterpriseRbac.enabled }}
    - name: anchore-rbac-manager
      port: {{ .Values.anchoreEnterpriseRbac.service.apiPort }}
      targetPort: {{ .Values.anchoreEnterpriseRbac.service.apiPort }}
      protocol: TCP
  {{- end }}
  selector:
    app: {{ template "anchore-engine.fullname" . }}
    component: {{ $component }}
