Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(trait): explicit jolokia agent dependency #5668

Merged
merged 1 commit into from
Jun 25, 2024
Merged
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
1 change: 1 addition & 0 deletions pkg/trait/jolokia.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (t *jolokiaTrait) Apply(e *Environment) error {
}
}
container.Args = append(container.Args, "-javaagent:"+jolokiaFilepath+"="+strings.Join(optionValues, ","))
container.Args = append(container.Args, "-cp", jolokiaFilepath)

containerPort := corev1.ContainerPort{
Name: "jolokia",
Expand Down
3 changes: 3 additions & 0 deletions pkg/trait/jolokia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestApplyJolokiaTraitNominalShouldSucceed(t *testing.T) {

assert.Equal(t, container.Args, []string{
"-javaagent:dependencies/lib/main/org.jolokia.jolokia-agent-jvm-1.7.1.jar=discoveryEnabled=false,host=*,port=8778",
"-cp", "dependencies/lib/main/org.jolokia.jolokia-agent-jvm-1.7.1.jar",
})

assert.Len(t, container.Ports, 1)
Expand Down Expand Up @@ -85,6 +86,7 @@ func TestApplyJolokiaTraitForOpenShiftProfileShouldSucceed(t *testing.T) {
"clientPrincipal.1=cn=system:master-proxy,clientPrincipal.2=cn=hawtio-online.hawtio.svc," +
"clientPrincipal.3=cn=fuse-console.fuse.svc,discoveryEnabled=false,extendedClientCheck=true," +
"host=*,port=8778,protocol=https,useSslClientAuthentication=true",
"-cp", "dependencies/lib/main/org.jolokia.jolokia-agent-jvm-1.7.1.jar",
})

assert.Len(t, container.Ports, 1)
Expand Down Expand Up @@ -134,6 +136,7 @@ func TestApplyJolokiaTraitWithOptionShouldOverrideDefault(t *testing.T) {
"-javaagent:dependencies/lib/main/org.jolokia.jolokia-agent-jvm-1.7.1.jar=caCert=.cacert,clientPrincipal=cn:any," +
"discoveryEnabled=true,extendedClientCheck=false,host=explicit-host,port=8778,protocol=http," +
"useSslClientAuthentication=false",
"-cp", "dependencies/lib/main/org.jolokia.jolokia-agent-jvm-1.7.1.jar",
})
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/trait/jvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func (t *jvmTrait) enableDebug(e *Environment) string {
}

func (t *jvmTrait) prepareClasspathItems(container *corev1.Container) []string {
existingClasspaths := extractExistingClasspathItems(container)
classpath := sets.NewSet()
// Deprecated: replaced by /etc/camel/resources.d/[_configmaps/_secrets] (camel.ResourcesConfigmapsMountPath/camel.ResourcesSecretsMountPath).
classpath.Add("./resources")
Expand All @@ -223,9 +224,28 @@ func (t *jvmTrait) prepareClasspathItems(container *corev1.Container) []string {
// Keep class path sorted so that it's consistent over reconciliation cycles
sort.Strings(items)

if existingClasspaths != nil {
existingClasspaths = append(existingClasspaths, items...)
return existingClasspaths
}

return items
}

// extractExistingClasspathItems returns any container classpath option (if exists).
func extractExistingClasspathItems(container *corev1.Container) []string {
for i, arg := range container.Args {
if arg == "-cp" || arg == "-classpath" {
if i < len(container.Args) {
// return the next argument
return strings.Split(container.Args[i+1], ":")
}
}
}

return nil
}

// Translate HTTP proxy environment variables, that are set by the environment trait,
// into corresponding JVM system properties.
func (t *jvmTrait) prepareHTTPProxy(container *corev1.Container) ([]string, error) {
Expand Down
43 changes: 43 additions & 0 deletions pkg/trait/jvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,49 @@ func TestApplyJvmTraitWithClasspath(t *testing.T) {
"io.quarkus.bootstrap.runner.QuarkusEntryPoint",
}, d.Spec.Template.Spec.Containers[0].Args)
}

func TestApplyJvmTraitWithClasspathAndExistingContainerCPArg(t *testing.T) {
trait, environment := createNominalJvmTest(v1.IntegrationKitTypePlatform)
trait.Classpath = "/path/to/my-dep.jar:/path/to/another/dep.jar"
d := appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: defaultContainerName,
Args: []string{
"-cp",
"my-precious-lib.jar",
},
},
},
},
},
},
}

environment.Resources.Add(&d)
configure, condition, err := trait.Configure(environment)
require.NoError(t, err)
assert.True(t, configure)
assert.Nil(t, condition)
err = trait.Apply(environment)

require.NoError(t, err)
assert.Equal(t, []string{
// WARN: we don't care if there are multiple classpath arguments
// as the application will use the second one
"-cp",
"my-precious-lib.jar",
"-cp",
fmt.Sprintf("my-precious-lib.jar:./resources:%s:%s:%s:%s:%s:dependencies/*",
rdMountPath, cmrMountPath, scrMountPath,
"/path/to/another/dep.jar", "/path/to/my-dep.jar"),
"io.quarkus.bootstrap.runner.QuarkusEntryPoint",
}, d.Spec.Template.Spec.Containers[0].Args)
}

func TestApplyJvmTraitKitMissing(t *testing.T) {
trait, environment := createNominalJvmTest(v1.IntegrationKitTypePlatform)
environment.IntegrationKit = nil
Expand Down