# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

{{/*
Define a helper template for creating ExternalName services.
This template takes three parameters:
- name: The name of the service to create
- namespace: The namespace where the service should be created
- externalName: The fully qualified domain name to redirect to
*/}}
{{- define "external-name-service" -}}
apiVersion: v1
kind: Service
metadata:
  name: {{ .name }}
  namespace: {{ .namespace }}
spec:
  type: ExternalName
  externalName: {{ .externalName }}
{{- end }}

{{/* Define namespace variables for better readability */}}
{{- $namespace := .Release.Namespace }}
{{- $workflowComputingUnitPoolNamespace := .Values.workflowComputingUnitPool.namespace }}

{{/* 
Create ExternalName services in the workflow namespace to allow compute units
to access services in the main namespace using the same service names.
*/}}

{{/* File service ExternalName */}}
{{- include "external-name-service" (dict 
  "name" (printf "%s-svc" .Values.fileService.name)
  "namespace" $workflowComputingUnitPoolNamespace
  "externalName" (printf "%s-svc.%s.svc.cluster.local" .Values.fileService.name $namespace)
) | nindent 0 }}

---
{{/* Config service ExternalName */}}
{{- include "external-name-service" (dict 
  "name" (printf "%s-svc" .Values.configService.name)
  "namespace" $workflowComputingUnitPoolNamespace
  "externalName" (printf "%s-svc.%s.svc.cluster.local" .Values.configService.name $namespace)
) | nindent 0 }}

---
{{/* PostgreSQL ExternalName */}}
{{- include "external-name-service" (dict 
  "name" (printf "%s-postgresql" .Release.Name)
  "namespace" $workflowComputingUnitPoolNamespace
  "externalName" (printf "%s-postgresql.%s.svc.cluster.local" .Release.Name $namespace)
) | nindent 0 }}

---
{{/* Webserver ExternalName */}}
{{- include "external-name-service" (dict 
  "name" (printf "%s-svc" .Values.webserver.name)
  "namespace" $workflowComputingUnitPoolNamespace
  "externalName" (printf "%s-svc.%s.svc.cluster.local" .Values.webserver.name $namespace)
) | nindent 0 }}

---
{{/* MinIO ExternalName */}}
{{- include "external-name-service" (dict 
  "name" (printf "%s-minio" .Release.Name)
  "namespace" $workflowComputingUnitPoolNamespace
  "externalName" (printf "%s-minio.%s.svc.cluster.local" .Release.Name $namespace)
) | nindent 0 }}


