Skip to content

Commit

Permalink
Add Ingress annotation for kind
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Sep 29, 2024
1 parent 2811eec commit 8628d9c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -77,6 +75,17 @@ public void createAnnotations(KubernetesConfig config, BuildProducer<KubernetesA
config.getAnnotations().forEach((k, v) -> annotations.produce(new KubernetesAnnotationBuildItem(k, v, KIND)));
}

@BuildStep
public void createIngressAnnotations(KubernetesConfig config, ApplicationInfoBuildItem applicationInfo,
BuildProducer<DecoratorBuildItem> decorators) {
// Ingress
final String name = ResourceNameUtil.getResourceName(config, applicationInfo);
for (Map.Entry<String, String> 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<KubernetesLabelBuildItem> labels,
BuildProducer<ContainerImageLabelBuildItem> imageLabels) {
Expand Down Expand Up @@ -164,7 +173,6 @@ void externalizeInitTasks(
BuildProducer<KubernetesRoleBuildItem> roles,
BuildProducer<KubernetesRoleBindingBuildItem> roleBindings,
BuildProducer<KubernetesServiceAccountBuildItem> serviceAccount,

BuildProducer<DecoratorBuildItem> decorators) {
final String name = ResourceNameUtil.getResourceName(config, applicationInfo);
if (config.isExternalizeInit()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ public class IngressConfig {
@ConfigItem
Map<String, IngressRuleConfig> rules;

public Map<String, String> getAnnotations() {
return annotations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -643,4 +643,8 @@ public Map<String, InitTaskConfig> getInitTasks() {
public InitTaskConfig getInitTaskDefaults() {
return initTaskDefaults;
}

public IngressConfig getIngress() {
return ingress;
}
}
Original file line number Diff line number Diff line change
@@ -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<HasMetadata> kubernetesList = DeserializationUtil.deserializeAsList(kubernetesFile);

SoftAssertions.assertSoftly(softly -> {
softly.assertThat(kubernetesList).hasSize(3);
Optional<HasMetadata> 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");
});
});

}
}

0 comments on commit 8628d9c

Please sign in to comment.