diff --git a/controllers/activemqartemis_reconciler.go b/controllers/activemqartemis_reconciler.go index 553a6e5b..c3baa37f 100644 --- a/controllers/activemqartemis_reconciler.go +++ b/controllers/activemqartemis_reconciler.go @@ -2424,16 +2424,11 @@ func (reconciler *ActiveMQArtemisReconcilerImpl) PodTemplateSpecForCR(customReso // env from CR can override pts.Spec.Containers[0].Env = append(pts.Spec.Containers[0].Env, customResource.Spec.Env...) - var reEvalJdkJavaOpts string = "" - currentEnv = environments.Retrieve(pts.Spec.Containers, jdkJavaOptionsEnvVarName) - if currentEnv != nil { - // support STATEFUL_SET_ORDINAL in JDK_JAVA_OPTIONS from CR - reEvalJdkJavaOpts = `export JDK_JAVA_OPTIONS=${JDK_JAVA_OPTIONS//\\$\\{STATEFUL_SET_ORDINAL\\}/${HOSTNAME##*-}};` - } + reEvalJdkOpts := generateReEvalOrdinaEnvReplacement(customResource.Spec.Env) pts.Spec.Containers[0].Command = []string{ "/bin/bash", "-c", - fmt.Sprintf("export STATEFUL_SET_ORDINAL=${HOSTNAME##*-}; %s exec java %s $JAVA_ARGS_APPEND org.apache.activemq.artemis.core.server.embedded.Main", reEvalJdkJavaOpts, commandLineString), + fmt.Sprintf("export STATEFUL_SET_ORDINAL=${HOSTNAME##*-}; %s exec java %s $JAVA_ARGS_APPEND org.apache.activemq.artemis.core.server.embedded.Main", reEvalJdkOpts, commandLineString), } } @@ -2442,6 +2437,21 @@ func (reconciler *ActiveMQArtemisReconcilerImpl) PodTemplateSpecForCR(customReso return pts, nil } +// support ${STATEFUL_SET_ORDINAL} replacement in JDK options from CR env if necessary +func generateReEvalOrdinaEnvReplacement(envVars []corev1.EnvVar) (cmd string) { + cmd = "" + for _, envVar := range envVars { + if supportsOrdinalReplacement(envVar) { + cmd += fmt.Sprintf("export %s=${%s//\\$\\{STATEFUL_SET_ORDINAL\\}/${HOSTNAME##*-}}; ", envVar.Name, envVar.Name) + } + } + return cmd +} + +func supportsOrdinalReplacement(envVar corev1.EnvVar) bool { + return (envVar.Name == jdkJavaOptionsEnvVarName || envVar.Name == javaArgsAppendEnvVarName) && strings.Contains(envVar.Value, "${STATEFUL_SET_ORDINAL}") +} + func getJaasConfigEnvVarName(customResource *brokerv1beta1.ActiveMQArtemis) string { if !isRestricted(customResource) { // legacy diff --git a/controllers/controll_plane_test.go b/controllers/controll_plane_test.go index 427e3b20..ea63034a 100644 --- a/controllers/controll_plane_test.go +++ b/controllers/controll_plane_test.go @@ -29,6 +29,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -141,6 +142,12 @@ var _ = Describe("minimal", func() { crd.Spec.Restricted = common.NewTrue() + // how the jdk command line can be configured or modified + crd.Spec.Env = []corev1.EnvVar{ + {Name: "JDK_JAVA_OPTIONS", Value: "-XshowSettings:system -Xlog:os+container=trace -DordinalPropForDoubleO=${STATEFUL_SET_ORDINAL}${STATEFUL_SET_ORDINAL}7"}, + {Name: "JAVA_ARGS_APPEND", Value: "-DordinalProp=${STATEFUL_SET_ORDINAL}"}, + } + By("Deploying the CRD " + crd.ObjectMeta.Name) Expect(k8sClient.Create(ctx, &crd)).Should(Succeed())