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

Plugin ephemeral volume feature for CheContainers #14539

Merged
merged 14 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.eclipse.che.workspace.infrastructure.kubernetes.server.secure.DefaultSecureServersFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.server.secure.SecureServerExposerFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.server.secure.SecureServerExposerFactoryProvider;
import org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.ChePluginsVolumeApplier;
import org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.KubernetesPluginsToolingApplier;
import org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.PluginBrokerManager;
import org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.SidecarToolingProvisioner;
Expand Down Expand Up @@ -146,6 +147,7 @@ protected void configure() {
bind(SecureServerExposerFactoryProvider.class)
.to(new TypeLiteral<SecureServerExposerFactoryProvider<KubernetesEnvironment>>() {});

bind(ChePluginsVolumeApplier.class);
sleshchenko marked this conversation as resolved.
Show resolved Hide resolved
MapBinder<String, ChePluginsApplier> chePluginsAppliers =
MapBinder.newMapBinder(binder(), String.class, ChePluginsApplier.class);
chePluginsAppliers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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 java.util.stream.Collectors.toList;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.PersistentVolumeClaim;
import io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder;
import io.fabric8.kubernetes.api.model.PersistentVolumeClaimVolumeSourceBuilder;
import io.fabric8.kubernetes.api.model.PodSpec;
import io.fabric8.kubernetes.api.model.VolumeBuilder;
import io.fabric8.kubernetes.api.model.VolumeMount;
import io.fabric8.kubernetes.api.model.VolumeMountBuilder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.eclipse.che.api.workspace.server.wsplugins.model.Volume;
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment;

@Singleton
public class ChePluginsVolumeApplier {
sleshchenko marked this conversation as resolved.
Show resolved Hide resolved

@Inject
public ChePluginsVolumeApplier() {}

public void applyVolumes(
sleshchenko marked this conversation as resolved.
Show resolved Hide resolved
Container container,
Collection<Volume> volumes,
KubernetesEnvironment kubernetesEnvironment,
KubernetesEnvironment.PodData pod) {
List<Volume> ephemeralVolumes = new ArrayList<>();
List<Volume> persistedVolumes = new ArrayList<>();
for (Volume volume : volumes) {
if (volume.isPersistVolume()) {
persistedVolumes.add(volume);
} else {
ephemeralVolumes.add(volume);
}
}

applyEphemeralVolumes(container, ephemeralVolumes, pod);
applyPersistedVolumes(container, persistedVolumes, pod, kubernetesEnvironment);
}

private void applyEphemeralVolumes(
Container container, List<Volume> volumes, KubernetesEnvironment.PodData pod) {
List<VolumeMount> ephemeralVolumeMounts =
volumes
.stream()
.map(
volume ->
new VolumeMountBuilder()
.withName(volume.getName())
.withMountPath(volume.getMountPath())
.build())
.collect(toList());

container.getVolumeMounts().addAll(ephemeralVolumeMounts);

for (VolumeMount ephemeralVolumeMount : ephemeralVolumeMounts) {
addEmptyDirVolumeIfAbsent(pod.getSpec(), ephemeralVolumeMount.getName());
}
}

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());
}
}

private void applyPersistedVolumes(
Container container,
List<Volume> volumes,
KubernetesEnvironment.PodData pod,
KubernetesEnvironment kubernetesEnvironment) {
List<VolumeMount> volumeMounts =
volumes
.stream()
.map(
volume ->
new VolumeMountBuilder()
.withName(volume.getName())
.withMountPath(volume.getMountPath())
.build())
.collect(toList());

container.getVolumeMounts().addAll(volumeMounts);

for (Volume volume : volumes) {
pod.getSpec()
.getVolumes()
.add(
new VolumeBuilder()
.withName(volume.getName())
.withPersistentVolumeClaim(
new PersistentVolumeClaimVolumeSourceBuilder()
.withClaimName(volume.getName())
.build())
.build());

PersistentVolumeClaim pluginsPVC =
new PersistentVolumeClaimBuilder()
.withNewMetadata()
.withName(volume.getName())
.endMetadata()
.build();
kubernetesEnvironment.getPersistentVolumeClaims().put(volume.getName(), pluginsPVC);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,21 @@ public class KubernetesPluginsToolingApplier implements ChePluginsApplier {
private final String sidecarImagePullPolicy;
private final boolean isAuthEnabled;
private final ProjectsRootEnvVariableProvider projectsRootEnvVariableProvider;
private final ChePluginsVolumeApplier chePluginsVolumeApplier;

@Inject
public KubernetesPluginsToolingApplier(
@Named("che.workspace.sidecar.image_pull_policy") String sidecarImagePullPolicy,
@Named("che.workspace.sidecar.default_memory_limit_mb") long defaultSidecarMemoryLimitMB,
@Named("che.agents.auth_enabled") boolean isAuthEnabled,
ProjectsRootEnvVariableProvider projectsRootEnvVariableProvider) {
ProjectsRootEnvVariableProvider projectsRootEnvVariableProvider,
ChePluginsVolumeApplier chePluginsVolumeApplier) {
this.defaultSidecarMemoryLimitBytes = String.valueOf(defaultSidecarMemoryLimitMB * 1024 * 1024);
this.isAuthEnabled = isAuthEnabled;
this.sidecarImagePullPolicy =
validImagePullPolicies.contains(sidecarImagePullPolicy) ? sidecarImagePullPolicy : null;
this.projectsRootEnvVariableProvider = projectsRootEnvVariableProvider;
this.chePluginsVolumeApplier = chePluginsVolumeApplier;
}

@Override
Expand Down Expand Up @@ -114,7 +117,10 @@ public void apply(
CommandsResolver commandsResolver = new CommandsResolver(kubernetesEnvironment);
for (ChePlugin chePlugin : chePlugins) {
for (CheContainer container : chePlugin.getInitContainers()) {
pod.getSpec().getInitContainers().add(toK8sContainer(container));
Container k8sInitContainer = toK8sContainer(container);
pod.getSpec().getInitContainers().add(k8sInitContainer);
chePluginsVolumeApplier.applyVolumes(
k8sInitContainer, container.getVolumes(), kubernetesEnvironment, pod);
}

Collection<CommandImpl> pluginRelatedCommands = commandsResolver.resolve(chePlugin);
Expand Down Expand Up @@ -211,6 +217,8 @@ private void addSidecar(
List<ChePluginEndpoint> containerEndpoints = k8sContainerResolver.getEndpoints();

Container k8sContainer = k8sContainerResolver.resolve();
chePluginsVolumeApplier.applyVolumes(
k8sContainer, container.getVolumes(), kubernetesEnvironment, pod);

String machineName = k8sContainer.getName();
Names.putMachineName(pod.getMetadata(), k8sContainer.getName(), machineName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ private void normalizeMemory(Container container, InternalMachineConfig machineC
projectsRootPathEnvVar.second,
projectsRootPathEnvVar.first));
}
result.put(volume.getName(), new VolumeImpl().withPath(volume.getMountPath()));
}
return result;
}
Expand Down