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

Support empty-dir volumes in Kubernetes, OpenShift, Knative extensions #27711

Merged
merged 1 commit into from
Sep 5, 2022
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
@@ -0,0 +1,15 @@

package io.quarkus.kubernetes.deployment;

import io.dekorate.kubernetes.config.EmptyDirVolume;
import io.dekorate.kubernetes.config.EmptyDirVolumeBuilder;

public class EmptyDirVolumeConverter {

public static EmptyDirVolume convert(String volumeName) {
EmptyDirVolumeBuilder b = new EmptyDirVolumeBuilder();
b.withVolumeName(volumeName);
return b.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.kubernetes.deployment;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -156,6 +157,12 @@ public class KnativeConfig implements PlatformConfiguration {
@ConfigItem
Map<String, ConfigMapVolumeConfig> configMapVolumes;

/**
* EmptyDir volumes
*/
@ConfigItem
Optional<List<String>> emptyDirVolumes;

/**
* Git Repository volumes
*/
Expand Down Expand Up @@ -315,6 +322,10 @@ public Map<String, ConfigMapVolumeConfig> getConfigMapVolumes() {
return configMapVolumes;
}

public List<String> getEmptyDirVolumes() {
return emptyDirVolumes.orElse(Collections.emptyList());
}

public Map<String, GitRepoVolumeConfig> getGitRepoVolumes() {
return gitRepoVolumes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.dekorate.knative.decorator.AddAzureDiskVolumeToRevisionDecorator;
import io.dekorate.knative.decorator.AddAzureFileVolumeToRevisionDecorator;
import io.dekorate.knative.decorator.AddConfigMapVolumeToRevisionDecorator;
import io.dekorate.knative.decorator.AddEmptyDirVolumeToRevisionDecorator;
import io.dekorate.knative.decorator.AddHostAliasesToRevisionDecorator;
import io.dekorate.knative.decorator.AddPvcVolumeToRevisionDecorator;
import io.dekorate.knative.decorator.AddSecretVolumeToRevisionDecorator;
Expand Down Expand Up @@ -333,6 +334,11 @@ private static List<DecoratorBuildItem> createVolumeDecorators(Optional<Project>
new AddConfigMapVolumeToRevisionDecorator(ConfigMapVolumeConverter.convert(e))));
});

config.getEmptyDirVolumes().forEach(e -> {
result.add(new DecoratorBuildItem(KNATIVE,
new AddEmptyDirVolumeToRevisionDecorator(EmptyDirVolumeConverter.convert(e))));
});

config.getPvcVolumes().entrySet().forEach(e -> {
result.add(new DecoratorBuildItem(KNATIVE, new AddPvcVolumeToRevisionDecorator(PvcVolumeConverter.convert(e))));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.dekorate.kubernetes.decorator.AddAzureDiskVolumeDecorator;
import io.dekorate.kubernetes.decorator.AddAzureFileVolumeDecorator;
import io.dekorate.kubernetes.decorator.AddConfigMapVolumeDecorator;
import io.dekorate.kubernetes.decorator.AddEmptyDirVolumeDecorator;
import io.dekorate.kubernetes.decorator.AddEnvVarDecorator;
import io.dekorate.kubernetes.decorator.AddHostAliasesDecorator;
import io.dekorate.kubernetes.decorator.AddImagePullSecretDecorator;
Expand Down Expand Up @@ -383,6 +384,10 @@ private static List<DecoratorBuildItem> createMountAndVolumeDecorators(Optional<
result.add(new DecoratorBuildItem(target, new AddConfigMapVolumeDecorator(ConfigMapVolumeConverter.convert(e))));
});

config.getEmptyDirVolumes().forEach(e -> {
result.add(new DecoratorBuildItem(target, new AddEmptyDirVolumeDecorator(EmptyDirVolumeConverter.convert(e))));
});

config.getPvcVolumes().entrySet().forEach(e -> {
result.add(new DecoratorBuildItem(target, new AddPvcVolumeDecorator(PvcVolumeConverter.convert(e))));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT;
import static io.quarkus.kubernetes.deployment.Constants.STATEFULSET;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -191,6 +192,12 @@ public enum DeploymentResourceKind {
@ConfigItem
Map<String, ConfigMapVolumeConfig> configMapVolumes;

/**
* EmptyDir volumes
*/
@ConfigItem
Optional<List<String>> emptyDirVolumes;

/**
* Git Repository volumes
*/
Expand Down Expand Up @@ -453,6 +460,10 @@ public Map<String, ConfigMapVolumeConfig> getConfigMapVolumes() {
return configMapVolumes;
}

public List<String> getEmptyDirVolumes() {
return emptyDirVolumes.orElse(Collections.emptyList());
}

public Map<String, GitRepoVolumeConfig> getGitRepoVolumes() {
return gitRepoVolumes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static io.quarkus.kubernetes.deployment.Constants.S2I;
import static io.quarkus.kubernetes.deployment.Constants.STATEFULSET;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -220,6 +221,12 @@ public static enum DeploymentResourceKind {
@ConfigItem
Map<String, ConfigMapVolumeConfig> configMapVolumes;

/**
* EmptyDir volumes
*/
@ConfigItem
Optional<List<String>> emptyDirVolumes;

/**
* Git Repository volumes
*/
Expand Down Expand Up @@ -406,6 +413,10 @@ public Map<String, ConfigMapVolumeConfig> getConfigMapVolumes() {
return configMapVolumes;
}

public List<String> getEmptyDirVolumes() {
return emptyDirVolumes.orElse(Collections.emptyList());
}

public Map<String, GitRepoVolumeConfig> getGitRepoVolumes() {
return gitRepoVolumes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public interface PlatformConfiguration extends EnvVarHolder {

Map<String, ConfigMapVolumeConfig> getConfigMapVolumes();

List<String> getEmptyDirVolumes();

Map<String, GitRepoVolumeConfig> getGitRepoVolumes();

Map<String, PvcVolumeConfig> getPvcVolumes();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

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.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.apps.Deployment;
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;

public class KubernetesWithEmptyDirVolumesTest {

private static final String NAME = "kubernetes-with-emptydir-volumes";

@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class))
.setApplicationName(NAME)
.setApplicationVersion("0.1-SNAPSHOT")
.withConfigurationResource(NAME + ".properties")
.setLogFileName("k8s.log")
.setForcedDependencies(List.of(Dependency.of("io.quarkus", "quarkus-kubernetes", Version.getVersion())));

@ProdBuildResults
private ProdModeTestResults prodModeTestResults;

@Test
public void assertGeneratedResources() throws IOException {
final Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
assertThat(kubernetesDir)
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json"))
.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(NAME);
});

assertThat(d.getSpec()).satisfies(deploymentSpec -> {
assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
assertThat(t.getSpec()).satisfies(podSpec -> {
assertThat(podSpec.getVolumes()).isNotEmpty();
assertThat(podSpec.getVolumes()).anySatisfy(volume -> {
assertThat(volume.getName()).isEqualTo("cache-volume");
assertThat(volume.getEmptyDir()).isNotNull();
});
assertThat(podSpec.getVolumes()).anySatisfy(volume -> {
assertThat(volume.getName()).isEqualTo("app-data");
assertThat(volume.getEmptyDir()).isNotNull();
});
});
});
});
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
quarkus.kubernetes.mounts.cache-volume.path=/cache
quarkus.kubernetes.mounts.app-data.path=/data
quarkus.kubernetes.empty-dir-volumes=cache-volume,app-data