From ee78d10606c9ea4aed159a5dd186acd2cad9afde Mon Sep 17 00:00:00 2001 From: David Kornel Date: Mon, 1 Jul 2024 15:27:43 +0200 Subject: [PATCH] Ability to specify image of scraper pod Signed-off-by: David Kornel --- .../skodjob/testframe/MetricsCollector.java | 45 +++++++++++++++++-- .../test/integration/MetricsCollectorIT.java | 20 +++++++-- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/MetricsCollector.java b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/MetricsCollector.java index be425dd..4e673ad 100644 --- a/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/MetricsCollector.java +++ b/test-frame-metrics-collector/src/main/java/io/skodjob/testframe/MetricsCollector.java @@ -57,6 +57,7 @@ public class MetricsCollector { protected String namespaceName; protected String scraperPodName; + protected String scraperPodImage; protected boolean deployScraperPod; protected MetricsComponent component; protected Map collectedData; @@ -72,6 +73,7 @@ public class MetricsCollector { public static class Builder { private String namespaceName; private String scraperPodName; + private String scraperPodImage = "quay.io/curl/curl:latest"; private boolean deployScraperPod; private MetricsComponent component; private Map collectedData; @@ -110,6 +112,17 @@ public Builder withScraperPodName(String scraperPodName) { return this; } + /** + * Sets image of scraper pod which is deployed by MetricsCollector + * + * @param image image full path + * @return this builder instance to allow for method chaining + */ + public Builder withScraperPodImage(String image) { + this.scraperPodImage = image; + return this; + } + /** * Deploy own scraper pod instead of using already created with name * @@ -234,6 +247,15 @@ public String getScraperPodName() { return scraperPodName; } + /** + * Retrieves the image of the scraper pod. + * + * @return the image of the scraper pod + */ + public String getScraperPodImage() { + return scraperPodImage; + } + /** * Retrieves the metrics component associated with this object. * @@ -243,6 +265,15 @@ public MetricsComponent getComponent() { return component; } + /** + * Retrieves the switch if deploy own scraper pod is enabled + * + * @return boolean indicates deploy own pod + */ + public boolean getDeployScraperPod() { + return deployScraperPod; + } + /** * Retrieves the collected data as a map. * @@ -266,12 +297,19 @@ protected MetricsCollector.Builder newBuilder() { } protected MetricsCollector.Builder updateBuilder(MetricsCollector.Builder builder) { - return builder + MetricsCollector.Builder b = builder .withNamespaceName(getNamespaceName()) .withComponent(getComponent()) + .withScraperPodImage(getScraperPodImage()) .withScraperPodName(getScraperPodName()) .withCollectedData(getCollectedData()) .withExec(getExec()); + + if (getDeployScraperPod()) { + b.withDeployScraperPod(); + } + + return b; } /** @@ -307,6 +345,7 @@ protected MetricsCollector(Builder builder) { namespaceName = builder.namespaceName; scraperPodName = builder.scraperPodName; + scraperPodImage = builder.scraperPodImage; deployScraperPod = builder.deployScraperPod; component = builder.component; collectedData = builder.collectedData; @@ -450,8 +489,8 @@ private void deployScraperPod() { .withNewSpec() .withRestartPolicy("Never") .addNewContainer() - .withName("curl-container") - .withImage("quay.io/curl/curl") + .withName("scraper-container") + .withImage(this.scraperPodImage) .withCommand("/bin/sh") .withArgs("-c", "while true; do sleep 3600; done") .endContainer() diff --git a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/MetricsCollectorIT.java b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/MetricsCollectorIT.java index 017b7ec..8cfba9a 100644 --- a/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/MetricsCollectorIT.java +++ b/test-frame-test-examples/src/test/java/io/skodjob/testframe/test/integration/MetricsCollectorIT.java @@ -70,7 +70,7 @@ public LabelSelector getLabelSelector() { } @Test - void testCollectMetricsWitAutoDeployedPod() throws IOException { + void testCollectMetricsWithAutoDeployedPod() throws IOException { //Create deployment List resources = KubeResourceManager.getKubeClient() .readResourcesFromFile(getClass().getClassLoader().getResourceAsStream("metrics-example.yaml")) @@ -84,7 +84,7 @@ void testCollectMetricsWitAutoDeployedPod() throws IOException { .inNamespace("metrics-test").withName("prometheus-example").get()); // Create metrics collector - MetricsCollector collector = new MetricsCollector.Builder() + MetricsCollector.Builder mcBuilder = new MetricsCollector.Builder() .withNamespaceName("metrics-test") .withDeployScraperPod() .withScraperPodName("test-scraper-pod") @@ -102,13 +102,25 @@ public LabelSelector getLabelSelector() { .withMatchLabels(Map.of("app", "prometheus-example-app")) .build(); } - }) - .build(); + }); + + MetricsCollector collector = mcBuilder.build(); + // Collect metrics assertDoesNotThrow(() -> collector.collectMetricsFromPods(30000)); // timeout in milliseconds Map metrics = collector.getCollectedData(); assertTrue(metrics.containsKey(KubeResourceManager.getKubeClient() .listPodsByPrefixInName("metrics-test", "prometheus-example").get(0) .getMetadata().getName())); + + // Update metrics collector with different image + MetricsCollector collector2 = mcBuilder.withScraperPodImage("quay.io/prometheus/busybox:latest").build(); + + // Collect metrics + assertDoesNotThrow(() -> collector2.collectMetricsFromPods(30000)); // timeout in milliseconds + Map metrics2 = collector.getCollectedData(); + assertTrue(metrics2.containsKey(KubeResourceManager.getKubeClient() + .listPodsByPrefixInName("metrics-test", "prometheus-example").get(0) + .getMetadata().getName())); } }