apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "powerdns-helm.fullname" . }}
  labels:
    {{- include "powerdns-helm.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "powerdns-helm.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "powerdns-helm.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      volumes:
        - name: data
          {{- .Values.volumes.data | nindent 10 }}
        - name: secret
          secret:
            secretName: {{ include "powerdns-helm.fullname" . }}
        - name: config
          configMap:
            name: {{ include "powerdns-helm.fullname" . }}
        - name: tmp
          emptyDir:
            medium: Memory
      initContainers:
        - name: sqlite-init
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: keinos/sqlite3
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - "sqlite3"
          args:
            - "/data/pdns.sqlite"
            - "-init"
            - "/secret/init.sql"
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - mountPath: /data
              name: data
            - mountPath: /secret
              name: secret
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - "pdns_server"
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
            - name: dns-udp
              containerPort: 5353
              protocol: UDP
            - name: dns-tcp
              containerPort: 5353
              protocol: TCP
          #startupProbe:
          #  httpGet:
          #    path: /healthz
          #    port: metrics
          #livenessProbe:
          #  httpGet:
          #    path: /healthz
          #    port: metrics
          #readinessProbe:
          #  httpGet:
          #    path: /healthz
          #    port: metrics
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - mountPath: /data
              name: data
            - mountPath: /etc/powerdns/pdns.conf
              name: config
              subPath: pdns.conf
            - mountPath: /etc/powerdns/pdns.d/secrets.conf
              name: secret
              subPath: pdns.conf
            - mountPath: /etc/powerdns/pdns.d/database.conf
              name: config
              subPath: database.conf
            - mountPath: /etc/powerdns/pdns.d/security.conf
              name: config
              subPath: security.conf
            - mountPath: /etc/powerdns/pdns.d/dnsupdate.conf
              name: config
              subPath: dnsupdate.conf
            - mountPath: /etc/powerdns/pdns.d/soa.conf
              name: config
              subPath: soa.conf
            - mountPath: /var/run
              name: tmp
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}