Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions chart/templates/backend-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,30 @@ spec:
{{- include "lightrun-backend-crons.environmentVariables" . | nindent 12 }}
- name: SPRING_PROFILES_ACTIVE
value: "prod,swagger,cluster"
{{- if .Values.deployments.backend.extraEnvs }}
{{- toYaml .Values.deployments.backend.extraEnvs | nindent 12 }}
{{- if not (include "list-of-maps-contains" (list .Values.deployments.backend.extraEnvs "_JAVA_OPTIONS") ) }}
- name: "_JAVA_OPTIONS"
value: {{- toYaml (include "calculate-heap-size" .Values.deployments.backend) | nindent 21 }}
{{- end }}
{{- else }}
- name: "_JAVA_OPTIONS"
value: {{- toYaml (include "calculate-heap-size" .Values.deployments.backend) | nindent 21 }}
{{- end }}
{{- $envs := .Values.deployments.backend.extraEnvs | default (list) -}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call it $extraEnvs

{{- $memMi := include "mem-to-mi" (.Values.deployments.backend.resources.memory | default "") | int -}}
{{- $javaVal := (include "get-env" (list $envs "_JAVA_OPTIONS") | trim) -}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call it $javaOptions

{{- /* start with user extraEnvs, but drop _JAVA_OPTIONS */ -}}
{{- $rendered := list -}}
{{- range $e := $envs -}}
{{- $name := $e.name | default "" -}}
{{- if ne $name "_JAVA_OPTIONS" -}}
{{- $rendered = append $rendered $e -}}
{{- end -}}
{{- end -}}

{{- /* add enhanced _JAVA_OPTIONS calculation */ -}}
{{- $enhancedJavaOpts := include "calculate-enhanced-java-options" (dict "deployment" .Values.deployments.backend "existingJavaOpts" $javaVal) -}}
{{- if ne $enhancedJavaOpts "" -}}
{{- $rendered = append $rendered (dict "name" "_JAVA_OPTIONS" "value" $enhancedJavaOpts) -}}
{{- else if eq $javaVal "" -}}
{{- $rendered = append $rendered (dict "name" "_JAVA_OPTIONS" "value" "") -}}
{{- end -}}

{{- if $rendered }}
{{ toYaml $rendered | nindent 12 }}
{{- end }}
{{- /* >>> END updated env-emitting block <<< */}}

# waiting for mysql, rabbitmq and keycloak initialization
initContainers:
Expand Down
51 changes: 44 additions & 7 deletions chart/templates/crons/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,50 @@ spec:
{{- include "lightrun-backend-crons.environmentVariables" . | nindent 12 }}
- name: SPRING_PROFILES_ACTIVE
value: "prod,swagger,cluster,cron"
{{- $mergedExtraEnvs := include "lightrun-crons.mergedExtraEnvs" . }}
{{- if $mergedExtraEnvs }}
{{ $mergedExtraEnvs | nindent 12 }}
{{- end }}
{{- if and (not (include "list-of-maps-contains" (list .Values.deployments.crons.extraEnvs "_JAVA_OPTIONS"))) (not (include "list-of-maps-contains" (list .Values.deployments.backend.extraEnvs "_JAVA_OPTIONS"))) }}
- name: "_JAVA_OPTIONS"
value: {{- toYaml (include "calculate-heap-size" (list .Values.deployments.crons 1 3)) | nindent 21 }}
{{- /* Enhanced _JAVA_OPTIONS logic for crons */ -}}
{{- $backendExtraEnvs := .Values.deployments.backend.extraEnvs | default list -}}
{{- $cronsExtraEnvs := .Values.deployments.crons.extraEnvs | default list -}}
{{- $mergedEnvs := list -}}
{{- $javaVal := "" -}}
{{- /* First, add all backend extraEnvs except _JAVA_OPTIONS */ -}}
{{- range $backendExtraEnvs -}}
{{- $backendEnv := . -}}
{{- $isOverridden := false -}}
{{- if eq $backendEnv.name "_JAVA_OPTIONS" -}}
{{- $javaVal = (printf "%v" $backendEnv.value) -}}
{{- else -}}
{{- /* Check if this env var is overridden in crons */ -}}
{{- range $cronsExtraEnvs -}}
{{- if eq .name $backendEnv.name -}}
{{- $isOverridden = true -}}
{{- end -}}
{{- end -}}
{{- /* Only add backend env if not overridden by crons */ -}}
{{- if not $isOverridden -}}
{{- $mergedEnvs = append $mergedEnvs $backendEnv -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{- /* Then, add all crons extraEnvs except _JAVA_OPTIONS */ -}}
{{- range $cronsExtraEnvs -}}
{{- if eq .name "_JAVA_OPTIONS" -}}
{{- $javaVal = (printf "%v" .value) -}}
{{- else -}}
{{- $mergedEnvs = append $mergedEnvs . -}}
{{- end -}}
{{- end -}}

{{- /* Add enhanced _JAVA_OPTIONS calculation */ -}}
{{- $enhancedJavaOpts := include "calculate-enhanced-java-options" (dict "deployment" .Values.deployments.crons "existingJavaOpts" $javaVal) -}}
{{- if ne $enhancedJavaOpts "" -}}
{{- $mergedEnvs = append $mergedEnvs (dict "name" "_JAVA_OPTIONS" "value" $enhancedJavaOpts) -}}
{{- else if eq $javaVal "" -}}
{{- $mergedEnvs = append $mergedEnvs (dict "name" "_JAVA_OPTIONS" "value" "") -}}
{{- end -}}

{{- if $mergedEnvs }}
{{ toYaml $mergedEnvs | nindent 12 }}
{{- end }}

# waiting for mysql, rabbitmq and keycloak initialization
Expand Down
180 changes: 151 additions & 29 deletions chart/templates/helpers/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -539,48 +539,170 @@ false
#####################
*/}}

{{- /* Convert a memory string like "1024Mi" or "2Gi" to Mi (int). Unknown/empty -> 0 */ -}}
{{- define "mem-to-mi" -}}
{{- $m := . | default "" -}}
{{- if $m | hasSuffix "Gi" -}}
{{- mul (trimSuffix "Gi" $m | int) 1024 -}}
{{- else if $m | hasSuffix "Mi" -}}
{{- trimSuffix "Mi" $m | int -}}
{{- else -}}
0
{{- end -}}
{{- end -}}

{{- define "get-env" -}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call it "get-env-var"

{{- $list := index . 0 -}}
{{- $name := index . 1 -}}
{{- $out := "" -}}
{{- range $list -}}
{{- if and (hasKey . "name") (eq .name $name) -}}
{{- if hasKey . "value" -}}
{{- $out = (printf "%v" .value) -}}
{{- else -}}
{{- $out = "" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $out -}}
{{- end -}}

{{- define "calculate-heap-size" -}}
{{- $deployment := . -}}
{{- $xmsRatio := 1 -}}
{{- $xmxRatio := 1 -}}
{{- $heap:= "" -}}
{{- $xms:= "" -}}
{{/* Check if called with parameters: deployment, xmsRatio, xmxRatio */}}
{{- if kindIs "slice" . -}}
{{- $deployment = index . 0 -}}
{{- if ge (len .) 2 -}}
{{- $xmsRatio = index . 1 | int -}}
{{- $deployment = index . 0 -}}
{{- if ge (len .) 2 -}}{{- $xmsRatio = index . 1 | int -}}{{- end -}}
{{- if ge (len .) 3 -}}{{- $xmxRatio = index . 2 | int -}}{{- end -}}
{{- end -}}
{{- if ne $xmsRatio 1 -}}{{- fail "calculate-heap-size: xmsRatio must be 1 (supported: 1:1..1:5)" -}}{{- end -}}
{{- if or (lt $xmxRatio 1) (gt $xmxRatio 5) -}}{{- fail "calculate-heap-size: xmxRatio must be 1..5 (supported: 1:1..1:5)" -}}{{- end -}}

{{- $memMi := include "mem-to-mi" ($deployment.resources.memory | default "") | int -}}
{{- if le $memMi 0 -}}
{{- "" -}}
{{- else -}}
{{- $xmx := div (mul $memMi 75) 100 -}} {{/* 75% of container memory */}}
{{- $xms := div $xmx $xmxRatio -}} {{/* Xms = Xmx / xmxRatio */}}
{{- printf "-Xmx%vm -Xms%vm" $xmx $xms -}}
{{- end -}}
{{- end -}}

{{- /* Parse existing _JAVA_OPTIONS to extract Xms and Xmx values */ -}}
{{- define "parse-java-options" -}}
{{- $javaOpts := . | default "" -}}
{{- $xms := "" -}}
{{- $xmx := "" -}}
{{- $otherOpts := "" -}}
{{- if ne $javaOpts "" -}}
{{- $parts := split " " $javaOpts -}}
{{- range $parts -}}
{{- $part := . | trim -}}
{{- if hasPrefix "-Xms" $part -}}
{{- $xms = $part -}}
{{- else if hasPrefix "-Xmx" $part -}}
{{- $xmx = $part -}}
{{- else if ne $part "" -}}
{{- if eq $otherOpts "" -}}
{{- $otherOpts = $part -}}
{{- else -}}
{{- $otherOpts = printf "%s %s" $otherOpts $part -}}
{{- end -}}
{{- end -}}
{{- if ge (len .) 3 -}}
{{- $xmxRatio = index . 2 | int -}}
{{- end -}}
{{- end -}}
{{- printf "%s|%s|%s" $xms $xmx $otherOpts -}}
{{- end -}}

{{- /* Calculate enhanced _JAVA_OPTIONS based on existing options and memory */ -}}
{{- define "calculate-enhanced-java-options" -}}
{{- $deployment := .deployment -}}
{{- $existingJavaOpts := .existingJavaOpts | default "" -}}
{{- $memMi := include "mem-to-mi" ($deployment.resources.memory | default "") | int -}}

{{- $hasXms := false -}}
{{- $hasXmx := false -}}
{{- $existingXms := "" -}}
{{- $existingXmx := "" -}}
{{- $otherOpts := "" -}}

{{- /* Parse existing options */ -}}
{{- if ne $existingJavaOpts "" -}}
{{- $parts := split " " $existingJavaOpts -}}
{{- range $parts -}}
{{- $part := . | trim -}}
{{- if or (hasPrefix "-Xms" $part) (hasPrefix "Xms" $part) -}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to check for Xms without -, it's not a valid option.

{{- $hasXms = true -}}
{{- if hasPrefix "-Xms" $part -}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after the change this should always be true

{{- $existingXms = $part -}}
{{- else -}}
{{- $existingXms = printf "-%s" $part -}}
{{- end -}}
{{- else if or (hasPrefix "-Xmx" $part) (hasPrefix "Xmx" $part) -}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to check for Xmx without -, it's not a valid option.

{{- $hasXmx = true -}}
{{- if hasPrefix "-Xmx" $part -}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

{{- $existingXmx = $part -}}
{{- else -}}
{{- $existingXmx = printf "-%s" $part -}}
{{- end -}}
{{- else if ne $part "" -}}
{{- if eq $otherOpts "" -}}
{{- $otherOpts = $part -}}
{{- else -}}
{{- $otherOpts = printf "%s %s" $otherOpts $part -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{/* Validate ratios - only allow sensible ratios like 1:1, 1:2, 1:3, 1:4, 1:5 */}}
{{- if ne $xmsRatio 1 -}}
{{- fail "calculate-heap-size: xmsRatio must be 1. Only ratios like 1:1, 1:2, 1:3, 1:4, 1:5 are supported." -}}

{{- $result := "" -}}
{{- $xms := "" -}}
{{- $xmx := "" -}}

{{- /* Calculate Xms and Xmx only if memory > 0 AND neither Xms nor Xmx is provided by user */ -}}
{{- if and (gt $memMi 0) (not $hasXms) (not $hasXmx) -}}
{{- $calculatedXmx := div (mul $memMi 75) 100 -}}
{{- $calculatedXms := $calculatedXmx -}}
{{- $xms = printf "-Xms%vm" $calculatedXms -}}
{{- $xmx = printf "-Xmx%vm" $calculatedXmx -}}
{{- else -}}
{{- /* Use existing values if provided, do not calculate missing ones */ -}}
{{- if $hasXms -}}{{- $xms = $existingXms -}}{{- end -}}
{{- if $hasXmx -}}{{- $xmx = $existingXmx -}}{{- end -}}
{{- end -}}

{{- /* Build the final result */ -}}
{{- if ne $xms "" -}}
{{- $result = $xms -}}
{{- end -}}
{{- if ne $xmx "" -}}
{{- if ne $result "" -}}
{{- $result = printf "%s %s" $result $xmx -}}
{{- else -}}
{{- $result = $xmx -}}
{{- end -}}
{{- end -}}
{{- if or (lt $xmxRatio 1) (gt $xmxRatio 5) -}}
{{- fail "calculate-heap-size: xmxRatio must be between 1 and 5. Only ratios like 1:1, 1:2, 1:3, 1:4, 1:5 are supported." -}}
{{- if ne $otherOpts "" -}}
{{- if ne $result "" -}}
{{- $result = printf "%s %s" $result $otherOpts -}}
{{- else -}}
{{- $result = $otherOpts -}}
{{- end -}}
{{- end -}}
{{- if contains "Gi" $deployment.resources.memory -}}
{{- $heap = div ($deployment.resources.memory | replace "Gi" "" | int | mul 1024 | mul 75 ) 100 -}}
{{- else if contains "Mi" $deployment.resources.memory -}}
{{- $heap = div ($deployment.resources.memory | replace "Mi" "" | int | mul 75) 100 -}}

{{- $result -}}
{{- end -}}
{{/* Calculate Xms = Xmx / xmxRatio */}}
{{- $xms = div $heap $xmxRatio -}}
{{- printf "-Xmx%vm -Xms%vm" $heap $xms -}}

{{- /* Unchanged: check if a list of {name,value} maps contains a given name */ -}}
{{- define "list-of-maps-contains" -}}
{{- $arg1 := index . 0 -}}
{{- $arg2 := index . 1 -}}
{{- range $arg1 -}}
{{- if eq .name $arg2 -}}true{{- end -}}
{{- end -}}
{{- end -}}

{{- define "list-of-maps-contains" }}
{{- $arg1 := index . 0 }}
{{- $arg2 := index . 1 }}
{{- range $arg1 }}
{{- if eq .name $arg2 }}
true
{{- end }}
{{- end }}
{{- end }}


{{/*
Expand Down
Loading
Loading