Skip to content

Commit

Permalink
[#1050] restricted mode, check for and substitute ${STATEFUL_SET_ORDI…
Browse files Browse the repository at this point in the history
…NAL} in jdk related env vars
  • Loading branch information
gtully committed Nov 15, 2024
1 parent 2db880f commit 3a890d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
24 changes: 17 additions & 7 deletions controllers/activemqartemis_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions controllers/controll_plane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())

Expand Down

0 comments on commit 3a890d2

Please sign in to comment.