# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "clamav.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "clamav.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "clamav.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
 # clamd-configmap.yaml
{{- if .Values.clamdConfig -}}
kind: ConfigMap
apiVersion: v1
metadata:
  name: {{ include "clamav.fullname" . }}-clamd
  labels:
    app: {{ template "clamav.name" . }}
    chart: {{ template "clamav.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
data:
  clamd.conf:
    {{ toYaml .Values.clamdConfig | indent 4 }}
{{- end }}
 # deployment.yaml
apiVersion: {{ default "apps/v1" .Values.kubeMeta.deploymentApiVersion }}
kind: Deployment
metadata:
  name: {{ include "clamav.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "clamav.name" . }}
    helm.sh/chart: {{ include "clamav.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "clamav.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "clamav.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
{{- if .Values.podLabels }}
{{ toYaml .Values.podLabels | indent 8 }}
{{- end }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- if .Values.freshclamConfig }}
          volumeMounts:
          - name: freshclam-config-volume
            mountPath: /etc/clamav/freshclam.conf
            subPath: freshclam.conf
{{- end }}
{{- if .Values.clamdConfig }}
          volumeMounts:
          - name: clamd-config-volume
            mountPath: /etc/clamav/clamd.conf
            subPath: clamd.conf
{{- end }}
          ports:
            - name: clamavport
              containerPort: 3310
              protocol: TCP
          livenessProbe:
            tcpSocket:
              port: clamavport
            initialDelaySeconds: 300
          readinessProbe:
            tcpSocket:
              port: clamavport
            initialDelaySeconds: 300
          resources:
{{ toYaml .Values.resources | indent 12 }}
{{- if .Values.freshclamConfig }}
      volumes:
        - name: freshclam-config-volume
          configMap:
            name: {{ include "clamav.fullname" . }}-freshclam
{{- end }}
{{- if .Values.clamdConfig }}
      volumes:
        - name: clamd-config-volume
          configMap:
            name: {{ include "clamav.fullname" . }}-clamd
{{- end }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
 # freshclam-configmap.yaml
{{- if .Values.freshclamConfig -}}
kind: ConfigMap
apiVersion: v1
metadata:
  name: {{ include "clamav.fullname" . }}-freshclam
  labels:
    app: {{ template "clamav.name" . }}
    chart: {{ template "clamav.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
data:
  freshclam.conf:
    {{ toYaml .Values.freshclamConfig | indent 4 }}
{{- end }}
 # ingress.yaml
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "clamav.fullname" . -}}
{{- $ingressPath := .Values.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: {{ $fullName }}
  labels:
    app.kubernetes.io/name: {{ include "clamav.name" . }}
    helm.sh/chart: {{ include "clamav.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- with .Values.ingress.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- if .Values.ingress.tls }}
  tls:
  {{- range .Values.ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . | quote }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
  rules:
  {{- range .Values.ingress.hosts }}
    - host: {{ . | quote }}
      http:
        paths:
          - path: {{ $ingressPath }}
            backend:
              serviceName: {{ $fullName }}
              servicePort: http
  {{- end }}
{{- end }}
 # service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ include "clamav.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "clamav.name" . }}
    helm.sh/chart: {{ include "clamav.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: 3310
      protocol: TCP
      name: clamavport
  selector:
    app.kubernetes.io/name: {{ include "clamav.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}

# Default values for ClamAV.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
  repository: mailu/clamav
  tag: "1.6"
  pullPolicy: IfNotPresent

nameOverride: ""
fullnameOverride: ""

service:
  type: ClusterIP
  port: 3310

ingress:
  enabled: false
  annotations: {}
    # kubernetes.io/ingress.class: nginx
    # kubernetes.io/tls-acme: "true"
  path: /
  hosts:
    - chart-example.local
  tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

# Use freshclamConfig to define the content of /etc/clamav/freshclam.conf. Example:
# freshclamConfig: |
#   HTTPProxyServer your-proxy.example.com
#   HTTPProxyPort 8080
#   DatabaseDirectory /data
#   LogSyslog yes
#   LogTime yes
#   PidFile /run/clamav/freshclam.pid
#   DatabaseOwner root
#   DatabaseMirror database.clamav.net
#   ScriptedUpdates yes
#   NotifyClamd /etc/clamav/clamd.conf
#   SafeBrowsing yes
#   Bytecode yes

resources: {}
  # We usually recommend not to specify default resources and to leave this as a conscious
  # choice for the user. This also increases chances charts run on environments with little
  # resources, such as Minikube. If you do want to specify resources, uncomment the following
  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.
  # limits:
  #  cpu: 100m
  #  memory: 128Mi
  # requests:
  #  cpu: 100m
  #  memory: 128Mi

# Additional pod labels
podLabels: {}

nodeSelector: {}

tolerations: []

affinity: {}

# Metadata surrounding the kubernetes distribution being deployed to.
kubeMeta:
  # The api that Deployments are under.
  deploymentApiVersion: apps/v1
