@@ -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