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

{{- if .Values.injector.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "vault.injector.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.injector.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: vault
    app.kubernetes.io/component: injector
  {{- if or .Values.injector.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.injector.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.injector.service.type }}
  {{- if and .Values.injector.service.clusterIP (eq .Values.injector.service.type "ClusterIP") }}
  clusterIP: {{ .Values.injector.service.clusterIP }}
  {{- end }}
  {{- if .Values.injector.service.sessionAffinity }}
  sessionAffinity: {{ .Values.injector.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.injector.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.injector.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.injector.service.type "LoadBalancer") (eq .Values.injector.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.injector.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.injector.service.type "LoadBalancer") (not (empty .Values.injector.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.injector.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.injector.service.type "LoadBalancer") (not (empty .Values.injector.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.injector.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: https
      port: {{ .Values.injector.service.ports.https }}
      protocol: TCP
      {{- if and (or (eq .Values.injector.service.type "NodePort") (eq .Values.injector.service.type "LoadBalancer")) (not (empty .Values.injector.service.nodePorts.https)) }}
      nodePort: {{ .Values.injector.service.nodePorts.https }}
      {{- else if eq .Values.injector.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: https
    {{- if .Values.injector.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.injector.service.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/part-of: vault
    app.kubernetes.io/component: injector
{{- end }}
