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

{{- if and .Values.storegateway.enabled (not .Values.storegateway.sharded.enabled) }}
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ include "thanos.storegateway.fullname" . }}
  namespace: {{ include "common.names.namespace" . }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: storegateway
    {{- if .Values.storegateway.statefulsetLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.storegateway.statefulsetLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if or .Values.commonAnnotations .Values.storegateway.statefulsetAnnotations }}
  annotations:
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if .Values.storegateway.statefulsetAnnotations }}
    {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.statefulsetAnnotations "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  {{- if not (or .Values.storegateway.autoscaling.enabled .Values.storegateway.externalAutoscaler) }}
  replicas: {{ .Values.storegateway.replicaCount }}
  {{- end }}
  revisionHistoryLimit: {{ .Values.storegateway.revisionHistoryLimit }}
  podManagementPolicy: {{ .Values.storegateway.podManagementPolicy }}
  serviceName: {{ include "thanos.storegateway.fullname" . }}-headless
  {{- if .Values.storegateway.updateStrategy }}
  updateStrategy: {{- toYaml .Values.storegateway.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.storegateway.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: storegateway
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: storegateway
      {{- if or .Values.commonAnnotations .Values.storegateway.podAnnotations (include "thanos.storegateway.createConfigmap" $) (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 (include "thanos.storegateway.createConfigmap" .) }}
        checksum/storegateway-configuration: {{ include "thanos.storegatewayConfigMap" . | sha256sum }}
        {{- end }}
        {{- if .Values.storegateway.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
      {{- include "thanos.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ include "thanos.storegateway.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.storegateway.automountServiceAccountToken }}
      {{- if .Values.storegateway.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.storegateway.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.storegateway.podAffinityPreset "component" "storegateway" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.storegateway.podAntiAffinityPreset "component" "storegateway" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.storegateway.nodeAffinityPreset.type "key" .Values.storegateway.nodeAffinityPreset.key "values" .Values.storegateway.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.storegateway.dnsConfig }}
      dnsConfig: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.dnsConfig "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.storegateway.dnsPolicy }}
      dnsPolicy: {{ .Values.storegateway.dnsPolicy | quote }}
      {{- end }}
      {{- if .Values.storegateway.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.storegateway.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.storegateway.priorityClassName }}
      priorityClassName: {{ .Values.storegateway.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.storegateway.schedulerName }}
      schedulerName: {{ .Values.storegateway.schedulerName }}
      {{- end }}
      {{- if .Values.storegateway.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.storegateway.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.storegateway.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if or .Values.storegateway.initContainers (and .Values.volumePermissions.enabled .Values.storegateway.persistence.enabled) }}
      initContainers:
        {{- if and .Values.volumePermissions.enabled .Values.storegateway.persistence.enabled }}
        - name: init-chmod-data
          image: {{ include "thanos.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - sh
            - -c
            - |
              mkdir -p /data
              chown -R "{{ .Values.storegateway.containerSecurityContext.runAsUser }}:{{ .Values.storegateway.podSecurityContext.fsGroup }}" /data
          securityContext:
            runAsUser: 0
          volumeMounts:
            - name: data
              mountPath: /data
        {{- end }}
        {{- if .Values.storegateway.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.initContainers "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        {{- if .Values.storegateway.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.sidecars "context" $) | nindent 8 }}
        {{- end }}
        - name: storegateway
          image: {{ include "thanos.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.storegateway.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.storegateway.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.storegateway.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.command "context" $) | nindent 12 }}
          {{- end }}
          args:
            {{- if .Values.storegateway.args }}
            {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.args "context" $) | nindent 12 }}
            {{- else }}
            - store
            - --log.level={{ .Values.storegateway.logLevel }}
            - --log.format={{ .Values.storegateway.logFormat }}
            - --grpc-address=0.0.0.0:{{ .Values.storegateway.containerPorts.grpc }}
            - --http-address=0.0.0.0:{{ .Values.storegateway.containerPorts.http }}
            - --data-dir=/data
            - --objstore.config-file=/conf/objstore.yml
            {{- if (include "thanos.httpConfigEnabled" .) }}
            - --http.config=/conf/http/http-config.yml
            {{- end }}
            {{- if .Values.indexCacheConfig }}
            - --index-cache.config-file=/conf/cache/index-cache.yml
            {{- end }}
            {{- if .Values.bucketCacheConfig }}
            - --store.caching-bucket.config-file=/conf/cache/bucket-cache.yml
            {{- end }}
            {{- if or .Values.storegateway.config .Values.storegateway.existingConfigmap }}
            - --index-cache.config-file=/conf/cache/config.yml
            {{- end }}
            {{- if .Values.storegateway.grpc.server.tls.enabled }}
            - --grpc-server-tls-cert=/certs/grpc/{{ include "common.secrets.key" (dict "existingSecret" .Values.storegateway.grpc.server.tls.existingSecret "key" "tls-cert") }}
            - --grpc-server-tls-key=/certs/grpc/{{ include "common.secrets.key" (dict "existingSecret" .Values.storegateway.grpc.server.tls.existingSecret "key" "tls-key") }}
            {{- if .Values.storegateway.grpc.server.tls.clientAuthEnabled }}
            - --grpc-server-tls-client-ca=/certs/grpc/{{ include "common.secrets.key" (dict "existingSecret" .Values.storegateway.grpc.server.tls.existingSecret "key" "ca-cert") }}
            {{- end }}
            {{- end }}
            {{- if .Values.storegateway.extraFlags }}
            {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.extraFlags "context" $) | nindent 12 }}
            {{- end }}
            {{- end }}
          {{- if .Values.storegateway.extraEnvVars }}
          env: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          {{- if or .Values.storegateway.extraEnvVarsCM .Values.storegateway.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.storegateway.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.storegateway.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.storegateway.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.storegateway.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.storegateway.containerPorts.http }}
              protocol: TCP
            - name: grpc
              containerPort: {{ .Values.storegateway.containerPorts.grpc }}
              protocol: TCP
          {{- if .Values.storegateway.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.storegateway.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.storegateway.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.storegateway.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.storegateway.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.storegateway.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.storegateway.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.storegateway.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.storegateway.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.storegateway.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.storegateway.resources }}
          resources: {{- toYaml .Values.storegateway.resources | nindent 12 }}
          {{- else if ne .Values.storegateway.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.storegateway.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if (include "thanos.httpConfigEnabled" .) }}
            - name: http-config
              mountPath: /conf/http
            {{- if .Values.https.enabled }}
            - name: http-certs
              mountPath: /certs
            {{- end }}
            {{- end }}
            - name: objstore-config
              mountPath: /conf
            - name: data
              mountPath: /data
            {{- if or (include "thanos.storegateway.createConfigmap" .) .Values.storegateway.existingConfigmap }}
            - name: cache-config
              mountPath: /conf/cache
            {{- end }}
            {{- if .Values.storegateway.grpc.server.tls.enabled }}
            - name: grpc-server-tls
              mountPath: /certs/grpc
            {{- end }}
            {{- if .Values.storegateway.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.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 .Values.storegateway.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
        {{- if or (include "thanos.storegateway.createConfigmap" .) .Values.storegateway.existingConfigmap }}
        - name: cache-config
          configMap:
            name: {{ include "thanos.storegateway.configmapName" . }}
        {{- 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.storegateway.grpc.server.tls.enabled }}
        - name: grpc-server-tls
          secret:
            secretName: {{ include "common.secrets.name" (dict "existingSecret" .Values.storegateway.grpc.server.tls.existingSecret "defaultNameSuffix" "store-grpc-server" "context" $) }}
        {{- end }}
  {{- if and .Values.storegateway.persistence.enabled .Values.storegateway.persistence.existingClaim }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ .Values.storegateway.persistence.existingClaim }}
  {{- else if not .Values.storegateway.persistence.enabled }}
        - name: data
          emptyDir: {}
  {{- else if and .Values.storegateway.persistence.enabled (not .Values.storegateway.persistence.existingClaim) }}
  {{- if .Values.storegateway.persistentVolumeClaimRetentionPolicy.enabled }}
  persistentVolumeClaimRetentionPolicy:
    whenDeleted: {{ .Values.storegateway.persistentVolumeClaimRetentionPolicy.whenDeleted }}
    whenScaled: {{ .Values.storegateway.persistentVolumeClaimRetentionPolicy.whenScaled }}
  {{- end }}
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: data
        {{- if .Values.storegateway.persistence.labels }}
        labels: {{- include "common.tplvalues.render" ( dict "value" .Values.storegateway.persistence.labels "context" $) | nindent 10 }}
        {{- end }}
        {{- if .Values.storegateway.persistence.annotations }}
        annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.storegateway.persistence.annotations "context" $) | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
        {{- range .Values.storegateway.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.storegateway.persistence.size | quote }}
        {{- include "common.storage.class" (dict "persistence" .Values.storegateway.persistence "global" .Values.global) | nindent 8 }}
  {{- end }}
{{- end }}
