{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- $resourceType := lower .Values.resourceType -}}
{{- if eq $resourceType "deployment" }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
{{- else if eq $resourceType "statefulset" }}
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
{{- else }}
apiVersion: {{ include "common.capabilities.daemonset.apiVersion" . }}
kind: DaemonSet
{{- end }}
metadata:
  name: {{ template "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: alloy
    app.kubernetes.io/part-of: grafana-alloy
  {{- if or .Values.controllerAnnotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.controllerAnnotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if and (not .Values.autoscaling.hpa.enabled) (not (eq $resourceType "daemonset")) }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  {{- if eq $resourceType "statefulset" }}
  podManagementPolicy: {{ .Values.podManagementPolicy | quote }}
  {{- end }}
  {{- if eq $resourceType "statefulset" }}
  serviceName: {{ template "common.names.fullname" . }}
  {{- if .Values.updateStrategy }}
  updateStrategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  {{- else if .Values.updateStrategy }}
  {{- if eq $resourceType "daemonset" }}
  updateStrategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- else }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" (dict "values" (list .Values.podLabels .Values.commonLabels) "context" .) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: alloy
      app.kubernetes.io/part-of: grafana-alloy
  template:
    metadata:
      annotations:
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
      {{- if .Values.podAnnotations }}
      {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: alloy
        app.kubernetes.io/part-of: grafana-alloy
    spec:
      {{- include "grafana-alloy.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ template "grafana-alloy.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.serviceAccount.automountServiceAccountToken }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "component" "alloy" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" "alloy" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.schedulerName }} 
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: alloy
          image: {{ template "grafana-alloy.image" . }}
          imagePullPolicy: {{ .Values.alloy.image.pullPolicy }}
          {{- if .Values.alloy.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.alloy.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.alloy.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.alloy.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.alloy.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.alloy.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - run
            - /opt/bitnami/grafana-alloy/config/config.alloy
            - --storage.path={{ .Values.alloy.storagePath }}
            - --server.http.listen-addr={{ .Values.alloy.listenAddr }}:{{ .Values.alloy.containerPorts.http }}
            - --server.http.ui-path-prefix={{ .Values.alloy.uiPathPrefix }}
            {{- if not .Values.alloy.enableReporting }}
            - --disable-reporting
            {{- end }}
            {{- if .Values.alloy.clustering.enabled }}
            - --cluster.enabled=true
            - --cluster.join-addresses={{ printf "%s-cluster" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
            {{- if .Values.alloy.clustering.name }}
            - --cluster.name={{ .Values.alloy.clustering.name }}
            {{- end }}
            {{- end }}
            {{- if .Values.alloy.stabilityLevel }}
            - --stability.level={{ .Values.alloy.stabilityLevel }}
            {{- end }}
          {{- end }}
          env:
            - name: ALLOY_DEPLOY_MODE
              value: "helm"
            - name: HOSTNAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            {{- if .Values.alloy.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.alloy.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.alloy.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.alloy.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.alloy.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.alloy.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.alloy.containerPorts.http }}
            {{- if .Values.alloy.extraContainerPorts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.alloy.extraContainerPorts "context" $) | nindent 12 }}
            {{- end }}
          {{- if .Values.alloy.resources }}
          resources: {{- toYaml .Values.alloy.resources | nindent 12 }}
          {{- else if ne .Values.alloy.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.alloy.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.alloy.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.alloy.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.alloy.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /-/healthy
              port: http
            initialDelaySeconds: {{ .Values.alloy.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.alloy.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.alloy.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.alloy.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.alloy.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.alloy.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.alloy.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.alloy.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /-/ready
              port: http
            initialDelaySeconds: {{ .Values.alloy.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.alloy.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.alloy.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.alloy.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.alloy.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.alloy.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.alloy.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.alloy.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: /-/ready
              port: http
            initialDelaySeconds: {{ .Values.alloy.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.alloy.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.alloy.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.alloy.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.alloy.startupProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.alloy.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.alloy.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          volumeMounts:
            - name: config
              mountPath: /opt/bitnami/grafana-alloy/config
            {{- if .Values.alloy.mounts.varlog }}
            - name: varlog
              mountPath: /var/log
              readOnly: true
            {{- end }}
            {{- if .Values.alloy.mounts.dockercontainers }}
            - name: dockercontainers
              mountPath: /var/lib/docker/containers
              readOnly: true
            {{- end }}
            - name: empty-dir
              mountPath: {{ .Values.alloy.storagePath }}
              subPath: app-storage-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            {{- if .Values.alloy.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.alloy.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.configReloader.enabled }}
        - name: config-reloader
          image: {{ template "config-reloader.image" . }}
          imagePullPolicy: {{ .Values.configReloader.image.pullPolicy }}
          {{- if .Values.configReloader.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.configReloader.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.configReloader.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.configReloader.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.configReloader.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.configReloader.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - -volume-dir=/opt/bitnami/grafana-alloy/config
            - -webhook-url=http://localhost:{{ .Values.alloy.containerPorts.http }}/-/reload
            - -web.listen-address=:{{ .Values.configReloader.containerPorts.http }}
          {{- end }}
          env:
            {{- if .Values.configReloader.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.configReloader.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.configReloader.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.configReloader.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.configReloader.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.configReloader.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.configReloader.containerPorts.http }}
            {{- if .Values.configReloader.extraContainerPorts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.configReloader.extraContainerPorts "context" $) | nindent 12 }}
            {{- end }}
          {{- if .Values.configReloader.resources }}
          resources: {{- toYaml .Values.configReloader.resources | nindent 12 }}
          {{- else if ne .Values.configReloader.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.configReloader.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.configReloader.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.configReloader.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.configReloader.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: http
            initialDelaySeconds: {{ .Values.configReloader.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.configReloader.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.configReloader.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.configReloader.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.configReloader.livenessProbe.failureThreshold }}          {{- end }}
          {{- if .Values.configReloader.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.configReloader.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.configReloader.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /metrics
              port: http
            initialDelaySeconds: {{ .Values.configReloader.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.configReloader.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.configReloader.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.configReloader.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.configReloader.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.configReloader.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.configReloader.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.configReloader.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: /metrics
              port: http
            initialDelaySeconds: {{ .Values.configReloader.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.configReloader.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.configReloader.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.configReloader.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.configReloader.startupProbe.failureThreshold }}          {{- end }}
          {{- if .Values.configReloader.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.configReloader.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          volumeMounts:
            - name: config
              mountPath: /opt/bitnami/grafana-alloy/config
            {{- if .Values.configReloader.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.configReloader.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: config
          secret:
            secretName: {{ include "grafana-alloy.secretName" . }}
        {{- if .Values.alloy.mounts.varlog }}
        - name: varlog
          hostPath:
            path: /var/log
        {{- end }}
        {{- if .Values.alloy.mounts.dockercontainers }}
        - name: dockercontainers
          hostPath:
            path: /var/lib/docker/containers
        {{- end }}
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
