Skip to content

Commit

Permalink
Ability to specify image of scraper pod
Browse files Browse the repository at this point in the history
Signed-off-by: David Kornel <[email protected]>
  • Loading branch information
kornys committed Jul 1, 2024
1 parent fb28955 commit ee78d10
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class MetricsCollector {

protected String namespaceName;
protected String scraperPodName;
protected String scraperPodImage;
protected boolean deployScraperPod;
protected MetricsComponent component;
protected Map<String, String> collectedData;
Expand All @@ -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<String, String> collectedData;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public LabelSelector getLabelSelector() {
}

@Test
void testCollectMetricsWitAutoDeployedPod() throws IOException {
void testCollectMetricsWithAutoDeployedPod() throws IOException {
//Create deployment
List<HasMetadata> resources = KubeResourceManager.getKubeClient()
.readResourcesFromFile(getClass().getClassLoader().getResourceAsStream("metrics-example.yaml"))
Expand All @@ -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")
Expand All @@ -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<String, String> 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<String, String> metrics2 = collector.getCollectedData();
assertTrue(metrics2.containsKey(KubeResourceManager.getKubeClient()
.listPodsByPrefixInName("metrics-test", "prometheus-example").get(0)
.getMetadata().getName()));
}
}

0 comments on commit ee78d10

Please sign in to comment.