Skip to content

Commit

Permalink
Support empty-dir volumes in Kubernetes, OpenShift, Knative extensions
Browse files Browse the repository at this point in the history
Fix #27222
  • Loading branch information
Sgitario committed Sep 5, 2022
1 parent f13f91c commit ae92bdc
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 0 deletions.
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

0 comments on commit ae92bdc

Please sign in to comment.