{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "frontend.name" -}}
{{- include "common.names.name" . -}}
{{- 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 "frontend.fullname" -}}
{{- include "common.names.fullname" . -}}
{{- end -}}

{{/*
Return the proper frontend image name
*/}}
{{- define "frontend.image" -}}
{{ include "common.images.image" (dict "imageRoot" .Values.image "global" .Values.global "appVer" .Chart.AppVersion) }}
{{- end -}}

{{/*
Return the proper observability image name
*/}}
{{- define "observability.image" -}}
{{ include "common.images.image" (dict "imageRoot" .Values.observability.image "global" .Values.global) }}
{{- end -}}

{{/*
Return the proper router image name
*/}}
{{- define "router.image" -}}
{{ include "common.images.image" (dict "imageRoot" .Values.router.image "global" .Values.global) }}
{{- end -}}

{{/*
Return the proper image name (for the init container volume-permissions image)
*/}}
{{- define "frontend.initContainers.image" -}}
{{ include "common.images.image" (dict "imageRoot" .Values.initContainers.image "global" .Values.global) }}
{{- end -}}

{{/*
Return the proper Docker Image Registry Secret Names
*/}}
{{- define "frontend.imagePullSecrets" -}}
{{ include "common.images.pullSecrets" (dict "images" (list .Values.image .Values.initContainers.image) "global" .Values.global) }}
{{- end -}}

{{/*
Return the proper observability Docker Image Registry Secret Names
*/}}
{{- define "observability.imagePullSecrets" -}}
{{ include "common.images.pullSecrets" (dict "images" (list .Values.observability.image .Values.initContainers.image) "global" .Values.global) }}
{{- end -}}

{{/*
Return podAnnotations
*/}}
{{- define "frontend.podAnnotations" -}}
{{- if .Values.podAnnotations }}
{{ include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) }}
{{- end }}
{{- if and .Values.metrics.enabled .Values.metrics.podAnnotations }}
{{ include "common.tplvalues.render" (dict "value" .Values.metrics.podAnnotations "context" $) }}
{{- end }}
{{- end -}}

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

{{/*
Resolve joinKey value
*/}}
{{- define "frontend.joinKey" -}}
{{- if .Values.global.joinKey -}}
{{- .Values.global.joinKey -}}
{{- else if .Values.joinKey -}}
{{- .Values.joinKey -}}
{{- end -}}
{{- end -}}

{{/*
Resolve masterKey value
*/}}
{{- define "frontend.masterKey" -}}
{{- if .Values.global.masterKey -}}
{{- .Values.global.masterKey -}}
{{- else if .Values.masterKey -}}
{{- .Values.masterKey -}}
{{- end -}}
{{- end -}}

{{/*
Resolve joinKeySecretName value
*/}}
{{- define "frontend.joinKeySecretName" -}}
{{- if .Values.global.joinKeySecretName -}}
{{- .Values.global.joinKeySecretName -}}
{{- else if .Values.joinKeySecretName -}}
{{- .Values.joinKeySecretName -}}
{{- else -}}
{{ include "frontend.fullname" . }}
{{- end -}}
{{- end -}}

{{/*
Resolve masterKeySecretName value
*/}}
{{- define "frontend.masterKeySecretName" -}}
{{- if .Values.global.masterKeySecretName -}}
{{- .Values.global.masterKeySecretName -}}
{{- else if .Values.masterKeySecretName -}}
{{- .Values.masterKeySecretName -}}
{{- else -}}
{{ include "frontend.fullname" . }}
{{- end -}}
{{- end -}}

{{/*
Scheme (http/https) based on Access TLS enabled/disabled
*/}}
{{- define "frontend.scheme" -}}
{{- if .Values.router.tlsEnabled -}}
{{- printf "%s" "https" -}}
{{- else -}}
{{- printf "%s" "http" -}}
{{- end -}}
{{- end -}}

{{/*
Resolve frontend requiredServiceTypes value
*/}}
{{- define "frontend.router.requiredServiceTypes" -}}
{{- $requiredTypes := "jffe" -}}
{{- if .Values.observability.enabled -}}
  {{- $requiredTypes = printf "%s,%s" $requiredTypes "jfob" -}}
{{- end -}}
{{- $requiredTypes -}}
{{- end -}}

{{/*
Resolve jfrogUrl value
*/}}
{{- define "frontend.jfrogUrl" -}}
{{- if .Values.global.jfrogUrl -}}
{{- .Values.global.jfrogUrl -}}
{{- else if .Values.jfrogUrl -}}
{{- .Values.jfrogUrl -}}
{{- end -}}
{{- end -}}

{{/*
Render container resources with optional CPU limits control.
This helper allows disabling CPU limits while keeping other resource settings intact.
Usage: {{ include "frontend.containerResources" (dict "resources" .Values.resources) }}

When resources.disableCpuLimits is true:
  - CPU limits will NOT be rendered in the pod spec
  - CPU requests are still rendered if specified
  - Memory limits and requests are unaffected

When resources.disableCpuLimits is false or not set (default):
  - All resource settings are rendered as-is (backward compatible)

This feature helps avoid CFS throttling for CPU-bound or bursty workloads.
*/}}
{{- define "frontend.containerResources" -}}
{{- $resources := .resources | default (dict) -}}
{{- $disableCpuLimits := default false $resources.disableCpuLimits -}}
{{- $result := dict -}}
{{- /* Copy all keys except the helm-only disableCpuLimits flag */ -}}
{{- range $key, $value := $resources -}}
{{- if ne $key "disableCpuLimits" -}}
{{- $_ := set $result $key $value -}}
{{- end -}}
{{- end -}}
{{- /* When disableCpuLimits=true, rebuild limits omitting only cpu */ -}}
{{- if $disableCpuLimits -}}
{{- $newLimits := dict -}}
{{- range $key, $value := ($resources.limits | default (dict)) -}}
{{- if ne $key "cpu" -}}
{{- $_ := set $newLimits $key $value -}}
{{- end -}}
{{- end -}}
{{- if $newLimits -}}
{{- $_ := set $result "limits" $newLimits -}}
{{- else -}}
{{- $_ := unset $result "limits" -}}
{{- end -}}
{{- end -}}
{{- toYaml $result -}}
{{- end -}}
