apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.name }}
  namespace: {{ .Release.Namespace }}
spec:
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.name }}
    spec:
      initContainers:
        - name: git-cloner
          image: alpine/git
          args:
            - clone
            - --single-branch
            - --branch=master
            - https://github.com/tenable/terrascan.git
            - /data
          volumeMounts:
            - mountPath: /data
              name: terrascan-data-sync
      containers:
        - name: terrascan-server
          securityContext:
            allowPrivilegeEscalation: false
            runAsNonRoot: false
          image: {{ .Values.terrascan_container_image }}
          command:
            - terrascan
          args:
            - "server"
            - "--cert-path"
            - "/etc/certs/tls.crt"
            - "--key-path"
            - "/etc/certs/tls.key"
{{- if .Values.use_debug }}
            - "-l"
            - "debug"
{{- end }}
{{- $globconfig := .Files.Glob "data/config.toml" }}
{{- if $globconfig }}
            - "-c"
            - "/etc/config/terrascan-config.toml"
{{- end }}
          env:
            - name: "K8S_WEBHOOK_API_KEY"
              value: {{ .Values.terrascan_webhook_key}}
          volumeMounts:
            - name: cert-volume
              mountPath: /etc/certs
            - name: ssh-volume
              mountPath: /home/terrascan/.ssh
{{- $globconfig := .Files.Glob "data/config.toml" }}
{{- if $globconfig }}
            - name: config-volume
              mountPath: /etc/config
{{- end }}
            - name: terrascan-data-sync
              mountPath: /home/terrascan/.terrascan
      volumes:
        - name: cert-volume
          secret:
            secretName: {{ .Values.cert_secret_name }}
        - name: ssh-volume
          secret:
            secretName: {{ .Values.ssh_secret_name }}
            optional: true
{{- $globconfig := .Files.Glob "data/config.toml" }}
{{- if $globconfig }}
        - name: config-volume
          configMap:
            name: {{ .Values.config_name }}
{{- end }}
{{- if and .Values.persistence.enabled .Values.persistence.existingClaim }}
        - name: terrascan-data-sync
          persistentVolumeClaim:
{{- with .Values.persistence.existingClaim }}
            claimName: {{ tpl . $ }}
{{- end }}
{{- else }}
        - name: terrascan-data-sync
          emptyDir: {}
{{- end }}
---
# Create all resources that require certificates in a single template.
{{ include "certificate_related_deployments" . }}
