{{- if .Values.trivy.enabled }}

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "harbor.trivy" . }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/component: trivy
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.trivy.replicas }}
  serviceName: {{ template "harbor.trivy" . }}
  updateStrategy: {{- toYaml .Values.trivy.updateStrategy | nindent 4 }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: trivy
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.trivy.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.trivy.podLabels "context" $) | nindent 8 }}
        {{- end }}
        app.kubernetes.io/component: trivy
      annotations:
        checksum/configmap-env: {{ include (print $.Template.BasePath "/trivy/trivy-cm-envvars.yaml") . | sha256sum }}
        checksum/secret-env: {{ include (print $.Template.BasePath "/trivy/trivy-secret-envvars.yaml") . | sha256sum }}
        {{- if and .Values.internalTLS.enabled (not .Values.trivy.tls.existingSecret) }}
        checksum/tls: {{ include (print $.Template.BasePath "/internal/internal-crt-secret.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.trivy.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.trivy.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "harbor.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.trivy.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.trivy.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.trivy.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.trivy.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.trivy.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.trivy.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext }}
      securityContext: {{- include "common.tplvalues.render" (dict "value" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      automountServiceAccountToken: {{ .Values.trivy.automountServiceAccountToken }}
      initContainers:
      {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "harbor.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - sh
            - -c
            - |
              mkdir -p "{{ .Values.trivy.cacheDir }}"
              mkdir -p "{{ .Values.trivy.cacheDir }}/trivy"
              mkdir -p "{{ .Values.trivy.cacheDir }}/reports"
              chown -R "{{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }}" "{{ .Values.trivy.cacheDir }}"
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: job-logs
              mountPath: /var/log/jobs
              subPath: {{ .Values.persistence.persistentVolumeClaim.jobservice.subPath }}
      {{- end }}
      {{- if .Values.trivy.initContainers }}
      {{- include "common.tplvalues.render" (dict "value" .Values.trivy.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: trivy
          image: {{ include "harbor.trivyImage" . }}
          imagePullPolicy: {{ .Values.trivyImage.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext }}
          securityContext: {{- include "common.tplvalues.render" (dict "value" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.trivy.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.trivy.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.trivy.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.trivy.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.trivyImage.debug | quote }}
            {{- if .Values.trivy.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.trivy.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            - configMapRef:
                name: {{ template "harbor.trivy" . }}-envvars
            - secretRef:
                name: {{ template "harbor.trivy" . }}-envvars
            {{- if .Values.trivy.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.trivy.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.trivy.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.trivy.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: api-server
              containerPort: {{ template "harbor.trivy.containerPort" . }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.trivy.cacheDir }}
              readOnly: false
            {{- if .Values.internalTLS.enabled }}
            - name: internal-tls-certs
              mountPath: /etc/harbor/ssl/trivy
            {{- end }}
            {{- if .Values.trivy.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.trivy.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
          {{- if .Values.trivy.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.trivy.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.trivy.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              scheme: {{ include "harbor.component.scheme" . | upper }}
              path: /probe/healthy
              port: api-server
            initialDelaySeconds: {{ .Values.trivy.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.trivy.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.trivy.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.trivy.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.trivy.livenessProbe.failureThreshold }}
          {{- else if .Values.trivy.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.trivy.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.trivy.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              scheme: {{ include "harbor.component.scheme" . | upper }}
              path: /probe/ready
              port: api-server
            initialDelaySeconds: {{ .Values.trivy.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.trivy.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.trivy.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.trivy.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.trivy.readinessProbe.failureThreshold }}
          {{- else if .Values.trivy.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.trivy.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          resources: {{- toYaml .Values.trivy.resources | nindent 12 }}
        {{- if .Values.trivy.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.trivy.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
      {{- if .Values.internalTLS.enabled }}
        - name: internal-tls-certs
          secret:
            secretName: {{ include "harbor.trivy.tls.secretName" . }}
      {{- end }}
      {{- if not .Values.persistence.enabled }}
        - name: data
          emptyDir: {}
      {{- end }}
      {{- if .Values.trivy.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.trivy.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
  {{- if .Values.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: data
        labels: {{- include "common.labels.matchLabels" . | nindent 10 }}
      spec:
        accessModes:
          - {{ .Values.persistence.persistentVolumeClaim.trivy.accessMode | quote }}
        {{- include "harbor.trivy.storageClass" . | nindent 8 }}
        resources:
          requests:
            storage: {{ .Values.persistence.persistentVolumeClaim.trivy.size | quote }}
  {{- end }}
{{- end }}
