{{/*
 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.
*/}}

############################
## OTel Collector Deployment
############################
{{- if or .Values.otelCollector.tracesEnabled .Values.otelCollector.metricsEnabled }}
{{- $nodeSelector := or .Values.otelCollector.nodeSelector .Values.nodeSelector }}
{{- $affinity := or .Values.otelCollector.affinity .Values.affinity }}
{{- $tolerations := or .Values.otelCollector.tolerations .Values.tolerations }}
{{- $topologySpreadConstraints := or .Values.otelCollector.topologySpreadConstraints .Values.topologySpreadConstraints }}
{{- $revisionHistoryLimit := include "airflow.revisionHistoryLimit" (list .Values.otelCollector.revisionHistoryLimit .Values.revisionHistoryLimit) }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "airflow.fullname" . }}-otel-collector
  labels:
    tier: airflow
    component: otel-collector
    release: {{ .Release.Name }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: {{ .Release.Service }}
    {{- if or .Values.labels .Values.otelCollector.labels }}
      {{- mustMerge .Values.otelCollector.labels .Values.labels | toYaml | nindent 4 }}
    {{- end }}
  {{- with .Values.otelCollector.annotations }}
  annotations: {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  replicas: 1
  {{- if ne $revisionHistoryLimit "" }}
  revisionHistoryLimit: {{ $revisionHistoryLimit }}
  {{- end }}
  selector:
    matchLabels:
      tier: airflow
      component: otel-collector
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        tier: airflow
        component: otel-collector
        release: {{ .Release.Name }}
        {{- if or .Values.labels .Values.otelCollector.labels }}
          {{- mustMerge .Values.otelCollector.labels .Values.labels | toYaml | nindent 8 }}
        {{- end }}
      annotations:
        checksum/otel-collector-config: {{ include (print $.Template.BasePath "/configmaps/otel-collector-configmap.yaml") . | sha256sum }}
        {{- with .Values.otelCollector.podAnnotations }}
          {{- tpl (toYaml .) $ | nindent 8 }}
        {{- end }}
    spec:
      nodeSelector: {{- toYaml $nodeSelector | nindent 8 }}
      affinity: {{- toYaml $affinity | nindent 8 }}
      tolerations: {{- toYaml $tolerations | nindent 8 }}
      topologySpreadConstraints: {{- toYaml $topologySpreadConstraints | nindent 8 }}
      terminationGracePeriodSeconds: {{ .Values.otelCollector.terminationGracePeriodSeconds }}
      serviceAccountName: {{ include "otelCollector.serviceAccountName" . }}
      {{- include "serviceLinks" . | nindent 6 }}
      {{- if .Values.otelCollector.priorityClassName }}
      priorityClassName: {{ .Values.otelCollector.priorityClassName }}
      {{- end }}
      restartPolicy: Always
      securityContext: {{- toYaml .Values.otelCollector.securityContexts.pod | nindent 8 }}
      containers:
        - name: otel-collector
          image: {{ template "otel_collector_image" . }}
          imagePullPolicy: {{ .Values.images.otelCollector.pullPolicy }}
          securityContext: {{- toYaml .Values.otelCollector.securityContexts.container | nindent 12 }}
          args: {{- tpl (toYaml .Values.otelCollector.args) . | nindent 12 }}
          ports:
            - name: otlp-grpc
              containerPort: {{ .Values.ports.otelCollectorOtlpGrpc }}
              protocol: TCP
            - name: otlp-http
              containerPort: {{ .Values.ports.otelCollectorOtlpHttp }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: 13133
            initialDelaySeconds: {{ .Values.otelCollector.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.otelCollector.livenessProbe.periodSeconds }}
          readinessProbe:
            httpGet:
              path: /
              port: 13133
            initialDelaySeconds: {{ .Values.otelCollector.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.otelCollector.readinessProbe.periodSeconds }}
          resources: {{- toYaml .Values.otelCollector.resources | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /etc/otel-collector
              readOnly: true
      volumes:
        - name: config
          configMap:
            name: {{ include "airflow.fullname" . }}-otel-collector
{{- end }}
