-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugin ephemeral volume feature for CheContainers (#14539)
* Add plugin ephemeral volume feature Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Move plugin containers volume logic to external class. Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Fix volume solution to work with unique pvc strategy Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Clean up. Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Clean up Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Clean up. Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Rename field persistVolume to ephemeral Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Fix up. Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Apply proposed changes changes. Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Apply proposed changes + enhasments. Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Reuse pvc claim from k8sEnv Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Use released version che-plugin-broker. Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * Use newer version che-init-plugin-broker Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com>
- Loading branch information
1 parent
ece3257
commit dd47af6
Showing
7 changed files
with
244 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...rg/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/ChePluginsVolumeApplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (c) 2012-2018 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
package org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins; | ||
|
||
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newPVC; | ||
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolume; | ||
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newVolumeMount; | ||
|
||
import io.fabric8.kubernetes.api.model.Container; | ||
import io.fabric8.kubernetes.api.model.PersistentVolumeClaim; | ||
import io.fabric8.kubernetes.api.model.PodSpec; | ||
import io.fabric8.kubernetes.api.model.VolumeBuilder; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
import org.eclipse.che.api.workspace.server.wsplugins.model.Volume; | ||
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment; | ||
|
||
/** | ||
* Components for applying workspace plugin volumes to the kubernetes {@link | ||
* io.fabric8.kubernetes.api.model.Container}. | ||
*/ | ||
@Singleton | ||
public class ChePluginsVolumeApplier { | ||
|
||
private final String pvcQuantity; | ||
private final String pvcAccessMode; | ||
private final String pvcStorageClassName; | ||
|
||
@Inject | ||
public ChePluginsVolumeApplier( | ||
@Named("che.infra.kubernetes.pvc.quantity") String pvcQuantity, | ||
@Named("che.infra.kubernetes.pvc.access_mode") String pvcAccessMode, | ||
@Named("che.infra.kubernetes.pvc.storage_class_name") String pvcStorageClassName) { | ||
this.pvcQuantity = pvcQuantity; | ||
this.pvcAccessMode = pvcAccessMode; | ||
this.pvcStorageClassName = pvcStorageClassName; | ||
} | ||
|
||
public void applyVolumes( | ||
KubernetesEnvironment.PodData pod, | ||
Container container, | ||
Collection<Volume> volumes, | ||
KubernetesEnvironment k8sEnv) { | ||
for (Volume volume : volumes) { | ||
String podVolumeName = provisionPodVolume(volume, pod, k8sEnv); | ||
|
||
container.getVolumeMounts().add(newVolumeMount(podVolumeName, volume.getMountPath(), "")); | ||
} | ||
} | ||
|
||
private String provisionPodVolume( | ||
Volume volume, KubernetesEnvironment.PodData pod, KubernetesEnvironment k8sEnv) { | ||
if (volume.isEphemeral()) { | ||
addEmptyDirVolumeIfAbsent(pod.getSpec(), volume.getName()); | ||
return volume.getName(); | ||
} else { | ||
return provisionPVCPodVolume(volume, pod, k8sEnv).getName(); | ||
} | ||
} | ||
|
||
private io.fabric8.kubernetes.api.model.Volume provisionPVCPodVolume( | ||
Volume volume, KubernetesEnvironment.PodData pod, KubernetesEnvironment k8sEnv) { | ||
String pvcName = volume.getName(); | ||
|
||
if (!k8sEnv.getPersistentVolumeClaims().containsKey(pvcName)) { | ||
final PersistentVolumeClaim pvc = | ||
newPVC(pvcName, pvcAccessMode, pvcQuantity, pvcStorageClassName); | ||
k8sEnv.getPersistentVolumeClaims().put(pvcName, pvc); | ||
} | ||
|
||
PodSpec podSpec = pod.getSpec(); | ||
Optional<io.fabric8.kubernetes.api.model.Volume> volumeOpt = | ||
podSpec | ||
.getVolumes() | ||
.stream() | ||
.filter( | ||
vm -> | ||
vm.getPersistentVolumeClaim() != null | ||
&& pvcName.equals(vm.getPersistentVolumeClaim().getClaimName())) | ||
.findAny(); | ||
io.fabric8.kubernetes.api.model.Volume podVolume; | ||
if (volumeOpt.isPresent()) { | ||
podVolume = volumeOpt.get(); | ||
} else { | ||
podVolume = newVolume(pvcName, pvcName); | ||
podSpec.getVolumes().add(podVolume); | ||
} | ||
return podVolume; | ||
} | ||
|
||
private void addEmptyDirVolumeIfAbsent(PodSpec podSpec, String uniqueVolumeName) { | ||
if (podSpec | ||
.getVolumes() | ||
.stream() | ||
.noneMatch(volume -> volume.getName().equals(uniqueVolumeName))) { | ||
podSpec | ||
.getVolumes() | ||
.add( | ||
new VolumeBuilder() | ||
.withName(uniqueVolumeName) | ||
.withNewEmptyDir() | ||
.endEmptyDir() | ||
.build()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.