Skip to content

Commit

Permalink
quarkus.knative.app-config-map misses adding the volumes to the ksvc …
Browse files Browse the repository at this point in the history
…template

#42466
  • Loading branch information
claudio4j committed Aug 16, 2024
1 parent 9070ad7 commit c9d8ce2
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import static io.quarkus.kubernetes.spi.KubernetesDeploymentTargetBuildItem.DEFAULT_PRIORITY;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.dekorate.knative.decorator.AddAwsElasticBlockStoreVolumeToRevisionDecorator;
Expand All @@ -28,13 +31,17 @@
import io.dekorate.knative.decorator.ApplyRevisionNameDecorator;
import io.dekorate.knative.decorator.ApplyServiceAccountToRevisionSpecDecorator;
import io.dekorate.knative.decorator.ApplyTrafficDecorator;
import io.dekorate.kubernetes.config.ConfigMapVolumeBuilder;
import io.dekorate.kubernetes.config.EnvBuilder;
import io.dekorate.kubernetes.config.MountBuilder;
import io.dekorate.kubernetes.config.Port;
import io.dekorate.kubernetes.config.SecretVolumeBuilder;
import io.dekorate.kubernetes.decorator.AddConfigMapDataDecorator;
import io.dekorate.kubernetes.decorator.AddConfigMapResourceProvidingDecorator;
import io.dekorate.kubernetes.decorator.AddEnvVarDecorator;
import io.dekorate.kubernetes.decorator.AddImagePullSecretToServiceAccountDecorator;
import io.dekorate.kubernetes.decorator.AddLabelDecorator;
import io.dekorate.kubernetes.decorator.AddMountDecorator;
import io.dekorate.kubernetes.decorator.AddServiceAccountResourceDecorator;
import io.dekorate.kubernetes.decorator.ApplicationContainerDecorator;
import io.dekorate.kubernetes.decorator.ApplyImagePullPolicyDecorator;
Expand Down Expand Up @@ -311,6 +318,7 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic

//Add revision decorators
result.addAll(createVolumeDecorators(project, name, config));
result.addAll(createAppConfigVolumeAndEnvDecorators(project, name, config));
config.getHostAliases().entrySet().forEach(e -> {
result.add(new DecoratorBuildItem(KNATIVE,
new AddHostAliasesToRevisionDecorator(name, HostAliasConverter.convert(e))));
Expand Down Expand Up @@ -375,4 +383,45 @@ private static List<DecoratorBuildItem> createVolumeDecorators(Optional<Project>
});
return result;
}

