Skip to content

Commit 71a8d88

Browse files
committed
java_options allows to set custom parameters
1 parent 0deeacb commit 71a8d88

File tree

4 files changed

+270
-157
lines changed

4 files changed

+270
-157
lines changed

chart/templates/backend-deployment.yaml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,32 @@ spec:
144144
{{- include "lightrun-backend-crons.environmentVariables" . | nindent 12 }}
145145
- name: SPRING_PROFILES_ACTIVE
146146
value: "prod,swagger,cluster"
147-
{{- if .Values.deployments.backend.extraEnvs }}
148-
{{- include "filter-out-java-options" .Values.deployments.backend.extraEnvs | nindent 12 }}
149-
{{- end }}
150-
- name: "_JAVA_OPTIONS"
151-
value: {{- toYaml (include "compose-java-options" (list .Values.deployments.backend (include "get-java-options-from-envs" .Values.deployments.backend.extraEnvs))) | nindent 21 }}
147+
{{- /* >>> BEGIN updated env-emitting block <<< */}}
148+
{{- $envs := .Values.deployments.backend.extraEnvs | default (list) -}}
149+
{{- $memMi := include "mem-to-mi" (.Values.deployments.backend.resources.memory | default "") | int -}}
150+
{{- $javaVal := (include "get-env" (list $envs "_JAVA_OPTIONS") | trim) -}}
151+
152+
{{- /* start with user extraEnvs, but drop _JAVA_OPTIONS */ -}}
153+
{{- $rendered := list -}}
154+
{{- range $e := $envs -}}
155+
{{- $name := $e.name | default "" -}}
156+
{{- if ne $name "_JAVA_OPTIONS" -}}
157+
{{- $rendered = append $rendered $e -}}
158+
{{- end -}}
159+
{{- end -}}
160+
161+
{{- /* add enhanced _JAVA_OPTIONS calculation */ -}}
162+
{{- $enhancedJavaOpts := include "calculate-enhanced-java-options" (dict "deployment" .Values.deployments.backend "existingJavaOpts" $javaVal) -}}
163+
{{- if ne $enhancedJavaOpts "" -}}
164+
{{- $rendered = append $rendered (dict "name" "_JAVA_OPTIONS" "value" $enhancedJavaOpts) -}}
165+
{{- else if eq $javaVal "" -}}
166+
{{- $rendered = append $rendered (dict "name" "_JAVA_OPTIONS" "value" "") -}}
167+
{{- end -}}
168+
169+
{{- if $rendered }}
170+
{{ toYaml $rendered | nindent 12 }}
171+
{{- end }}
172+
{{- /* >>> END updated env-emitting block <<< */}}
152173

153174
# waiting for mysql, rabbitmq and keycloak initialization
154175
initContainers:

