Skip to content

Commit

Permalink
Add test for Kubernetes component with autowiring disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed Feb 17, 2025
1 parent 575c9d9 commit 14c680c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.fabric8.kubernetes.api.model.Pod;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
Expand All @@ -32,12 +33,15 @@
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.kubernetes.KubernetesConstants;
import org.apache.camel.component.kubernetes.KubernetesOperations;
import org.apache.camel.component.kubernetes.pods.KubernetesPodsComponent;
import org.eclipse.microprofile.config.ConfigProvider;

@Path("/kubernetes/pods")
@ApplicationScoped
Expand Down Expand Up @@ -70,17 +74,32 @@ public Pod getPod(
public Response createPod(
@PathParam("namespace") String namespace,
@PathParam("podName") String podName,
@QueryParam("isAutowiredClient") boolean isAutowiredClient,
Pod pod) throws Exception {

String directEndpointUri = "direct:start";
Map<String, Object> headers = new HashMap<>();
headers.put("componentName", "kubernetes-pods");
if (isAutowiredClient) {
headers.put("componentName", "kubernetes-pods");
} else {
directEndpointUri += "NoAutoWired";
String masterUrl = ConfigProvider.getConfig()
.getOptionalValue("kubernetes.master", String.class)
.orElseGet(() -> ConfigProvider.getConfig()
.getOptionalValue("quarkus.kubernetes-client.api-server-url", String.class)
.orElse(null));

headers.put("componentName", "kubernetes-pods-no-autowire");
headers.put("masterUrl", masterUrl);
}

headers.put(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, namespace);
headers.put(KubernetesConstants.KUBERNETES_POD_NAME, podName);
headers.put(KubernetesConstants.KUBERNETES_POD_SPEC, pod.getSpec());
headers.put(KubernetesConstants.KUBERNETES_PODS_LABELS, Map.of("app", podName));
headers.put(KubernetesConstants.KUBERNETES_OPERATION, KubernetesOperations.CREATE_POD_OPERATION);

Pod createdPod = producerTemplate.requestBodyAndHeaders("direct:start", null, headers, Pod.class);
Pod createdPod = producerTemplate.requestBodyAndHeaders(directEndpointUri, null, headers, Pod.class);
return Response
.created(new URI("https://camel.apache.org/"))
.entity(createdPod)
Expand Down Expand Up @@ -157,4 +176,11 @@ public Response getEvents() {
Pod pod = consumerTemplate.receiveBody("seda:podEvents", 10000, Pod.class);
return Response.ok().entity(pod).build();
}

@Named("kubernetes-pods-no-autowire")
KubernetesPodsComponent kubernetesPodsComponent() {
KubernetesPodsComponent component = new KubernetesPodsComponent();
component.setAutowiredEnabled(false);
return component;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public void configure() {
from("direct:start")
.toD("${header.componentName}:local");

from("direct:startNoAutoWired")
.toD("kubernetes-pods-no-autowire:${header.masterUrl}");

from("kubernetes-config-maps:local?resourceName=camel-configmap-watched")
.id("configmap-listener")
.autoStartup(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,60 @@ void podOperations() throws Exception {
}
}

@Test
void createPodWithKubernetesClientAutowiringDisabled() throws Exception {
try (CamelKubernetesNamespace namespace = new CamelKubernetesNamespace()) {
namespace.awaitCreation();

Container container = new Container();
container.setImage("busybox:latest");
container.setName("camel-pod-no-autowire");
container.setCommand(List.of("/bin/sh", "-c", "while true; do echo 'Hello, World!'; sleep 5; done"));

Map<String, String> labels = Map.of("app", "camel-pod-no-autowire");
Pod pod = new PodBuilder()
.withNewMetadata()
.withName("camel-pod-no-autowire")
.withNamespace(namespace.getNamespace())
.withLabels(labels)
.endMetadata()
.withNewSpec()
.withContainers(container)
.endSpec()
.build();

// Create with the non-autowired k8s client component
RestAssured.given()
.queryParam("isAutowiredClient", "false")
.contentType(ContentType.JSON)
.body(pod)
.when()
.post("/kubernetes/pods/" + namespace.getNamespace() + "/camel-pod-no-autowire")
.then()
.statusCode(201)
.body("metadata.name", is("camel-pod-no-autowire"),
"metadata.namespace", is(namespace.getNamespace()),
"metadata.resourceVersion", notNullValue());

// Read
RestAssured.given()
.when()
.get("/kubernetes/pods/" + namespace.getNamespace() + "/camel-pod-no-autowire")
.then()
.statusCode(200)
.body("metadata.name", is("camel-pod-no-autowire"),
"metadata.namespace", is(namespace.getNamespace()),
"metadata.resourceVersion", notNullValue());

// Delete
RestAssured.given()
.when()
.delete("/kubernetes/pods/" + namespace.getNamespace() + "/camel-pod-no-autowire")
.then()
.statusCode(204);
}
}

@Test
void podEvents() throws Exception {
try (CamelKubernetesNamespace namespace = new CamelKubernetesNamespace()) {
Expand Down

0 comments on commit 14c680c

Please sign in to comment.