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

apiVersion: v1
kind: Service
metadata:
  name:  {{ include "flink.taskmanager.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flink
    app.kubernetes.io/component: taskmanager
  {{- if or .Values.taskmanager.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.taskmanager.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.taskmanager.service.type }}
  {{- if and (eq .Values.taskmanager.service.type "LoadBalancer") (not (empty .Values.taskmanager.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.taskmanager.service.loadBalancerIP }}
  {{- end }}
  {{- if and (eq .Values.taskmanager.service.type "LoadBalancer") (not (empty .Values.taskmanager.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.taskmanager.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and .Values.taskmanager.service.clusterIP (eq .Values.taskmanager.service.type "ClusterIP") }}
  clusterIP: {{ .Values.taskmanager.service.clusterIP }}
  {{- end }}
  {{- if .Values.taskmanager.service.sessionAffinity }}
  sessionAffinity: {{ .Values.taskmanager.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.taskmanager.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.taskmanager.service.type "LoadBalancer") (eq .Values.taskmanager.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.taskmanager.service.externalTrafficPolicy | quote }}
  {{- end }}
  ports:
    - name: tcp-data
      port: {{ .Values.taskmanager.service.ports.data }}
      targetPort: data
      {{- if and (or (eq .Values.taskmanager.service.type "NodePort") (eq .Values.taskmanager.service.type "LoadBalancer")) (not (empty .Values.taskmanager.service.nodePorts.data)) }}
      nodePort: {{ .Values.taskmanager.service.nodePorts.data }}
      {{- else if eq .Values.taskmanager.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - name: tcp-rpc
      port: {{ .Values.taskmanager.service.ports.rpc }}
      targetPort: rpc
      {{- if and (or (eq .Values.taskmanager.service.type "NodePort") (eq .Values.taskmanager.service.type "LoadBalancer")) (not (empty .Values.taskmanager.service.nodePorts.rpc)) }}
      nodePort: {{ .Values.taskmanager.service.nodePorts.rpc }}
      {{- else if eq .Values.taskmanager.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - name: tcp-internal-metrics
      port: {{ .Values.taskmanager.service.ports.internalMetrics }}
      targetPort: int-metrics
      {{- if and (or (eq .Values.taskmanager.service.type "NodePort") (eq .Values.taskmanager.service.type "LoadBalancer")) (not (empty .Values.taskmanager.service.nodePorts.internalMetrics)) }}
      nodePort: {{ .Values.taskmanager.service.nodePorts.internalMetrics }}
      {{- else if eq .Values.taskmanager.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.taskmanager.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.taskmanager.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.taskmanager.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: flink
    app.kubernetes.io/component: taskmanager
