{{/*
Expand the name of the chart.
*/}}
{{- define "freeleaps-devops-reconciler.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "freeleaps-devops-reconciler.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "freeleaps-devops-reconciler.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "freeleaps-devops-reconciler.labels" -}}
helm.sh/chart: {{ include "freeleaps-devops-reconciler.chart" . }}
{{ include "freeleaps-devops-reconciler.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "freeleaps-devops-reconciler.selectorLabels" -}}
app.kubernetes.io/name: {{ include "freeleaps-devops-reconciler.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "freeleaps-devops-reconciler.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "freeleaps-devops-reconciler.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

{{/*
Create the image reference
*/}}
{{- define "freeleaps-devops-reconciler.image" -}}
{{- $tag := .Values.reconciler.image.tag | default .Chart.AppVersion }}
{{- printf "%s:%s" .Values.reconciler.image.repository $tag }}
{{- end }}

{{/*
Environment variables template
*/}}
{{- define "freeleaps-devops-reconciler.env" -}}
- name: RECONCILER_DEBUG
  value: {{ .Values.env.reconcilerDebug | quote }}
- name: DEFAULT_HTTP_TIMEOUT
  value: {{ .Values.env.defaultHttpTimeout | quote }}
- name: K8S_CLUSTER_DOMAIN
  value: {{ .Values.env.k8sClusterDomain | quote }}
- name: KUBERNETES_API_TIMEOUT
  value: {{ .Values.env.kubernetesApiTimeout | quote }}
- name: AUTO_DISCOVER_K8S_CLUSTER_DOMAIN_MAX_RETRIES
  value: {{ .Values.env.autoDiscoverK8sClusterDomainMaxRetries | quote }}
- name: LOG_LEVEL
  value: {{ .Values.env.logLevel | quote }}
- name: LOG_FORMAT
  value: {{ .Values.env.logFormat | quote }}
- name: OPERATOR_NAMESPACE
  value: {{ .Values.env.operatorNamespace | quote }}
- name: RECONCILE_INTERVAL
  value: {{ .Values.env.reconcileInterval | quote }}
- name: RABBITMQ_HOST
  value: {{ .Values.env.rabbitmq.host | quote }}
- name: RABBITMQ_PORT
  value: {{ .Values.env.rabbitmq.port | quote }}
- name: RABBITMQ_VHOST
  value: {{ .Values.env.rabbitmq.vhost | quote }}
- name: RABBITMQ_USERNAME
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: rabbitmq-username
- name: RABBITMQ_PASSWORD
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: rabbitmq-password
- name: RABBITMQ_INPUT_QUEUE
  value: {{ .Values.env.rabbitmq.inputQueue | quote }}
- name: RABBITMQ_OUTPUT_QUEUE
  value: {{ .Values.env.rabbitmq.outputQueue | quote }}
- name: RABBITMQ_ENABLE_EXCHANGE_BINDING
  value: {{ .Values.env.rabbitmq.enableExchangeBinding | quote }}
- name: RABBITMQ_INPUT_EXCHANGE
  value: {{ .Values.env.rabbitmq.inputExchange | quote }}
- name: RABBITMQ_INPUT_EXCHANGE_TYPE
  value: {{ .Values.env.rabbitmq.inputExchangeType | quote }}
- name: RABBITMQ_INPUT_ROUTING_KEY
  value: {{ .Values.env.rabbitmq.inputRoutingKey | quote }}
- name: RABBITMQ_OUTPUT_EXCHANGE
  value: {{ .Values.env.rabbitmq.outputExchange | quote }}
- name: RABBITMQ_OUTPUT_ROUTING_KEY
  value: {{ .Values.env.rabbitmq.outputRoutingKey | quote }}
- name: JENKINS_ENDPOINT
  value: {{ .Values.env.jenkins.endpoint | quote }}
- name: JENKINS_USERNAME
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: jenkins-username
- name: JENKINS_TOKEN
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: jenkins-token
- name: JENKINS_API_TIMEOUT
  value: {{ .Values.env.jenkins.apiTimeout | quote }}
- name: JENKINS_FOLDER_CREATION_RETRY_COUNT
  value: {{ .Values.env.jenkins.folderCreationRetryCount | quote }}
- name: ARGOCD_ENDPOINT
  value: {{ .Values.env.argocd.endpoint | quote }}
- name: ARGOCD_USERNAME
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: argocd-username
- name: ARGOCD_PASSWORD
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: argocd-password
- name: ARGOCD_API_TIMEOUT
  value: {{ .Values.env.argocd.apiTimeout | quote }}
- name: ARGOCD_RESOURCE_CREATION_TIMEOUT
  value: {{ .Values.env.argocd.resourceCreationTimeout | quote }}
- name: DEFAULT_GIT_USERNAME
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: default-git-username
- name: DEFAULT_GIT_PASSWORD
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: default-git-password
- name: DEFAULT_DOCKER_REGISTRY_USERNAME
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: default-docker-registry-username
- name: DEFAULT_DOCKER_REGISTRY_PASSWORD
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: default-docker-registry-password
- name: DOCKER_REGISTRY_PAT_USERNAME
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: docker-registry-pat-username
- name: DOCKER_REGISTRY_PAT
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: docker-registry-pat
- name: ALLOW_HTTP_GIT_URLS
  value: {{ .Values.env.allowHttpGitUrls | quote }}
- name: DOMAIN_TEMPLATE
  value: {{ .Values.env.networkResources.domainTemplate | quote }}
- name: INGRESS_CLASS_NAME
  value: {{ .Values.env.networkResources.ingressClassName | quote }}
- name: CERT_MANAGER_CLUSTER_ISSUER
  value: {{ .Values.env.networkResources.certManagerClusterIssuer | quote }}
- name: INGRESS_CONTROLLER_IP
  value: {{ .Values.env.networkResources.ingressControllerIp | quote }}
- name: DNS_CREATION_TIMEOUT
  value: {{ .Values.env.networkResources.dnsCreationTimeout | quote }}
- name: CERTIFICATE_ISSUANCE_TIMEOUT
  value: {{ .Values.env.networkResources.certificateIssuanceTimeout | quote }}
- name: INGRESS_READY_TIMEOUT
  value: {{ .Values.env.networkResources.ingressReadyTimeout | quote }}
- name: NETWORK_RESOURCE_CLEANUP_TIMEOUT
  value: {{ .Values.env.networkResources.networkResourceCleanupTimeout | quote }}
- name: NETWORK_RESOURCE_RETRY_COUNT
  value: {{ .Values.env.networkResources.networkResourceRetryCount | quote }}
- name: NETWORK_RESOURCE_RETRY_DELAY
  value: {{ .Values.env.networkResources.networkResourceRetryDelay | quote }}
- name: AZURE_KEY_VAULT_ENDPOINT
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-key-vault-endpoint
- name: AZURE_KEY_VAULT_CLIENT_ID
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-key-vault-client-id
- name: AZURE_KEY_VAULT_CLIENT_SECRET
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-key-vault-client-secret
- name: AZURE_KEY_VAULT_TENANT_ID
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-key-vault-tenant-id
- name: AZURE_KEY_VAULT_RESOURCE_GROUP
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-key-vault-resource-group
- name: AZURE_KEY_VAULT_SUBSCRIPTION_ID
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-key-vault-subscription-id
- name: AZURE_KEY_VAULT_NAME
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-key-vault-name
- name: AZURE_DNS_SUBSCRIPTION_ID
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-dns-subscription-id
- name: AZURE_DNS_TENANT_ID
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-dns-tenant-id
- name: AZURE_DNS_CLIENT_ID
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-dns-client-id
- name: AZURE_DNS_CLIENT_SECRET
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-dns-client-secret
- name: AZURE_DNS_RESOURCE_GROUP
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-dns-resource-group
- name: AZURE_DNS_ZONE_NAME
  valueFrom:
    secretKeyRef:
      name: {{ include "freeleaps-devops-reconciler.fullname" . }}-secrets
      key: azure-dns-zone-name
{{- end }}