forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate prometheus ServiceMonitor CR
- Loading branch information
Showing
16 changed files
with
318 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...nt/src/main/java/io/quarkus/kubernetes/deployment/AddServiceMonitorResourceDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.quarkus.kubernetes.deployment; | ||
|
||
import io.dekorate.kubernetes.decorator.ResourceProvidingDecorator; | ||
import io.dekorate.prometheus.model.ServiceMonitorBuilder; | ||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
|
||
public class AddServiceMonitorResourceDecorator extends ResourceProvidingDecorator<KubernetesListBuilder> { | ||
|
||
private final String scheme; | ||
private final String targetPort; | ||
private final String path; | ||
private final int interval; | ||
private final boolean honorLabels; | ||
|
||
public AddServiceMonitorResourceDecorator(String scheme, String targetPort, String path, int interval, | ||
boolean honorLabels) { | ||
this.scheme = scheme; | ||
this.targetPort = targetPort; | ||
this.path = path; | ||
this.interval = interval; | ||
this.honorLabels = honorLabels; | ||
} | ||
|
||
@Override | ||
public void visit(KubernetesListBuilder list) { | ||
ObjectMeta meta = getMandatoryDeploymentMetadata(list, ANY); | ||
list.addToItems(new ServiceMonitorBuilder() | ||
.withNewMetadata() | ||
.withName(meta.getName()) | ||
.withLabels(meta.getLabels()) | ||
.endMetadata() | ||
.withNewSpec() | ||
.withNewSelector() | ||
.addToMatchLabels(meta.getLabels()) | ||
.endSelector() | ||
.addNewEndpoint() | ||
.withScheme(scheme) | ||
.withNewTargetPort(targetPort) | ||
.withPath(path) | ||
.withInterval(interval + "s") | ||
.withHonorLabels(honorLabels) | ||
.endEndpoint() | ||
.endSpec()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...ard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithMetricsNoServiceMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.quarkus.it.kubernetes; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.entry; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
import io.quarkus.builder.Version; | ||
import io.quarkus.maven.dependency.Dependency; | ||
import io.quarkus.test.LogFile; | ||
import io.quarkus.test.ProdBuildResults; | ||
import io.quarkus.test.ProdModeTestResults; | ||
import io.quarkus.test.QuarkusProdModeTest; | ||
|
||
public class KubernetesWithMetricsNoServiceMonitor { | ||
|
||
@RegisterExtension | ||
static final QuarkusProdModeTest config = new QuarkusProdModeTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class)) | ||
.setApplicationName("metrics") | ||
.setApplicationVersion("0.1-SNAPSHOT") | ||
.setRun(true) | ||
.setLogFileName("k8s.log") | ||
.overrideConfigKey("quarkus.http.port", "9090") | ||
.overrideConfigKey("quarkus.smallrye-metrics.path", "/met") | ||
.overrideConfigKey("quarkus.kubernetes.prometheus.generate-service-monitor", "false") | ||
.overrideConfigKey("quarkus.kubernetes.prometheus.prefix", "example.io") | ||
.setForcedDependencies(List.of( | ||
Dependency.of("io.quarkus", "quarkus-micrometer-registry-prometheus", Version.getVersion()))); | ||
|
||
@ProdBuildResults | ||
private ProdModeTestResults prodModeTestResults; | ||
|
||
@LogFile | ||
private Path logfile; | ||
|
||
@Test | ||
public void assertApplicationRuns() { | ||
assertThat(logfile).isRegularFile().hasFileName("k8s.log"); | ||
TestUtil.assertLogFileContents(logfile, "kubernetes", "metrics"); | ||
|
||
given() | ||
.when().get("/greeting") | ||
.then() | ||
.statusCode(200) | ||
.body(is("hello")); | ||
} | ||
|
||
@Test | ||
public void assertGeneratedResources() throws IOException { | ||
final Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes"); | ||
assertThat(kubernetesDir) | ||
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json")) | ||
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml")); | ||
List<HasMetadata> kubernetesList = DeserializationUtil | ||
.deserializeAsList(kubernetesDir.resolve("kubernetes.yml")); | ||
assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> { | ||
assertThat(d.getMetadata()).satisfies(m -> { | ||
assertThat(m.getName()).isEqualTo("metrics"); | ||
}); | ||
|
||
assertThat(d.getSpec()).satisfies(deploymentSpec -> { | ||
assertThat(deploymentSpec.getTemplate()).satisfies(t -> { | ||
assertThat(t.getMetadata()).satisfies(meta -> { | ||
// Annotations should not have been created in this configuration. | ||
assertThat(meta.getAnnotations()).contains( | ||
entry("prometheus.io/scrape", "true"), | ||
entry("prometheus.io/path", "/met"), | ||
entry("prometheus.io/port", "9090"), | ||
entry("prometheus.io/scheme", "http")); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
assertThat(kubernetesList).filteredOn(i -> i.getKind().equals("ServiceMonitor")).isEmpty(); | ||
} | ||
} |
Oops, something went wrong.