下面列出了io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder#io.fabric8.kubernetes.api.model.EmptyDirVolumeSource 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates an empty directory volume
*
* @param name Name of the Volume
* @param sizeLimit Volume size
* @return The Volume created
*/
public static Volume createEmptyDirVolume(String name, String sizeLimit) {
String validName = getValidVolumeName(name);
EmptyDirVolumeSource emptyDirVolumeSource = new EmptyDirVolumeSourceBuilder().build();
if (sizeLimit != null && !sizeLimit.isEmpty()) {
emptyDirVolumeSource.setSizeLimit(new Quantity(sizeLimit));
}
Volume volume = new VolumeBuilder()
.withName(validName)
.withEmptyDir(emptyDirVolumeSource)
.build();
log.trace("Created emptyDir Volume named '{}' with sizeLimit '{}'", validName, sizeLimit);
return volume;
}
private void replacePVCsWithEmptyDir(KubernetesEnvironment k8sEnv) {
for (PodData pod : k8sEnv.getPodsData().values()) {
PodSpec podSpec = pod.getSpec();
podSpec
.getVolumes()
.stream()
.filter(v -> v.getPersistentVolumeClaim() != null)
.forEach(
v -> {
v.setPersistentVolumeClaim(null);
v.setEmptyDir(new EmptyDirVolumeSource());
});
}
}
@Test
public void testConvertsAllPVCsToEmptyDir() throws Exception {
// given
k8sEnv.getPersistentVolumeClaims().put("pvc1", mock(PersistentVolumeClaim.class));
k8sEnv.getPersistentVolumeClaims().put("pvc2", mock(PersistentVolumeClaim.class));
io.fabric8.kubernetes.api.model.Volume configMapVolume =
new VolumeBuilder().withNewConfigMap().withName("configMap").endConfigMap().build();
io.fabric8.kubernetes.api.model.Volume emptyDirVolume =
new VolumeBuilder().withNewEmptyDir().endEmptyDir().build();
io.fabric8.kubernetes.api.model.Volume pvcVolume =
new VolumeBuilder()
.withNewPersistentVolumeClaim()
.withClaimName("pvc1")
.endPersistentVolumeClaim()
.build();
Pod pod =
new PodBuilder()
.withNewMetadata()
.withName(POD_NAME)
.endMetadata()
.withNewSpec()
.withVolumes(
new VolumeBuilder(pvcVolume).build(),
new VolumeBuilder(configMapVolume).build(),
new VolumeBuilder(emptyDirVolume).build())
.endSpec()
.build();
k8sEnv.addPod(pod);
ephemeralWorkspaceAdapter.provision(k8sEnv, identity);
assertTrue(k8sEnv.getPersistentVolumeClaims().isEmpty());
assertNull(pod.getSpec().getVolumes().get(0).getPersistentVolumeClaim());
assertEquals(pod.getSpec().getVolumes().get(0).getEmptyDir(), new EmptyDirVolumeSource());
assertEquals(pod.getSpec().getVolumes().get(1), configMapVolume);
assertEquals(pod.getSpec().getVolumes().get(2), emptyDirVolume);
}
public PodSpec buildPodSpec(
final String rabbitName,
final String initContainerImage,
final Container container
) {
return new PodSpecBuilder()
.withServiceAccountName("rabbitmq-user")
.addNewInitContainer()
.withName("copy-rabbitmq-config")
.withImage(initContainerImage)
.withCommand(new String[]{"sh", "-c", "cp /configmap/* /etc/rabbitmq"})
.addNewVolumeMount().withName("config").withMountPath("/etc/rabbitmq").endVolumeMount()
.addNewVolumeMount().withName("configmap").withMountPath("/configmap").endVolumeMount()
.endInitContainer()
.withContainers(container)
.addNewVolume().withName("config").withEmptyDir(new EmptyDirVolumeSource()).endVolume()
.addNewVolume().withName("configmap").withConfigMap(
new ConfigMapVolumeSourceBuilder()
.withName("rabbitmq-config")
.addNewItem("rabbitmq.conf", 0644, "rabbitmq.conf")
.addNewItem("enabled_plugins", 0644, "enabled_plugins")
.build()
).endVolume()
.addNewVolume().withName("probes").withConfigMap(
new ConfigMapVolumeSourceBuilder()
.withName("rabbitmq-probes")
.addNewItem("readiness.sh", 0755, "readiness.sh")
.build()
).endVolume()
.addNewVolume().withName("startup-scripts").withConfigMap(
new ConfigMapVolumeSourceBuilder()
.withName("rabbitmq-startup-scripts")
.addNewItem("users.sh", 0755, "users.sh")
.build()
).endVolume()
.withNewAffinity().withNewPodAntiAffinity().withPreferredDuringSchedulingIgnoredDuringExecution(
new WeightedPodAffinityTermBuilder()
.withNewWeight(1)
.withNewPodAffinityTerm()
.withNewLabelSelector()
.addToMatchLabels(Labels.Kubernetes.INSTANCE, rabbitName)
.endLabelSelector()
.withTopologyKey("kubernetes.io/hostname")
.endPodAffinityTerm()
.build()
).endPodAntiAffinity().endAffinity()
.build();
}