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

apiVersion: v1
kind: Service
metadata:
  name: {{ include "chainloop.cas.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{ include "chainloop.cas.labels" . | nindent 4 }}
  {{- if or .Values.cas.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.cas.service.annotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.cas.service.type }}
  {{- if and .Values.cas.service.clusterIP (eq .Values.cas.service.type "ClusterIP") }}
  clusterIP: {{ .Values.cas.service.clusterIP }}
  {{- end }}
  {{- if .Values.cas.service.sessionAffinity }}
  sessionAffinity: {{ .Values.cas.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.cas.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.cas.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.cas.service.type "LoadBalancer") (eq .Values.cas.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.cas.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.cas.service.type "LoadBalancer") (not (empty .Values.cas.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.cas.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.cas.service.type "LoadBalancer") (not (empty .Values.cas.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.cas.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http
      {{- $port := coalesce .Values.cas.service.port .Values.cas.service.ports.http }}
      port: {{ $port }}
      {{- if not (eq $port .Values.cas.containerPorts.http) }}
      targetPort: {{ .Values.cas.containerPorts.http }}
      {{- end }}
      protocol: TCP
      {{- if and (or (eq .Values.cas.service.type "NodePort") (eq .Values.cas.service.type "LoadBalancer")) (not (empty .Values.cas.service.nodePorts.http)) }}
      nodePort: {{ .Values.cas.service.nodePorts.http }}
      {{- else if eq .Values.cas.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.cas.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.cas.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector: {{ include "chainloop.cas.selectorLabels" . | nindent 4 }}
