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

{{- if and .Values.storegateway.enabled (not .Values.storegateway.sharded.enabled) }}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "thanos.storegateway.fullname" . }}
  namespace: {{ include "common.names.namespace" . }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.storegateway.service.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: storegateway
    {{- include "thanos.servicemonitor.matchLabels" . | nindent 4 -}}
  {{- 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 and .Values.storegateway.service.clusterIP (eq .Values.storegateway.service.type "ClusterIP") }}
  clusterIP: {{ .Values.storegateway.service.clusterIP }}
  {{- end }}
  {{- if ne .Values.storegateway.service.type "ClusterIP" }}
  externalTrafficPolicy: {{ .Values.storegateway.service.externalTrafficPolicy }}
  {{- end }}
  {{- if and .Values.storegateway.service.loadBalancerIP (eq .Values.storegateway.service.type "LoadBalancer") }}
  loadBalancerIP: {{ .Values.storegateway.service.loadBalancerIP }}
  {{- 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 and (or (eq .Values.storegateway.service.type "NodePort") (eq .Values.storegateway.service.type "LoadBalancer")) .Values.storegateway.service.nodePorts.http }}
      nodePort: {{ .Values.storegateway.service.nodePorts.http }}
      {{- else if eq .Values.storegateway.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - port: {{ .Values.storegateway.service.ports.grpc }}
      targetPort: grpc
      protocol: TCP
      name: grpc
      {{- if and (or (eq .Values.storegateway.service.type "NodePort") (eq .Values.storegateway.service.type "LoadBalancer")) .Values.storegateway.service.nodePorts.grpc }}
      nodePort: {{ .Values.storegateway.service.nodePorts.grpc }}
      {{- 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
    {{- end }}
{{- end }}
