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

{{- if and .Values.hubble.relay.enabled .Values.hubble.ui.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "cilium.hubble.ui.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.hubble.ui.frontend.image "chart" .Chart ) ) }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cilium
    app.kubernetes.io/component: hubble-ui
  {{- if or .Values.hubble.ui.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.hubble.ui.service.annotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.hubble.ui.service.type }}
  {{- if and .Values.hubble.ui.service.clusterIP (eq .Values.hubble.ui.service.type "ClusterIP") }}
  clusterIP: {{ .Values.hubble.ui.service.clusterIP }}
  {{- end }}
  {{- if .Values.hubble.ui.service.sessionAffinity }}
  sessionAffinity: {{ .Values.hubble.ui.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.hubble.ui.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.hubble.ui.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.hubble.ui.service.type "LoadBalancer") (eq .Values.hubble.ui.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.hubble.ui.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.hubble.ui.service.type "LoadBalancer") (not (empty .Values.hubble.ui.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.hubble.ui.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.hubble.ui.service.type "LoadBalancer") (not (empty .Values.hubble.ui.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.hubble.ui.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.hubble.ui.service.ports.http }}
      targetPort: frontend
      protocol: TCP
      {{- if and (or (eq .Values.hubble.ui.service.type "NodePort") (eq .Values.hubble.ui.service.type "LoadBalancer")) (not (empty .Values.hubble.ui.service.nodePorts.http)) }}
      nodePort: {{ .Values.hubble.ui.service.nodePorts.http }}
      {{- else if eq .Values.hubble.ui.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.hubble.ui.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.hubble.ui.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" (dict "values" (list .Values.hubble.ui.podLabels .Values.commonLabels $versionLabel) "context" .) | fromYaml }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: cilium
    app.kubernetes.io/component: hubble-ui
{{- end }}
