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

{{- if .Values.server.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-active" (include "vault.server.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: vault
    app.kubernetes.io/component: server
  {{- if or .Values.server.service.active.annotations .Values.commonAnnotations }}
  annotations:
    {{- if or .Values.server.service.active.annotations .Values.commonAnnotations (and .Values.server.metrics.enabled .Values.server.metrics.annotations) }}
    {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.server.service.active.annotations .Values.commonAnnotations ) "context" . ) }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
    {{- end }}
    {{- if and .Values.server.metrics.enabled .Values.server.metrics.annotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.server.metrics.annotations "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ .Values.server.service.active.type }}
  {{- if and .Values.server.service.active.clusterIP (eq .Values.server.service.active.type "ClusterIP") }}
  clusterIP: {{ .Values.server.service.active.clusterIP }}
  {{- end }}
  {{- if .Values.server.service.active.sessionAffinity }}
  sessionAffinity: {{ .Values.server.service.active.sessionAffinity }}
  {{- end }}
  {{- if .Values.server.service.active.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.server.service.active.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.server.service.active.type "LoadBalancer") (eq .Values.server.service.active.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.server.service.active.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.server.service.active.type "LoadBalancer") (not (empty .Values.server.service.active.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.server.service.active.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.server.service.active.type "LoadBalancer") (not (empty .Values.server.service.active.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.server.service.active.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.server.service.active.ports.http }}
      protocol: TCP
      {{- if and (or (eq .Values.server.service.active.type "NodePort") (eq .Values.server.service.active.type "LoadBalancer")) (not (empty .Values.server.service.active.nodePorts.http)) }}
      nodePort: {{ .Values.server.service.active.nodePorts.http }}
      {{- else if eq .Values.server.service.active.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: http
    - name: https-internal
      port: {{ .Values.server.service.active.ports.internal }}
      protocol: TCP
      {{- if and (or (eq .Values.server.service.active.type "NodePort") (eq .Values.server.service.active.type "LoadBalancer")) (not (empty .Values.server.service.active.nodePorts.internal)) }}
      nodePort: {{ .Values.server.service.active.nodePorts.internal }}
      {{- else if eq .Values.server.service.active.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: https-internal
    {{- if .Values.server.service.active.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.server.service.active.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.injector.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: server
    app.kubernetes.io/part-of: vault
    vault-active: "true"
{{- end }}
