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

{{- if and .Values.storegateway.enabled .Values.storegateway.sharded.enabled }}

{{- $shards := int 0 }}
{{- $hashShards := int 1 }}
{{- $timeShards := int 1 }}
{{- if .Values.storegateway.sharded.hashPartitioning.shards }}
  {{- $hashShards = int .Values.storegateway.sharded.hashPartitioning.shards }}
{{- end }}
{{- if not (empty .Values.storegateway.sharded.timePartitioning) }}
  {{- $timeShards = len .Values.storegateway.sharded.timePartitioning }}
{{- end }}
{{- $shards = mul $hashShards $timeShards | int }}

{{- range $index, $_ := until $shards }}
apiVersion: v1
kind: Service
metadata:
  {{- $svcNamePrefix := include "thanos.storegateway.fullname" $ }}
  name: {{ printf "%s-%s" $svcNamePrefix (toString $index) }}
  namespace: {{ include "common.names.namespace" $ }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $.Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: storegateway
    shard: {{ $index | quote }}
    {{- if and $.Values.metrics.enabled $.Values.metrics.serviceMonitor.enabled }}
    prometheus-operator/monitor: 'true'
    {{- end }}
  {{- if or $.Values.storegateway.service.annotations $.Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list $.Values.storegateway.service.annotations $.Values.commonAnnotations ) "context" $ ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ $.Values.storegateway.service.type }}
  {{- if ne "false" (include "thanos.validateValues.storegateway.sharded.length" (dict "property" $.Values.storegateway.sharded.service.clusterIPs "context" $) ) }}
  clusterIP: {{ index $.Values.storegateway.sharded.service.clusterIPs $index }}
  {{- end }}
  {{- if ne $.Values.storegateway.service.type "ClusterIP" }}
  externalTrafficPolicy: {{ $.Values.storegateway.service.externalTrafficPolicy }}
  {{- end }}
  {{- if ne "false" (include "thanos.validateValues.storegateway.sharded.length" (dict "property" $.Values.storegateway.sharded.service.loadBalancerIPs "context" $) ) }}
  loadBalancerIP: {{ $.Values.storegateway.sharded.service.loadBalancerIPs }}
  {{- end }}
  {{- if and (eq $.Values.storegateway.service.type "LoadBalancer") $.Values.storegateway.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges: {{- toYaml $.Values.storegateway.service.loadBalancerSourceRanges | nindent 4 }}
  {{- end }}
  {{- if $.Values.storegateway.service.ipFamilyPolicy }}
  ipFamilyPolicy: {{ $.Values.storegateway.service.ipFamilyPolicy }}
  {{- end }}
  {{- if $.Values.storegateway.service.ipFamilies }}
  ipFamilies: {{ toYaml $.Values.storegateway.service.ipFamilies | nindent 4 }}
  {{- end }}
  ports:
    - port: {{ $.Values.storegateway.service.ports.http }}
      targetPort: http
      protocol: TCP
      name: http
      {{- if ne "false" (include "thanos.validateValues.storegateway.sharded.length" (dict "property" $.Values.storegateway.sharded.service.http.nodePorts "context" $) ) }}
      nodePort: {{ index $.Values.storegateway.sharded.service.http.nodePorts $index }}
      {{- else if eq $.Values.storegateway.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - port: {{ $.Values.storegateway.service.ports.grpc }}
      targetPort: grpc
      protocol: TCP
      name: grpc
      {{- if ne "false" (include "thanos.validateValues.storegateway.sharded.length" (dict "property" $.Values.storegateway.sharded.service.grpc.nodePorts "context" $) ) }}
      nodePort: {{ index $.Values.storegateway.sharded.service.grpc.nodePorts $index }}
      {{- else if eq $.Values.storegateway.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if $.Values.storegateway.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" $.Values.storegateway.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector:
    {{- if $.Values.storegateway.service.labelSelectorsOverride }}
    {{- include "common.tplvalues.render" (dict "value" $.Values.storegateway.service.labelSelectorsOverride "context" $) | nindent 4 }}
    {{- else }}
    {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list $.Values.storegateway.podLabels $.Values.commonLabels ) "context" $ ) }}
    {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: storegateway
    shard: {{ $index | quote }}
    {{- end }}
---
{{- end }}
{{- end }}
