Repository: apache/camel-k
Pull request #1193 (2020-01-13 13:21:39)

Jolokia trait improvements
**Release Note**
```release-note
feat(jolokia): Support auto-configuration for OpenShift 4
feat(jolokia): Automatically adds the camel-management dependency
```

This enables using [Hawtio Online](https://github.com/hawtio/hawtio-online) OOTB on OpenShift 4 with the latest Fabric8 base image:

<img width="1277" alt="Screenshot 2020-01-13 at 14 28 31" src="https://user-images.githubusercontent.com/366207/72259791-9af03980-3611-11ea-92e7-7ee457402161.png">



Patch:
diff --git a/pkg/trait/jolokia.go b/pkg/trait/jolokia.go
index 9972b1a9b..60f602209 100644
--- a/pkg/trait/jolokia.go
+++ b/pkg/trait/jolokia.go
@@ -22,10 +22,11 @@ import (
 	"strconv"
 	"strings"
 
+	corev1 "k8s.io/api/core/v1"
+
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/apache/camel-k/pkg/util"
 	"github.com/apache/camel-k/pkg/util/envvar"
-
-	corev1 "k8s.io/api/core/v1"
 )
 
 // The Jolokia trait activates and configures the Jolokia Java agent.
@@ -78,6 +79,10 @@ func newJolokiaTrait() *jolokiaTrait {
 	}
 }
 
+func (t *jolokiaTrait) isEnabled() bool {
+	return t.Enabled != nil && *t.Enabled
+}
+
 func (t *jolokiaTrait) Configure(e *Environment) (bool, error) {
 	options, err := parseCsvMap(t.Options)
 	if err != nil {
@@ -90,16 +95,27 @@ func (t *jolokiaTrait) Configure(e *Environment) (bool, error) {
 	// Configure HTTPS by default for OpenShift
 	if e.DetermineProfile() == v1.TraitProfileOpenShift {
 		setDefaultJolokiaOption(options, &t.Protocol, "protocol", "https")
-		setDefaultJolokiaOption(options, &t.CaCert, "caCert", "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt")
+		setDefaultJolokiaOption(options, &t.CaCert, "caCert", "/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt")
 		setDefaultJolokiaOption(options, &t.ExtendedClientCheck, "extendedClientCheck", true)
-		setDefaultJolokiaOption(options, &t.ClientPrincipal, "clientPrincipal", "cn=system:master-proxy")
 		setDefaultJolokiaOption(options, &t.UseSslClientAuthentication, "useSslClientAuthentication", true)
 	}
 
-	return e.IntegrationInPhase(v1.IntegrationPhaseDeploying, v1.IntegrationPhaseRunning), nil
+	return e.IntegrationInPhase(
+		v1.IntegrationPhaseInitialization,
+		v1.IntegrationPhaseDeploying,
+		v1.IntegrationPhaseRunning,
+	), nil
 }
 
 func (t *jolokiaTrait) Apply(e *Environment) (err error) {
+	if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
+		if t.isEnabled() {
+			// Add Camel management dependency
+			util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, "mvn:org.apache.camel/camel-management")
+		}
+		return nil
+	}
+
 	containerName := defaultContainerName
 	dt := e.Catalog.GetTrait(containerTraitID)
 	if dt != nil {
@@ -117,7 +133,7 @@ func (t *jolokiaTrait) Apply(e *Environment) (err error) {
 		return nil
 	}
 
-	if t.Enabled == nil || !*t.Enabled {
+	if !t.isEnabled() {
 		// Deactivate the Jolokia Java agent
 		// Note: the AB_JOLOKIA_OFF environment variable acts as an option flag
 		envvar.SetVal(&container.Env, "AB_JOLOKIA_OFF", "true")
@@ -136,7 +152,16 @@ func (t *jolokiaTrait) Apply(e *Environment) (err error) {
 
 	// Then add explicitly set trait configuration properties
 	addToJolokiaOptions(options, "caCert", t.CaCert)
-	addToJolokiaOptions(options, "clientPrincipal", t.ClientPrincipal)
+	if options["clientPrincipal"] == "" && t.ClientPrincipal == nil && e.DetermineProfile() == v1.TraitProfileOpenShift {
+		// TODO: simplify when trait array options are supported
+		// Master API proxy for OpenShift 3
+		addToJolokiaOptions(options, "clientPrincipal.1", "cn=system:master-proxy")
+		// Default Hawtio and Fuse consoles for OpenShift 4
+		addToJolokiaOptions(options, "clientPrincipal.2", "cn=hawtio-online.hawtio.svc")
+		addToJolokiaOptions(options, "clientPrincipal.3", "cn=fuse-console.fuse.svc")
+	} else {
+		addToJolokiaOptions(options, "clientPrincipal", t.ClientPrincipal)
+	}
 	addToJolokiaOptions(options, "discoveryEnabled", t.DiscoveryEnabled)
 	addToJolokiaOptions(options, "extendedClientCheck", t.ExtendedClientCheck)
 	addToJolokiaOptions(options, "host", t.Host)
@@ -211,5 +236,9 @@ func addToJolokiaOptions(options map[string]string, key string, value interface{
 		if v != nil {
 			options[key] = *v
 		}
+	case string:
+		if v != "" {
+			options[key] = v
+		}
 	}
 }


Test patch:
diff --git a/pkg/trait/jolokia_test.go b/pkg/trait/jolokia_test.go
index 858da8739..bbaf4e464 100644
--- a/pkg/trait/jolokia_test.go
+++ b/pkg/trait/jolokia_test.go
@@ -77,9 +77,8 @@ func TestConfigureJolokiaTraitForOpenShiftProfileShouldSetDefaultHttpsJolokiaOpt
 	assert.Nil(t, err)
 	assert.True(t, configured)
 	assert.Equal(t, *trait.Protocol, "https")
-	assert.Equal(t, *trait.CaCert, "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt")
+	assert.Equal(t, *trait.CaCert, "/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt")
 	assert.Equal(t, *trait.ExtendedClientCheck, true)
-	assert.Equal(t, *trait.ClientPrincipal, "cn=system:master-proxy")
 	assert.Equal(t, *trait.UseSslClientAuthentication, true)
 }