private static List<DecoratorBuildItem> createAppConfigVolumeAndEnvDecorators(Optional<Project> project, String name,
PlatformConfiguration config) {

List<DecoratorBuildItem> result = new ArrayList<>();
Set<String> paths = new HashSet<>();

config.getAppSecret().ifPresent(s -> {
result.add(new DecoratorBuildItem(KNATIVE, new AddSecretVolumeToRevisionDecorator(new SecretVolumeBuilder()
.withSecretName(s)
.withVolumeName("app-secret")
.build())));
result.add(new DecoratorBuildItem(KNATIVE, new AddMountDecorator(new MountBuilder()
.withName("app-secret")
.withPath("/mnt/app-secret")
.build())));
paths.add("/mnt/app-secret");
});

config.getAppConfigMap().ifPresent(s -> {
result.add(new DecoratorBuildItem(KNATIVE, new AddConfigMapVolumeToRevisionDecorator(new ConfigMapVolumeBuilder()
.withConfigMapName(s)
.withVolumeName("app-config-map")
.build())));
result.add(new DecoratorBuildItem(KNATIVE, new AddMountDecorator(new MountBuilder()
.withName("app-config-map")
.withPath("/mnt/app-config-map")
.build())));
paths.add("/mnt/app-config-map");
});

if (!paths.isEmpty()) {
result.add(new DecoratorBuildItem(KNATIVE,
new AddEnvVarDecorator(ApplicationContainerDecorator.ANY, name, new EnvBuilder()
.withName("SMALLRYE_CONFIG_LOCATIONS")
.withValue(paths.stream().collect(Collectors.joining(",")))
.build())));
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.quarkus.it.kubernetes;

import static org.assertj.core.api.Assertions.assertThat;

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

import org.assertj.core.api.AbstractObjectAssert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.fabric8.knative.serving.v1.RevisionSpec;
import io.fabric8.knative.serving.v1.Service;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeMount;
import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;

public class KnativeWithAppConfigMapTest {

@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class))
.setApplicationName("knative-with-app-config-map")
.setApplicationVersion("0.1-SNAPSHOT")
.withConfigurationResource("knative-with-app-config-map.properties");

@ProdBuildResults
private ProdModeTestResults prodModeTestResults;

@Test
public void assertGeneratedResources() throws IOException {
Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
assertThat(kubernetesDir)
.isDirectoryContaining(p -> p.getFileName().endsWith("knative.json"))
.isDirectoryContaining(p -> p.getFileName().endsWith("knative.yml"))
.satisfies(p -> assertThat(p.toFile().listFiles()).hasSize(2));

List<HasMetadata> kubernetesList = DeserializationUtil
.deserializeAsList(kubernetesDir.resolve("knative.yml"));

assertThat(kubernetesList).filteredOn(i -> "Service".equals(i.getKind())).singleElement().satisfies(i -> {
assertThat(i).isInstanceOfSatisfying(Service.class, s -> {
assertThat(s.getSpec()).satisfies(spec -> {
assertThat(s.getMetadata()).satisfies(m -> {
assertThat(m.getName()).isEqualTo("knative-with-app-config-map");
});
});
});

AbstractObjectAssert<?, ?> specAssert = assertThat(i).extracting("spec");
specAssert.extracting("template").extracting("spec").isInstanceOfSatisfying(RevisionSpec.class,
revisionSpec -> {
assertThat(revisionSpec.getContainers()).singleElement().satisfies(container -> {
List<EnvVar> envVars = container.getEnv();
assertThat(envVars).anySatisfy(envVar -> {
assertThat(envVar.getName()).isEqualTo("SMALLRYE_CONFIG_LOCATIONS");
assertThat(envVar.getValue()).isEqualTo("/mnt/app-config-map");
});

List<VolumeMount> mounts = container.getVolumeMounts();
assertThat(mounts).anySatisfy(mount -> {
assertThat(mount.getName()).isEqualTo("app-config-map");
assertThat(mount.getMountPath()).isEqualTo("/mnt/app-config-map");
});
});
List<Volume> volumes = revisionSpec.getVolumes();
assertThat(volumes).anySatisfy(volume -> {
assertThat(volume.getName()).isEqualTo("app-config-map");
assertThat(volume.getConfigMap().getName()).isEqualTo("my-kn-config-map");
});
});
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.quarkus.it.kubernetes;

import static org.assertj.core.api.Assertions.assertThat;

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

import org.assertj.core.api.AbstractObjectAssert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.fabric8.knative.serving.v1.RevisionSpec;
import io.fabric8.knative.serving.v1.Service;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeMount;
import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;

public class KnativeWithAppSecretTest {

@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class))
.setApplicationName("knative-with-app-secret")
.setApplicationVersion("0.1-SNAPSHOT")
.withConfigurationResource("knative-with-app-secret.properties");

@ProdBuildResults
private ProdModeTestResults prodModeTestResults;

@Test
public void assertGeneratedResources() throws IOException {
Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
assertThat(kubernetesDir)
.isDirectoryContaining(p -> p.getFileName().endsWith("knative.json"))
.isDirectoryContaining(p -> p.getFileName().endsWith("knative.yml"))
.satisfies(p -> assertThat(p.toFile().listFiles()).hasSize(2));

List<HasMetadata> kubernetesList = DeserializationUtil
.deserializeAsList(kubernetesDir.resolve("knative.yml"));

assertThat(kubernetesList).filteredOn(i -> "Service".equals(i.getKind())).singleElement().satisfies(i -> {
assertThat(i).isInstanceOfSatisfying(Service.class, s -> {
assertThat(s.getSpec()).satisfies(spec -> {
assertThat(s.getMetadata()).satisfies(m -> {
assertThat(m.getName()).isEqualTo("knative-with-app-secret");
});
});
});

AbstractObjectAssert<?, ?> specAssert = assertThat(i).extracting("spec");
specAssert.extracting("template").extracting("spec").isInstanceOfSatisfying(RevisionSpec.class,
revisionSpec -> {
assertThat(revisionSpec.getContainers()).singleElement().satisfies(container -> {
List<EnvVar> envVars = container.getEnv();
assertThat(envVars).anySatisfy(envVar -> {
assertThat(envVar.getName()).isEqualTo("SMALLRYE_CONFIG_LOCATIONS");
assertThat(envVar.getValue()).isEqualTo("/mnt/app-secret");
});

List<VolumeMount> mounts = container.getVolumeMounts();
assertThat(mounts).anySatisfy(mount -> {
assertThat(mount.getName()).isEqualTo("app-secret");
assertThat(mount.getMountPath()).isEqualTo("/mnt/app-secret");
});
});
List<Volume> volumes = revisionSpec.getVolumes();
assertThat(volumes).anySatisfy(volume -> {
assertThat(volume.getName()).isEqualTo("app-secret");
assertThat(volume.getSecret().getSecretName()).isEqualTo("my-kn-secret");
});
});
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.kubernetes.deployment-target=knative
quarkus.knative.app-config-map=my-kn-config-map
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.kubernetes.deployment-target=knative
quarkus.knative.app-secret=my-kn-secret

0 comments on commit c9d8ce2

Please sign in to comment.