{{/*
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
*/}}

################################
## Airflow StatsD Deployment
#################################
{{- if .Values.statsd.enabled }}
{{- $nodeSelector := or .Values.statsd.nodeSelector .Values.nodeSelector }}
{{- $affinity := or .Values.statsd.affinity .Values.affinity }}
{{- $tolerations := or .Values.statsd.tolerations .Values.tolerations }}
{{- $topologySpreadConstraints := or .Values.statsd.topologySpreadConstraints .Values.topologySpreadConstraints }}
{{- $revisionHistoryLimit := include "airflow.revisionHistoryLimit" (list .Values.statsd.revisionHistoryLimit .Values.revisionHistoryLimit) }}
{{- $securityContext := include "localPodSecurityContext" .Values.statsd }}
{{- $containerSecurityContext := include "externalContainerSecurityContext" .Values.statsd }}
{{- $containerLifecycleHooks := .Values.statsd.containerLifecycleHooks }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "airflow.fullname" . }}-statsd
  labels:
    tier: airflow
    component: statsd
    release: {{ .Release.Name }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: {{ .Release.Service }}
    {{- with .Values.labels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- with .Values.statsd.annotations }}
  annotations: {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  replicas: 1
  {{- if ne $revisionHistoryLimit "" }}
  revisionHistoryLimit: {{ $revisionHistoryLimit }}
  {{- end }}
  selector:
    matchLabels:
      tier: airflow
      component: statsd
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        tier: airflow
        component: statsd
        release: {{ .Release.Name }}
        {{- if or .Values.labels .Values.statsd.labels }}
          {{- mustMerge .Values.statsd.labels .Values.labels | toYaml | nindent 8 }}
        {{- end }}
      {{- if or .Values.statsd.extraMappings .Values.statsd.podAnnotations }}
      annotations:
        checksum/statsd-config: {{ include (print $.Template.BasePath "/configmaps/statsd-configmap.yaml") . | sha256sum }}
        {{- if .Values.statsd.podAnnotations }}
          {{- tpl (toYaml .Values.statsd.podAnnotations) . | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
      {{- if .Values.statsd.priorityClassName }}
      priorityClassName: {{ .Values.statsd.priorityClassName }}
      {{- end }}
      nodeSelector: {{- toYaml $nodeSelector | nindent 8 }}
      affinity: {{- toYaml $affinity | nindent 8 }}
      tolerations: {{- toYaml $tolerations | nindent 8 }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName }}
      {{- end }}
      topologySpreadConstraints: {{- toYaml $topologySpreadConstraints | nindent 8 }}
      terminationGracePeriodSeconds: {{ .Values.statsd.terminationGracePeriodSeconds }}
      serviceAccountName: {{ include "statsd.serviceAccountName" . }}
      {{- include "serviceLinks" . | nindent 6 }}
      securityContext: {{ $securityContext | nindent 8 }}
      restartPolicy: Always
      imagePullSecrets: {{ include "image_pull_secrets" . | nindent 8 }}
      containers:
        - name: statsd
          image: {{ template "statsd_image" . }}
          imagePullPolicy: {{ .Values.images.statsd.pullPolicy }}
          securityContext: {{ $containerSecurityContext | nindent 12 }}
          {{- if $containerLifecycleHooks }}
          lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }}
          {{- end }}
          args:
          {{- if .Values.statsd.cache.size }}
            - "--statsd.cache-size={{ .Values.statsd.cache.size }}"
          {{- end }}
          {{- if .Values.statsd.cache.type }}
            - "--statsd.cache-type={{ .Values.statsd.cache.type }}"
          {{- end }}
          {{- if .Values.statsd.args }}
          {{- range $arg := .Values.statsd.args }}
            - {{ $arg | quote }}
          {{- end }}
          {{- else }}
            - "--statsd.mapping-config=/etc/statsd-exporter/mappings.yml"
          {{- end }}
          resources: {{- toYaml .Values.statsd.resources | nindent 12 }}
          {{- with .Values.statsd.env }}
          env: {{- toYaml . | nindent 12 }}
          {{- end }}
          ports:
            - name: statsd-ingest
              protocol: UDP
              containerPort: {{ .Values.ports.statsdIngest }}
            - name: statsd-scrape
              containerPort: {{ .Values.ports.statsdScrape }}
          livenessProbe:
            httpGet:
              path: /metrics
              port: {{ .Values.ports.statsdScrape }}
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: /metrics
              port: {{ .Values.ports.statsdScrape }}
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 5
          volumeMounts:
            - name: config
              mountPath: /etc/statsd-exporter
              readOnly: true
      volumes:
        - name: config
          configMap:
            name: {{ include "airflow.fullname" . }}-statsd
{{- end }}
