From 8628d9cebbc029bb7c12a646b5d18836a394719d Mon Sep 17 00:00:00 2001 From: Matheus Cruz Date: Sat, 28 Sep 2024 23:05:57 -0300 Subject: [PATCH] Add Ingress annotation for kind --- .../kind/deployment/KindProcessor.java | 20 ++++-- .../kubernetes/deployment/IngressConfig.java | 3 + .../deployment/KubernetesConfig.java | 4 ++ .../kubernetes/KubernetesWithKindIngress.java | 61 +++++++++++++++++++ 4 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithKindIngress.java diff --git a/extensions/kubernetes/kind/deployment/src/main/java/io/quarkus/kind/deployment/KindProcessor.java b/extensions/kubernetes/kind/deployment/src/main/java/io/quarkus/kind/deployment/KindProcessor.java index b3b2ce748ddf23..02ac1150aa15c4 100644 --- a/extensions/kubernetes/kind/deployment/src/main/java/io/quarkus/kind/deployment/KindProcessor.java +++ b/extensions/kubernetes/kind/deployment/src/main/java/io/quarkus/kind/deployment/KindProcessor.java @@ -1,16 +1,14 @@ package io.quarkus.kind.deployment; -import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT; -import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_GROUP; -import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_VERSION; -import static io.quarkus.kubernetes.deployment.Constants.KIND; -import static io.quarkus.kubernetes.deployment.Constants.KUBERNETES; +import static io.quarkus.kubernetes.deployment.Constants.*; import static io.quarkus.kubernetes.spi.KubernetesDeploymentTargetBuildItem.DEFAULT_PRIORITY; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Optional; +import io.dekorate.kubernetes.decorator.AddAnnotationDecorator; import io.quarkus.container.spi.BaseImageInfoBuildItem; import io.quarkus.container.spi.ContainerImageBuilderBuildItem; import io.quarkus.container.spi.ContainerImageInfoBuildItem; @@ -77,6 +75,17 @@ public void createAnnotations(KubernetesConfig config, BuildProducer annotations.produce(new KubernetesAnnotationBuildItem(k, v, KIND))); } + @BuildStep + public void createIngressAnnotations(KubernetesConfig config, ApplicationInfoBuildItem applicationInfo, + BuildProducer decorators) { + // Ingress + final String name = ResourceNameUtil.getResourceName(config, applicationInfo); + for (Map.Entry annotation : config.getIngress().getAnnotations().entrySet()) { + decorators.produce(new DecoratorBuildItem(KIND, + new AddAnnotationDecorator(name, annotation.getKey(), annotation.getValue(), INGRESS))); + } + } + @BuildStep public void createLabels(KubernetesConfig config, BuildProducer labels, BuildProducer imageLabels) { @@ -164,7 +173,6 @@ void externalizeInitTasks( BuildProducer roles, BuildProducer roleBindings, BuildProducer serviceAccount, - BuildProducer decorators) { final String name = ResourceNameUtil.getResourceName(config, applicationInfo); if (config.isExternalizeInit()) { diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/IngressConfig.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/IngressConfig.java index d1ea33b6c830ae..6e0c4a2ad920b7 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/IngressConfig.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/IngressConfig.java @@ -54,4 +54,7 @@ public class IngressConfig { @ConfigItem Map rules; + public Map getAnnotations() { + return annotations; + } } diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java index 33feeca5700e61..861c45927b514d 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java @@ -643,4 +643,8 @@ public Map getInitTasks() { public InitTaskConfig getInitTaskDefaults() { return initTaskDefaults; } + + public IngressConfig getIngress() { + return ingress; + } } diff --git a/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithKindIngress.java b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithKindIngress.java new file mode 100644 index 00000000000000..31ef347b5cfe58 --- /dev/null +++ b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithKindIngress.java @@ -0,0 +1,61 @@ +package io.quarkus.it.kubernetes; + +import io.fabric8.kubernetes.api.model.HasMetadata; +import io.quarkus.builder.Version; +import io.quarkus.maven.dependency.Dependency; +import io.quarkus.test.ProdBuildResults; +import io.quarkus.test.ProdModeTestResults; +import io.quarkus.test.QuarkusProdModeTest; +import org.assertj.core.api.SoftAssertions; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +public class KubernetesWithKindIngress { + + @RegisterExtension + static final QuarkusProdModeTest config = new QuarkusProdModeTest() + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(GreetingResource.class)) + .setApplicationName("kind-ingress") + .setApplicationVersion("0.1-SNAPSHOT") + .setLogFileName("k8s.log") + .overrideConfigKey("quarkus.kubernetes.ingress.expose", "true") + .overrideConfigKey("quarkus.kubernetes.ingress.annotations.\"nginx.ingress.kubernetes.io/rewrite-target\"", "/$2") + .setForcedDependencies(Arrays.asList( + Dependency.of("io.quarkus", "quarkus-kind", Version.getVersion()), + Dependency.of("io.quarkus", "quarkus-kubernetes", Version.getVersion()))); + + private static final Logger log = LoggerFactory.getLogger(KubernetesWithKindIngress.class); + + @ProdBuildResults + private ProdModeTestResults prodModeTestResults; + + @Test + void kindWithIngress() throws IOException { + final Path kubernetesFile = prodModeTestResults.getBuildDir().resolve("kubernetes").resolve("kind.yml"); + List kubernetesList = DeserializationUtil.deserializeAsList(kubernetesFile); + + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(kubernetesList).hasSize(3); + Optional ingress = kubernetesList.stream().filter(item -> item.getKind().equals("Ingress")).findFirst(); + softly.assertThat(ingress).isPresent(); + softly.assertThat(ingress.get().getMetadata().getAnnotations()) + .anySatisfy((key, value) -> { + assertThat(key).isEqualTo("nginx.ingress.kubernetes.io/rewrite-target"); + assertThat(value).isEqualTo("/$2"); + }); + }); + + } +}