{{- if $.Values.openTelemetryCollector.enabled }}
# OneUptime otel-collector Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ printf "%s-%s" $.Release.Name "otel-collector"  }}
  namespace: {{ $.Release.Namespace }}
  labels:
    app: {{ printf "%s-%s" $.Release.Name "otel-collector"  }}
    app.kubernetes.io/part-of: oneuptime
    app.kubernetes.io/managed-by: Helm
    appname: oneuptime
    {{- if $.Values.deployment.includeTimestampLabel }}
    date: "{{ now | unixEpoch }}"
    {{- end }}
spec:
  selector:
    matchLabels:
      app: {{ printf "%s-%s" $.Release.Name "otel-collector"  }}
  {{- if $.Values.openTelemetryCollector.replicaCount }}
  replicas: {{ $.Values.openTelemetryCollector.replicaCount }}
  {{- else }}
  {{- if or (not $.Values.autoscaling.enabled) ($.Values.openTelemetryCollector.disableAutoscaler) }}
  replicas: {{ $.Values.deployment.replicaCount }}
  {{- end }}
  {{- end }}
  strategy: {{- toYaml $.Values.deployment.updateStrategy | nindent 4 }}
  template:
    metadata:
      labels:
        app: {{ printf "%s-%s" $.Release.Name "otel-collector"  }}
        {{- if $.Values.deployment.includeTimestampLabel }}
        date: "{{ now | unixEpoch }}"
        {{- end }}
        appname: oneuptime
    spec:
      volumes:
        - name: greenlockrc
          emptyDir:
            sizeLimit: "1Gi"
      {{- if $.Values.openTelemetryCollector.podSecurityContext }}
      securityContext:
        {{- toYaml $.Values.openTelemetryCollector.podSecurityContext | nindent 8 }}
      {{- else if $.Values.podSecurityContext }}
      securityContext:
        {{- toYaml $.Values.podSecurityContext | nindent 8 }}
      {{- end }}
      {{- if $.Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml $.Values.imagePullSecrets | nindent 8 }}
      {{- end }}
      {{- if $.Values.affinity }}
      affinity: {{- $.Values.affinity | toYaml | nindent 8 }}
      {{- end }}
      {{- if $.Values.tolerations }}
      tolerations: {{- $.Values.tolerations | toYaml | nindent 8 }}
      {{- end }}
      {{- if $.Values.openTelemetryCollector.nodeSelector }}
      nodeSelector:
        {{- toYaml $.Values.openTelemetryCollector.nodeSelector | nindent 8 }}
      {{- else if $.Values.nodeSelector }}
      nodeSelector:
        {{- toYaml $.Values.nodeSelector | nindent 8 }}
      {{- end }}
      containers:
        - image: {{ include "oneuptime.image" (dict "Values" $.Values "ServiceName" "otel-collector") }}
          name: {{ printf "%s-%s" $.Release.Name "otel-collector"  }}
          # Liveness probe
          {{- if $.Values.startupProbe.enabled }}
          # Startup probe
          startupProbe:
            httpGet:
              path: /health/status
              port: 13133
            periodSeconds: {{ $.Values.startupProbe.periodSeconds }}
            failureThreshold: {{ $.Values.startupProbe.failureThreshold }}
          {{- end }}
          {{- if $.Values.livenessProbe.enabled }}
          # Liveness probe
          livenessProbe:
            httpGet:
              path: /health/status
              port: 13133
            periodSeconds: {{ $.Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ $.Values.livenessProbe.timeoutSeconds }}
            initialDelaySeconds: {{ $.Values.livenessProbe.initialDelaySeconds }}
          {{- end }}
          {{- if $.Values.readinessProbe.enabled }}
          # Readyness Probe
          readinessProbe:
            httpGet:
              path: /health/status
              port: 13133
            periodSeconds: {{ $.Values.readinessProbe.periodSeconds }}
            initialDelaySeconds: {{ $.Values.readinessProbe.initialDelaySeconds }}
            timeoutSeconds: {{ $.Values.readinessProbe.timeoutSeconds }}
          {{- end }}
          {{- if $.Values.openTelemetryCollector.containerSecurityContext }}
          securityContext:
            {{- toYaml $.Values.openTelemetryCollector.containerSecurityContext | nindent 12 }}
          {{- else if $.Values.containerSecurityContext }}
          securityContext:
            {{- toYaml $.Values.containerSecurityContext | nindent 12 }}
          {{- end }}
          imagePullPolicy: {{ $.Values.image.pullPolicy }}
          env:
            {{- include "oneuptime.env.common" . | nindent 12 }}
            {{- include "oneuptime.env.runtime" (dict "Values" $.Values "Release" $.Release) | nindent 12 }}
            - name: PORT
              value: {{ $.Values.openTelemetryCollector.ports.grpc | quote }} 
            - name: OPENTELEMETRY_COLLECTOR_SENDING_QUEUE_ENABLED
              value: {{ $.Values.openTelemetryCollector.sendingQueue.enabled | quote }}
            - name: OPENTELEMETRY_COLLECTOR_SENDING_QUEUE_NUM_CONSUMERS
              value: {{ $.Values.openTelemetryCollector.sendingQueue.numConsumers | quote }}
            - name: OPENTELEMETRY_COLLECTOR_SENDING_QUEUE_SIZE
              value: {{ $.Values.openTelemetryCollector.sendingQueue.size | quote }}
            - name: DISABLE_TELEMETRY
              value: {{ $.Values.openTelemetryCollector.disableTelemetryCollection | quote }}
          ports:
            - containerPort: {{ $.Values.openTelemetryCollector.ports.http }}
              protocol: TCP
              name: http
            - containerPort: {{ $.Values.openTelemetryCollector.ports.grpc }}
              protocol: TCP
              name: grpc
          {{- if $.Values.openTelemetryCollector.resources }}
          resources:
            {{- toYaml $.Values.openTelemetryCollector.resources | nindent 12 }}
          {{- end }}
      restartPolicy: {{ $.Values.image.restartPolicy }}
---

# OneUptime otel-collector Service
{{- $otelCollectorPorts := dict "grpc" $.Values.openTelemetryCollector.ports.grpc "http" $.Values.openTelemetryCollector.ports.http -}}
{{- $identityServiceArgs := dict "ServiceName" "otel-collector" "Ports" $otelCollectorPorts "Release" $.Release "Values" $.Values -}}
{{- include "oneuptime.service" $identityServiceArgs }}
---

# OneUptime otel-collector autoscaler
{{- if not $.Values.openTelemetryCollector.disableAutoscaler }}
{{- $identityAutoScalerArgs := dict "ServiceName" "otel-collector" "Release" $.Release "Values" $.Values -}}
{{- include "oneuptime.autoscaler" $identityAutoScalerArgs }}
{{- end }}
---

{{- end }}
