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

{{- if .Values.bucketweb.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "thanos.bucketweb.fullname" . }}
  namespace: {{ include "common.names.namespace" . }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: bucketweb
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not (or .Values.bucketweb.autoscaling.enabled .Values.bucketweb.externalAutoscaler) }}
  replicas: {{ .Values.bucketweb.replicaCount }}
  {{- end }}
  revisionHistoryLimit: {{ .Values.bucketweb.revisionHistoryLimit }}
  {{- if .Values.bucketweb.updateStrategy }}
  strategy: {{- toYaml .Values.bucketweb.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.bucketweb.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: bucketweb
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: bucketweb
      {{- if or .Values.commonAnnotations .Values.bucketweb.podAnnotations (include "thanos.createObjstoreSecret" .) }}
      annotations:
        {{- if .Values.commonAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if (include "thanos.createObjstoreSecret" .) }}
        checksum/objstore-configuration: {{ include "thanos.objstoreConfig" . | sha256sum }}
        {{- end }}
        {{- if .Values.bucketweb.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
      {{- include "thanos.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ include "thanos.bucketweb.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.bucketweb.automountServiceAccountToken }}
      {{- if .Values.bucketweb.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.bucketweb.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.bucketweb.podAffinityPreset "component" "bucketweb" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.bucketweb.podAntiAffinityPreset "component" "bucketweb" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.bucketweb.nodeAffinityPreset.type "key" .Values.bucketweb.nodeAffinityPreset.key "values" .Values.bucketweb.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.bucketweb.dnsConfig }}
      dnsConfig: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.dnsConfig "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.bucketweb.dnsPolicy }}
      dnsPolicy: {{ .Values.bucketweb.dnsPolicy | quote }}
      {{- end }}
      {{- if .Values.bucketweb.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.bucketweb.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.bucketweb.priorityClassName }}
      priorityClassName: {{ .Values.bucketweb.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.bucketweb.schedulerName }}
      schedulerName: {{ .Values.bucketweb.schedulerName }}
      {{- end }}
      {{- if .Values.bucketweb.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.bucketweb.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.bucketweb.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.bucketweb.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        {{- if .Values.bucketweb.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.sidecars "context" $) | nindent 8 }}
        {{- end }}
        - name: bucketweb
          image: {{ include "thanos.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.bucketweb.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.bucketweb.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.bucketweb.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.command "context" $) | nindent 12 }}
          {{- end }}
          args:
            {{- if .Values.bucketweb.args }}
            {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.args "context" $) | nindent 12 }}
            {{- else }}
            - tools
            - bucket
            - web
            - --http-address=0.0.0.0:{{ .Values.bucketweb.containerPorts.http }}
            - --log.level={{ .Values.bucketweb.logLevel }}
            - --log.format={{ .Values.bucketweb.logFormat }}
            - --objstore.config-file=/conf/objstore.yml
            {{- if .Values.bucketweb.refresh }}
            - --refresh={{ .Values.bucketweb.refresh }}
            {{- end }}
            {{- if .Values.bucketweb.timeout }}
            - --timeout={{ .Values.bucketweb.timeout }}
            {{- end }}
            {{- if (include "thanos.httpConfigEnabled" .) }}
            - --http.config=/conf/http/http-config.yml
            {{- end }}
            {{- if .Values.bucketweb.extraFlags }}
            {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.extraFlags "context" $) | nindent 12 }}
            {{- end }}
            {{- end }}
          {{- if .Values.bucketweb.extraEnvVars }}
          env: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          {{- if or .Values.bucketweb.extraEnvVarsCM .Values.bucketweb.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.bucketweb.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.bucketweb.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.bucketweb.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.bucketweb.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.bucketweb.containerPorts.http }}
              protocol: TCP
          {{- if .Values.bucketweb.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.bucketweb.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.bucketweb.livenessProbe "enabled") "context" $) | nindent 12 }}
            {{- if not .Values.auth.basicAuthUsers }}
            httpGet:
              path: /-/healthy
              port: http
              scheme: {{ ternary "HTTPS" "HTTP" .Values.https.enabled }}
            {{- else }}
            tcpSocket:
              port: http
            {{- end }}
          {{- end }}
          {{- if .Values.bucketweb.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.bucketweb.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.bucketweb.readinessProbe "enabled") "context" $) | nindent 12 }}
            {{- if not .Values.auth.basicAuthUsers }}
            httpGet:
              path: /-/ready
              port: http
              scheme: {{ ternary "HTTPS" "HTTP" .Values.https.enabled }}
            {{- else }}
            tcpSocket:
              port: http
            {{- end }}
          {{- end }}
          {{- if .Values.bucketweb.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.bucketweb.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.bucketweb.startupProbe "enabled") "context" $) | nindent 12 }}
            {{- if not .Values.auth.basicAuthUsers }}
            httpGet:
              path: /-/ready
              port: http
              scheme: {{ ternary "HTTPS" "HTTP" .Values.https.enabled }}
            {{- else }}
            tcpSocket:
              port: http
            {{- end }}
          {{- end }}
          {{- if .Values.bucketweb.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.bucketweb.resources }}
          resources: {{- toYaml .Values.bucketweb.resources | nindent 12 }}
          {{- else if ne .Values.bucketweb.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.bucketweb.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: objstore-config
              mountPath: /conf
            {{- if (include "thanos.httpConfigEnabled" .) }}
            - name: http-config
              mountPath: /conf/http
            {{- if .Values.https.enabled }}
            - name: http-certs
              mountPath: /certs
            {{- end }}
            {{- end }}
            {{- if .Values.bucketweb.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
      volumes:
        - name: objstore-config
          secret:
            secretName: {{ include "thanos.objstoreSecretName" . }}
            {{- if .Values.existingObjstoreSecretItems }}
            items: {{- toYaml .Values.existingObjstoreSecretItems | nindent 14 }}
            {{- end }}
        {{- if (include "thanos.httpConfigEnabled" .) }}
        - name: http-config
          secret:
            secretName: {{ include "thanos.httpConfigSecretName" . }}
        {{- if .Values.https.enabled }}
        - name: http-certs
          secret:
            secretName: {{ include "thanos.httpCertsSecretName" . }}
        {{- end }}
        {{- end }}
        {{- if .Values.bucketweb.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.bucketweb.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
