{{- if and .Values.storegateway.enabled (not .Values.storegateway.sharded.enabled) }}
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ include "common.names.fullname" . }}-storegateway
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: storegateway
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.storegateway.autoscaling.enabled }}
  replicas: {{ .Values.storegateway.replicaCount }}
  {{- end }}
  podManagementPolicy: {{ .Values.storegateway.podManagementPolicy }}
  serviceName: {{ include "common.names.fullname" . }}-storegateway-headless
  {{- if .Values.storegateway.updateStrategy }}
  updateStrategy: {{- toYaml .Values.storegateway.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: storegateway
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: storegateway
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.storegateway.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.podLabels "context" $) | nindent 8 }}
        {{- end }}
      annotations:
        checksum/ojbstore-configuration: {{ include (print $.Template.BasePath "/objstore-secret.yaml") . | sha256sum }}
        {{- if (include "thanos.storegateway.createConfigmap" .) }}
        checksum/storegateway-configuration: {{ include (print $.Template.BasePath "/storegateway/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.storegateway.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "thanos.imagePullSecrets" . | nindent 6 }}
      serviceAccount: {{ include "thanos.serviceAccount.name" (dict "component" "storegateway" "context" $) }}
      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" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.storegateway.podAntiAffinityPreset "component" "storegateway" "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.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: {{- omit .Values.storegateway.podSecurityContext "enabled" | toYaml | 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: {{- omit .Values.storegateway.containerSecurityContext "enabled" | toYaml | 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:10901
            - --http-address=0.0.0.0:10902
            - --data-dir=/data
            - --objstore.config-file=/conf/objstore.yml
            {{- if .Values.indexCacheConfig }}
            - --index-cache.config-file=/conf/index-cache.yml
            {{- end }}
            {{- if .Values.bucketCacheConfig }}
            - --store.caching-bucket.config-file=/conf/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/{{ include "common.secrets.key" (dict "existingSecret" .Values.storegateway.grpc.server.tls.existingSecret "key" "tls-cert") }}
            - --grpc-server-tls-key=/certs/{{ include "common.secrets.key" (dict "existingSecret" .Values.storegateway.grpc.server.tls.existingSecret "key" "tls-key") }}
            - --grpc-server-tls-client-ca=/certs/{{ include "common.secrets.key" (dict "existingSecret" .Values.storegateway.grpc.server.tls.existingSecret "key" "ca-cert") }}
            {{- end }}
            {{- if .Values.storegateway.extraFlags }}
            {{- .Values.storegateway.extraFlags | toYaml | 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: 10902
              protocol: TCP
            - name: grpc
              containerPort: 10901
              protocol: TCP
          {{- if .Values.storegateway.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.storegateway.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /-/healthy
              port: http
          {{- else if .Values.storegateway.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.storegateway.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.storegateway.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /-/ready
              port: http
          {{- else if .Values.storegateway.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.storegateway.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.storegateway.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /-/ready
              port: http
          {{- else if .Values.storegateway.customReadinessProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.storegateway.customStartupProbe "context" $) | nindent 12 }}
          {{- 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 }}
          {{- end }}
          volumeMounts:
            - name: objstore-config
              mountPath: /conf
            - name: data
              mountPath: /data
            {{- if or .Values.storegateway.config .Values.storegateway.existingConfigmap }}
            - name: cache-config
              mountPath: /conf/cache
            {{- end }}
            {{- if .Values.storegateway.grpc.server.tls.enabled }}
            - name: grpc-server-tls
              mountPath: /certs
            {{- 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 .Values.storegateway.config .Values.storegateway.existingConfigmap }}
        - name: cache-config
          configMap:
            name: {{ include "thanos.storegateway.configmapName" . }}
        {{- 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) }}
  volumeClaimTemplates:
    - metadata:
        name: data
        {{- 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 }}
