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

apiVersion: v1
kind: Service
metadata:
  name: {{ include "jupyterhub.hub.name" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: hub
  {{- if or .Values.hub.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.hub.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.hub.service.type }}
  {{- if and .Values.hub.service.clusterIP (eq .Values.hub.service.type "ClusterIP") }}
  clusterIP: {{ .Values.hub.service.clusterIP }}
  {{- end }}
  {{- if or (eq .Values.hub.service.type "LoadBalancer") (eq .Values.hub.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.hub.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.hub.service.type "LoadBalancer") (not (empty .Values.hub.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.hub.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.hub.service.type "LoadBalancer") (not (empty .Values.hub.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.hub.service.loadBalancerIP }}
  {{- end }}
  {{- if .Values.hub.service.sessionAffinity }}
  sessionAffinity: {{ .Values.hub.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.hub.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.hub.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.hub.service.ports.http }}
      targetPort: http
      protocol: TCP
      {{- if and (or (eq .Values.hub.service.type "NodePort") (eq .Values.hub.service.type "LoadBalancer")) (not (empty .Values.hub.service.nodePorts.http)) }}
      nodePort: {{ .Values.hub.service.nodePorts.http }}
      {{- else if eq .Values.hub.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.hub.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.hub.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.hub.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: hub
