Skip to content

Commit

Permalink
Merge pull request #18989 from geoand/#15598
Browse files Browse the repository at this point in the history
Only add prometheus annotation and specific k8s resources
  • Loading branch information
geoand authored Jul 28, 2021
2 parents df3e7a7 + 7f0bae7 commit fdddf2a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
public class KubernetesCommonHelper {

private static final String OUTPUT_ARTIFACT_FORMAT = "%s%s.jar";
private static final String[] PROMETHEUS_ANNOTATION_TARGETS = { "Service",
"Deployment", "DeploymentConfig" };

public static Optional<Project> createProject(ApplicationInfoBuildItem app,
Optional<CustomProjectRootBuildItem> customProjectRoot, OutputTargetBuildItem outputTarget,
Expand Down Expand Up @@ -440,13 +442,16 @@ private static List<DecoratorBuildItem> createAnnotationDecorators(Optional<Proj
String prefix = config.getPrometheusConfig().prefix;
if (!ports.isEmpty() && path != null) {
result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name,
config.getPrometheusConfig().scrape.orElse(prefix + "/scrape"), "true")));
config.getPrometheusConfig().scrape.orElse(prefix + "/scrape"), "true",
PROMETHEUS_ANNOTATION_TARGETS)));
result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name,
config.getPrometheusConfig().path.orElse(prefix + "/path"), path)));
config.getPrometheusConfig().path.orElse(prefix + "/path"), path, PROMETHEUS_ANNOTATION_TARGETS)));
result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name,
config.getPrometheusConfig().port.orElse(prefix + "/port"), "" + ports.get(0).getPort())));
config.getPrometheusConfig().port.orElse(prefix + "/port"), "" + ports.get(0).getPort(),
PROMETHEUS_ANNOTATION_TARGETS)));
result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name,
config.getPrometheusConfig().scheme.orElse(prefix + "/scheme"), "http")));
config.getPrometheusConfig().scheme.orElse(prefix + "/scheme"), "http",
PROMETHEUS_ANNOTATION_TARGETS)));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

import org.jboss.shrinkwrap.api.ShrinkWrap;
Expand Down Expand Up @@ -35,8 +34,9 @@ public class KubernetesWithMetricsTest {
.setLogFileName("k8s.log")
.withConfigurationResource("kubernetes-with-metrics.properties")
.setForcedDependencies(
Collections.singletonList(
new AppArtifact("io.quarkus", "quarkus-smallrye-metrics", Version.getVersion())));
List.of(
new AppArtifact("io.quarkus", "quarkus-smallrye-metrics", Version.getVersion()),
new AppArtifact("io.quarkus", "quarkus-kubernetes-client", Version.getVersion())));

@ProdBuildResults
private ProdModeTestResults prodModeTestResults;
Expand Down Expand Up @@ -64,20 +64,40 @@ public void assertGeneratedResources() throws IOException {
.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 -> {
assertThat(meta.getAnnotations()).contains(entry("prometheus.io/scrape", "true"),
entry("prometheus.io/path", "/q/metrics"), entry("prometheus.io/port", "9090"),
entry("prometheus.io/scheme", "http"));

assertThat(kubernetesList).filteredOn(h -> "Deployment".equals(h.getKind())).singleElement()
.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 -> {
assertThat(meta.getAnnotations()).contains(entry("prometheus.io/scrape", "true"),
entry("prometheus.io/path", "/q/metrics"), entry("prometheus.io/port", "9090"),
entry("prometheus.io/scheme", "http"));
});
});
});
});
});

assertThat(kubernetesList).filteredOn(h -> "Service".equals(h.getKind())).singleElement().satisfies(h -> {
assertThat(h.getMetadata().getAnnotations()).contains(entry("prometheus.io/scrape", "true"),
entry("prometheus.io/path", "/q/metrics"), entry("prometheus.io/port", "9090"),
entry("prometheus.io/scheme", "http"));
});

assertThat(kubernetesList).filteredOn(h -> "ServiceAccount".equals(h.getKind())).singleElement().satisfies(h -> {
if (h.getMetadata().getAnnotations() != null) {
assertThat(h.getMetadata().getAnnotations()).doesNotContainKeys("prometheus.io/scrape", "prometheus.io/path",
"prometheus.io/port", "prometheus.io/scheme");
}
});

assertThat(kubernetesList).filteredOn(h -> "RoleBinding".equals(h.getKind())).singleElement().satisfies(h -> {
if (h.getMetadata().getAnnotations() != null) {
assertThat(h.getMetadata().getAnnotations()).doesNotContainKeys("prometheus.io/scrape", "prometheus.io/path",
"prometheus.io/port", "prometheus.io/scheme");
}
});
}

Expand Down

0 comments on commit fdddf2a

Please sign in to comment.