chart/templates/crons/deployment.yaml

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,52 @@ spec:
136136
{{- include "lightrun-backend-crons.environmentVariables" . | nindent 12 }}
137137
- name: SPRING_PROFILES_ACTIVE
138138
value: "prod,swagger,cluster,cron"
139-
{{- $mergedExtraEnvs := include "lightrun-crons.mergedExtraEnvs" . }}
140-
{{- if $mergedExtraEnvs }}
141-
{{ $mergedExtraEnvs | nindent 12 }}
139+
{{- /* Enhanced _JAVA_OPTIONS logic for crons */ -}}
140+
{{- $backendExtraEnvs := .Values.deployments.backend.extraEnvs | default list -}}
141+
{{- $cronsExtraEnvs := .Values.deployments.crons.extraEnvs | default list -}}
142+
{{- $mergedEnvs := list -}}
143+
{{- $javaVal := "" -}}
144+
145+
{{- /* First, add all backend extraEnvs except _JAVA_OPTIONS */ -}}
146+
{{- range $backendExtraEnvs -}}
147+
{{- $backendEnv := . -}}
148+
{{- $isOverridden := false -}}
149+
{{- if eq $backendEnv.name "_JAVA_OPTIONS" -}}
150+
{{- $javaVal = (printf "%v" $backendEnv.value) -}}
151+
{{- else -}}
152+
{{- /* Check if this env var is overridden in crons */ -}}
153+
{{- range $cronsExtraEnvs -}}
154+
{{- if eq .name $backendEnv.name -}}
155+
{{- $isOverridden = true -}}
156+
{{- end -}}
157+
{{- end -}}
158+
{{- /* Only add backend env if not overridden by crons */ -}}
159+
{{- if not $isOverridden -}}
160+
{{- $mergedEnvs = append $mergedEnvs $backendEnv -}}
161+
{{- end -}}
162+
{{- end -}}
163+
{{- end -}}
164+
165+
{{- /* Then, add all crons extraEnvs except _JAVA_OPTIONS */ -}}
166+
{{- range $cronsExtraEnvs -}}
167+
{{- if eq .name "_JAVA_OPTIONS" -}}
168+
{{- $javaVal = (printf "%v" .value) -}}
169+
{{- else -}}
170+
{{- $mergedEnvs = append $mergedEnvs . -}}
171+
{{- end -}}
172+
{{- end -}}
173+
174+
{{- /* Add enhanced _JAVA_OPTIONS calculation */ -}}
175+
{{- $enhancedJavaOpts := include "calculate-enhanced-java-options" (dict "deployment" .Values.deployments.crons "existingJavaOpts" $javaVal) -}}
176+
{{- if ne $enhancedJavaOpts "" -}}
177+
{{- $mergedEnvs = append $mergedEnvs (dict "name" "_JAVA_OPTIONS" "value" $enhancedJavaOpts) -}}
178+
{{- else if eq $javaVal "" -}}
179+
{{- $mergedEnvs = append $mergedEnvs (dict "name" "_JAVA_OPTIONS" "value" "") -}}
180+
{{- end -}}
181+
182+
{{- if $mergedEnvs }}
183+
{{ toYaml $mergedEnvs | nindent 12 }}
142184
{{- end }}
143-
- name: "_JAVA_OPTIONS"
144-
value: {{- toYaml (include "compose-java-options" (list .Values.deployments.crons (include "lightrun-crons.getMergedJavaOptions" .) 1 3)) | nindent 21 }}
145185

146186
# waiting for mysql, rabbitmq and keycloak initialization
147187
initContainers:

chart/templates/helpers/_helpers.tpl

Lines changed: 147 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -539,105 +539,170 @@ false
539539
#####################
540540
*/}}
541541

