CAMEL-21087: Fix jolokia trait service exposure to Camel Jbang Kubernetes plugin (#18169)
diff --git a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/JolokiaTrait.java b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/JolokiaTrait.java
index aeaa97d..0026892 100644
--- a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/JolokiaTrait.java
+++ b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/main/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/JolokiaTrait.java
@@ -77,17 +77,19 @@
.endSpec()
.endTemplate()
.endSpec());
- context.doWithServices(
- s -> s.editSpec()
- .addNewPort()
- .withName(Optional.ofNullable(jolokiaTrait.getServicePortName()).orElse(DEFAULT_JOLOKIA_PORT_NAME))
- .withPort(Optional.ofNullable(jolokiaTrait.getServicePort()).map(Long::intValue)
- .orElse(DEFAULT_JOLOKIA_PORT))
- .withTargetPort(new IntOrString(
- Optional.ofNullable(jolokiaTrait.getContainerPortName()).orElse(DEFAULT_JOLOKIA_PORT_NAME)))
- .endPort()
- .endSpec());
+ if (Boolean.TRUE.equals(jolokiaTrait.getExpose())) {
+ context.doWithServices(
+ s -> s.editSpec()
+ .addNewPort()
+ .withName(Optional.ofNullable(jolokiaTrait.getServicePortName()).orElse(DEFAULT_JOLOKIA_PORT_NAME))
+ .withPort(Optional.ofNullable(jolokiaTrait.getServicePort()).map(Long::intValue)
+ .orElse(DEFAULT_JOLOKIA_PORT))
+ .withTargetPort(new IntOrString(
+ Optional.ofNullable(jolokiaTrait.getContainerPortName()).orElse(DEFAULT_JOLOKIA_PORT_NAME)))
+ .endPort()
+ .endSpec());
+ }
}
}
diff --git a/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/test/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/JolokiaTraitTest.java b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/test/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/JolokiaTraitTest.java
new file mode 100644
index 0000000..a3b72cd
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-plugin-kubernetes/src/test/java/org/apache/camel/dsl/jbang/core/commands/kubernetes/traits/JolokiaTraitTest.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands.kubernetes.traits;
+
+import org.apache.camel.dsl.jbang.core.commands.kubernetes.traits.model.Jolokia;
+import org.apache.camel.dsl.jbang.core.commands.kubernetes.traits.model.Traits;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class JolokiaTraitTest {
+
+ @Test
+ public void jolokiaExposeFalseTest() {
+ TraitContext context = mock(TraitContext.class);
+
+ Traits traitConfig = mock(Traits.class);
+ Jolokia jolokia = new Jolokia();
+ jolokia.setExpose(false);
+ when(traitConfig.getJolokia()).thenReturn(jolokia);
+
+ JolokiaTrait trait = new JolokiaTrait();
+ trait.apply(traitConfig, context);
+
+ verify(context, times(1)).doWithDeployments(any());
+ verify(context, times(1)).doWithKnativeServices(any());
+ verify(context, times(0)).doWithServices(any());
+
+ }
+
+ @Test
+ public void jolokiaExposeTrueTest() {
+ TraitContext context = mock(TraitContext.class);
+
+ Traits traitConfig = mock(Traits.class);
+ Jolokia jolokia = new Jolokia();
+ jolokia.setExpose(true);
+ when(traitConfig.getJolokia()).thenReturn(jolokia);
+
+ JolokiaTrait trait = new JolokiaTrait();
+ trait.apply(traitConfig, context);
+
+ verify(context, times(1)).doWithDeployments(any());
+ verify(context, times(1)).doWithKnativeServices(any());
+ verify(context, times(1)).doWithServices(any());
+
+ }
+
+}