Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only add prometheus annotation and specific k8s resources #18989

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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