542-
{{- define "calculate-heap-size" -}}
543-
{{- $deployment := . -}}
544-
{{- $xmsRatio := 1 -}}
545-
{{- $xmxRatio := 1 -}}
546-
{{- $heap:= "" -}}
547-
{{- $xms:= "" -}}
548-
{{/* Check if called with parameters: deployment, xmsRatio, xmxRatio */}}
549-
{{- if kindIs "slice" . -}}
550-
{{- $deployment = index . 0 -}}
551-
{{- if ge (len .) 2 -}}
552-
{{- $xmsRatio = index . 1 | int -}}
553-
{{- end -}}
554-
{{- if ge (len .) 3 -}}
555-
{{- $xmxRatio = index . 2 | int -}}
556-
{{- end -}}
557-
{{- end -}}
558-
{{/* Validate ratios - only allow sensible ratios like 1:1, 1:2, 1:3, 1:4, 1:5 */}}
559-
{{- if ne $xmsRatio 1 -}}
560-
{{- fail "calculate-heap-size: xmsRatio must be 1. Only ratios like 1:1, 1:2, 1:3, 1:4, 1:5 are supported." -}}
542+
{{- /* Convert a memory string like "1024Mi" or "2Gi" to Mi (int). Unknown/empty -> 0 */ -}}
543+
{{- define "mem-to-mi" -}}
544+
{{- $m := . | default "" -}}
545+
{{- if $m | hasSuffix "Gi" -}}
546+
{{- mul (trimSuffix "Gi" $m | int) 1024 -}}
547+
{{- else if $m | hasSuffix "Mi" -}}
548+
{{- trimSuffix "Mi" $m | int -}}
549+
{{- else -}}
550+
0
561551
{{- end -}}
562-
{{- if or (lt $xmxRatio 1) (gt $xmxRatio 5) -}}
563-
{{- 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." -}}
564552
{{- end -}}
565-
{{- if contains "Gi" $deployment.resources.memory -}}
566-
{{- $heap = div ($deployment.resources.memory | replace "Gi" "" | int | mul 1024 | mul 75 ) 100 -}}
567-
{{- else if contains "Mi" $deployment.resources.memory -}}
568-
{{- $heap = div ($deployment.resources.memory | replace "Mi" "" | int | mul 75) 100 -}}
553+
554+
{{- define "get-env" -}}
555+
{{- $list := index . 0 -}}
556+
{{- $name := index . 1 -}}
557+
{{- $out := "" -}}
558+
{{- range $list -}}
559+
{{- if and (hasKey . "name") (eq .name $name) -}}
560+
{{- if hasKey . "value" -}}
561+
{{- $out = (printf "%v" .value) -}}
562+
{{- else -}}
563+
{{- $out = "" -}}
564+
{{- end -}}
565+
{{- end -}}
569566
{{- end -}}
570-
{{/* Calculate Xms = Xmx / xmxRatio */}}
571-
{{- $xms = div $heap $xmxRatio -}}
572-
{{- printf "-Xmx%vm -Xms%vm" $heap $xms -}}
567+
{{- $out -}}
573568
{{- end -}}
574569

575-
{{/*
576-
Build final JAVA options string according to the rules:
577-
- If user provided -Xms and/or -Xmx, use exactly what they provided and do not add the missing pair
578-
- If neither -Xms nor -Xmx were provided, calculate both and append any additional user options
579-
Usage:
580-
{{ include "compose-java-options" (list $deployment $userOptions) }}
581-
{{ include "compose-java-options" (list $deployment $userOptions 1 3) }} # with ratios
582-
*/}}
583-
{{- define "compose-java-options" -}}
584-
{{- $deployment := index . 0 -}}
585-
{{- $userOptions := index . 1 | default "" -}}
570+
{{- define "calculate-heap-size" -}}
571+
{{- $deployment := . -}}
586572
{{- $xmsRatio := 1 -}}
587573
{{- $xmxRatio := 1 -}}
588-
{{- if ge (len .) 3 -}}
589-
{{- $xmsRatio = index . 2 | int -}}
590-
{{- end -}}
591-
{{- if ge (len .) 4 -}}
592-
{{- $xmxRatio = index . 3 | int -}}
574+
{{- if kindIs "slice" . -}}
575+
{{- $deployment = index . 0 -}}
576+
{{- if ge (len .) 2 -}}{{- $xmsRatio = index . 1 | int -}}{{- end -}}
577+
{{- if ge (len .) 3 -}}{{- $xmxRatio = index . 2 | int -}}{{- end -}}
593578
{{- end -}}
594-
{{- $hasXms := and $userOptions (contains "-Xms" $userOptions) -}}
595-
{{- $hasXmx := and $userOptions (contains "-Xmx" $userOptions) -}}
596-
{{- if or $hasXms $hasXmx -}}
597-
{{- $userOptions -}}
579+
{{- if ne $xmsRatio 1 -}}{{- fail "calculate-heap-size: xmsRatio must be 1 (supported: 1:1..1:5)" -}}{{- end -}}
580+
{{- if or (lt $xmxRatio 1) (gt $xmxRatio 5) -}}{{- fail "calculate-heap-size: xmxRatio must be 1..5 (supported: 1:1..1:5)" -}}{{- end -}}
581+
582+
{{- $memMi := include "mem-to-mi" ($deployment.resources.memory | default "") | int -}}
583+
{{- if le $memMi 0 -}}
584+
{{- "" -}}
598585
{{- else -}}
599-
{{- $calculated := include "calculate-heap-size" (list $deployment $xmsRatio $xmxRatio) -}}
600-
{{- printf "%s %s" $calculated $userOptions | trim -}}
601-
{{- end -}}
586+
{{- $xmx := div (mul $memMi 75) 100 -}} {{/* 75% of container memory */}}
587+
{{- $xms := div $xmx $xmxRatio -}} {{/* Xms = Xmx / xmxRatio */}}
588+
{{- printf "-Xmx%vm -Xms%vm" $xmx $xms -}}
589+
{{- end -}}
590+
{{- end -}}
591+
592+
{{- /* Parse existing _JAVA_OPTIONS to extract Xms and Xmx values */ -}}
593+
{{- define "parse-java-options" -}}
594+
{{- $javaOpts := . | default "" -}}
595+
{{- $xms := "" -}}
596+
{{- $xmx := "" -}}
597+
{{- $otherOpts := "" -}}
598+
{{- if ne $javaOpts "" -}}
599+
{{- $parts := split " " $javaOpts -}}
600+
{{- range $parts -}}
601+
{{- $part := . | trim -}}
602+
{{- if hasPrefix "-Xms" $part -}}
603+
{{- $xms = $part -}}
604+
{{- else if hasPrefix "-Xmx" $part -}}
605+
{{- $xmx = $part -}}
606+
{{- else if ne $part "" -}}
607+
{{- if eq $otherOpts "" -}}
608+
{{- $otherOpts = $part -}}
609+
{{- else -}}
610+
{{- $otherOpts = printf "%s %s" $otherOpts $part -}}
611+
{{- end -}}
612+
{{- end -}}
613+
{{- end -}}
602614
{{- end -}}
603-
604-
{{/* Extract JAVA options value from a list of env maps */}}
605-
{{- define "get-java-options-from-envs" -}}
606-
{{- $envs := . | default list -}}
607-
{{- $opts := "" -}}
608-
{{- range $envs -}}
609-
{{- if or (eq .name "_JAVA_OPTIONS") (eq .name "JAVA_OPTIONS") -}}
610-
{{- if .value -}}
611-
{{- $opts = .value -}}
615+
{{- printf "%s|%s|%s" $xms $xmx $otherOpts -}}
616+
{{- end -}}
617+
618+
{{- /* Calculate enhanced _JAVA_OPTIONS based on existing options and memory */ -}}
619+
{{- define "calculate-enhanced-java-options" -}}
620+
{{- $deployment := .deployment -}}
621+
{{- $existingJavaOpts := .existingJavaOpts | default "" -}}
622+
{{- $memMi := include "mem-to-mi" ($deployment.resources.memory | default "") | int -}}
623+
624+
{{- $hasXms := false -}}
625+
{{- $hasXmx := false -}}
626+
{{- $existingXms := "" -}}
627+
{{- $existingXmx := "" -}}
628+
{{- $otherOpts := "" -}}
629+
630+
{{- /* Parse existing options */ -}}
631+
{{- if ne $existingJavaOpts "" -}}
632+
{{- $parts := split " " $existingJavaOpts -}}
633+
{{- range $parts -}}
634+
{{- $part := . | trim -}}
635+
{{- if or (hasPrefix "-Xms" $part) (hasPrefix "Xms" $part) -}}
636+
{{- $hasXms = true -}}
637+
{{- if hasPrefix "-Xms" $part -}}
638+
{{- $existingXms = $part -}}
639+
{{- else -}}
640+
{{- $existingXms = printf "-%s" $part -}}
641+
{{- end -}}
642+
{{- else if or (hasPrefix "-Xmx" $part) (hasPrefix "Xmx" $part) -}}
643+
{{- $hasXmx = true -}}
644+
{{- if hasPrefix "-Xmx" $part -}}
645+
{{- $existingXmx = $part -}}
646+
{{- else -}}
647+
{{- $existingXmx = printf "-%s" $part -}}
648+
{{- end -}}
649+
{{- else if ne $part "" -}}
650+
{{- if eq $otherOpts "" -}}
651+
{{- $otherOpts = $part -}}
652+
{{- else -}}
653+
{{- $otherOpts = printf "%s %s" $otherOpts $part -}}
654+
{{- end -}}
612655
{{- end -}}
613656
{{- end -}}
614657
{{- end -}}
615-
{{- $opts -}}
658+
659+
{{- $result := "" -}}
660+
{{- $xms := "" -}}
661+
{{- $xmx := "" -}}
662+
663+
{{- /* Calculate Xms and Xmx only if memory > 0 AND neither Xms nor Xmx is provided by user */ -}}
664+
{{- if and (gt $memMi 0) (not $hasXms) (not $hasXmx) -}}
665+
{{- $calculatedXmx := div (mul $memMi 75) 100 -}}
666+
{{- $calculatedXms := $calculatedXmx -}}
667+
{{- $xms = printf "-Xms%vm" $calculatedXms -}}
668+
{{- $xmx = printf "-Xmx%vm" $calculatedXmx -}}
669+
{{- else -}}
670+
{{- /* Use existing values if provided, do not calculate missing ones */ -}}
671+
{{- if $hasXms -}}{{- $xms = $existingXms -}}{{- end -}}
672+
{{- if $hasXmx -}}{{- $xmx = $existingXmx -}}{{- end -}}
616673
{{- end -}}
617674

618-
{{/* Return env list without any JAVA options entries */}}
619-
{{- define "filter-out-java-options" -}}
620-
{{- $envs := . | default list -}}
621-
{{- $out := list -}}
622-
{{- range $envs -}}
623-
{{- if and (ne .name "_JAVA_OPTIONS") (ne .name "JAVA_OPTIONS") -}}
624-
{{- $out = append $out . -}}
675+
{{- /* Build the final result */ -}}
676+
{{- if ne $xms "" -}}
677+
{{- $result = $xms -}}
678+
{{- end -}}
679+
{{- if ne $xmx "" -}}
680+
{{- if ne $result "" -}}
681+
{{- $result = printf "%s %s" $result $xmx -}}
682+
{{- else -}}
683+
{{- $result = $xmx -}}
625684
{{- end -}}
626685
{{- end -}}
627-
{{- if $out -}}
628-
{{- toYaml $out -}}
686+
{{- if ne $otherOpts "" -}}
687+
{{- if ne $result "" -}}
688+
{{- $result = printf "%s %s" $result $otherOpts -}}
689+
{{- else -}}
690+
{{- $result = $otherOpts -}}
691+
{{- end -}}
629692
{{- end -}}
693+
694+
{{- $result -}}
695+
{{- end -}}
696+
697+
{{- /* Unchanged: check if a list of {name,value} maps contains a given name */ -}}
698+
{{- define "list-of-maps-contains" -}}
699+
{{- $arg1 := index . 0 -}}
700+
{{- $arg2 := index . 1 -}}
701+
{{- range $arg1 -}}
702+
{{- if eq .name $arg2 -}}true{{- end -}}
703+
{{- end -}}
630704
{{- end -}}
631705

632-
{{- define "list-of-maps-contains" }}
633-
{{- $arg1 := index . 0 }}
634-
{{- $arg2 := index . 1 }}
635-
{{- range $arg1 }}
636-
{{- if eq .name $arg2 }}
637-
true
638-
{{- end }}
639-
{{- end }}
640-
{{- end }}
641706

642707

643708
{{/*
@@ -909,8 +974,7 @@ Cron-specific asyncProfiler helpers
909974
{{- end -}}
910975

911976
{{/*
912-
Merge extraEnvs from backend and crons with crons taking precedence for duplicate keys.
913-
JAVA options are intentionally filtered out and should be added explicitly by templates.
977+
Merge extraEnvs from backend and crons with crons taking precedence for duplicate keys
914978
*/}}
915979
{{- define "lightrun-crons.mergedExtraEnvs" -}}
916980
{{- $backendExtraEnvs := .Values.deployments.backend.extraEnvs | default list -}}
@@ -929,17 +993,13 @@ JAVA options are intentionally filtered out and should be added explicitly by te
929993
{{- end -}}
930994
{{/* Only add backend env if not overridden by crons */}}
931995
{{- if not $isOverridden -}}
932-
{{- if and (ne $backendEnv.name "_JAVA_OPTIONS") (ne $backendEnv.name "JAVA_OPTIONS") -}}
933-
{{- $mergedEnvs = append $mergedEnvs $backendEnv -}}
934-
{{- end -}}
996+
{{- $mergedEnvs = append $mergedEnvs $backendEnv -}}
935997
{{- end -}}
936998
{{- end -}}
937999

9381000
{{/* Then, add all crons extraEnvs (these take precedence) */}}
9391001
{{- range $cronsExtraEnvs -}}
940-
{{- if and (ne .name "_JAVA_OPTIONS") (ne .name "JAVA_OPTIONS") -}}
941-
{{- $mergedEnvs = append $mergedEnvs . -}}
942-
{{- end -}}
1002+
{{- $mergedEnvs = append $mergedEnvs . -}}
9431003
{{- end -}}
9441004

9451005
{{/* Output merged envs as YAML if any exist */}}
@@ -948,20 +1008,6 @@ JAVA options are intentionally filtered out and should be added explicitly by te
9481008
{{- end -}}
9491009
{{- end -}}
9501010

951-
952-
{{/* Get merged JAVA options from backend+crons with crons taking precedence */}}
953-
{{- define "lightrun-crons.getMergedJavaOptions" -}}
954-
{{- $backendExtraEnvs := .Values.deployments.backend.extraEnvs | default list -}}
955-
{{- $cronsExtraEnvs := .Values.deployments.crons.extraEnvs | default list -}}
956-
{{- $opts := include "get-java-options-from-envs" $backendExtraEnvs -}}
957-
{{- $cronsOpts := include "get-java-options-from-envs" $cronsExtraEnvs -}}
958-
{{- if $cronsOpts -}}
959-
{{- $cronsOpts -}}
960-
{{- else -}}
961-
{{- $opts -}}
962-
{{- end -}}
963-
{{- end -}}
964-
9651011
{{/*
9661012
################
9671013
### Datadog ###

0 commit comments

Comments
 (0)