Skip to content

Commit

Permalink
fixup! Propagate default ephemeral mode value
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Leshchenko <[email protected]>
  • Loading branch information
sleshchenko committed Jan 21, 2020
1 parent 8b77f7f commit 956d17c
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,12 @@ che.workspace.plugin_registry_url=https://che-plugin-registry.prod-preview.opens
# In case Che plugins tooling is not needed value 'NULL' should be used
che.workspace.devfile_registry_url=https://che-devfile-registry.prod-preview.openshift.io/

# Defines a default behaviour if user does not configure persist volumes.
# Possible values: true or false
# In case of false - PersistentVolumeClaims are used by declared volumes by user and plugins.
# In case of true - emptyDir is used instead of PVCs. Note that data will be lost after workspace restart.
che.workspace.persist_volumes=false

# Configures in which way secure servers will be protected with authentication.
# Suitable values:
# - 'default': no additionally authentication system will be enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ protected PersistentVolumeClaim createCommonPVC(String workspaceId) {
public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity)
throws InfrastructureException {
final String workspaceId = identity.getWorkspaceId();
if (EphemeralWorkspaceUtility.isEphemeral(k8sEnv.getAttributes())) {
if (ephemeralWorkspaceAdapter.isEphemeral(k8sEnv.getAttributes())) {
ephemeralWorkspaceAdapter.provision(k8sEnv, identity);
return;
}
Expand Down Expand Up @@ -179,7 +179,7 @@ public void prepare(KubernetesEnvironment k8sEnv, RuntimeIdentity identity, long

TracingTags.WORKSPACE_ID.set(workspaceId);

if (EphemeralWorkspaceUtility.isEphemeral(k8sEnv.getAttributes())) {
if (ephemeralWorkspaceAdapter.isEphemeral(k8sEnv.getAttributes())) {
return;
}

Expand Down Expand Up @@ -227,7 +227,7 @@ public void prepare(KubernetesEnvironment k8sEnv, RuntimeIdentity identity, long

@Override
public void cleanup(Workspace workspace) throws InfrastructureException {
if (EphemeralWorkspaceUtility.isEphemeral(workspace)) {
if (ephemeralWorkspaceAdapter.isEphemeral(workspace)) {
return;
}
String workspaceId = workspace.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
*/
package org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc;

import static org.eclipse.che.api.workspace.shared.Constants.CHE_WORKSPACE_PERSIST_VOLUMES_PROPERTY;
import static org.eclipse.che.api.workspace.shared.Constants.PERSIST_VOLUMES_ATTRIBUTE;

import io.fabric8.kubernetes.api.model.EmptyDirVolumeSource;
import io.fabric8.kubernetes.api.model.PersistentVolumeClaim;
import io.fabric8.kubernetes.api.model.PodSpec;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.eclipse.che.api.core.model.workspace.Workspace;
import org.eclipse.che.api.core.model.workspace.devfile.Devfile;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment;
Expand All @@ -44,11 +50,59 @@ public class EphemeralWorkspaceAdapter {

private final PVCProvisioner pvcProvisioner;
private final SubPathPrefixes subPathPrefixes;
private final boolean defaultPersistVolumes;

@Inject
public EphemeralWorkspaceAdapter(PVCProvisioner pvcProvisioner, SubPathPrefixes subPathPrefixes) {
public EphemeralWorkspaceAdapter(
PVCProvisioner pvcProvisioner,
SubPathPrefixes subPathPrefixes,
@Named(CHE_WORKSPACE_PERSIST_VOLUMES_PROPERTY) boolean defaultPersistVolumes) {
this.pvcProvisioner = pvcProvisioner;
this.subPathPrefixes = subPathPrefixes;
this.defaultPersistVolumes = defaultPersistVolumes;
}

/**
* @param workspaceAttributes workspace config or devfile attributes to check is ephemeral mode is
* enabled
* @return true if `persistVolumes` attribute exists and set to 'false'. In this case regardless
* of the PVC strategy, workspace volumes would be created as `emptyDir`. When a workspace Pod
* is removed for any reason, the data in the `emptyDir` volume is deleted forever
*/
public boolean isEphemeral(Map<String, String> workspaceAttributes) {
String persistVolumes = workspaceAttributes.get(PERSIST_VOLUMES_ATTRIBUTE);
if (persistVolumes == null) {
return !defaultPersistVolumes;
} else {
return !"true".equals(persistVolumes);
}
}

/**
* @param workspace workspace to check is ephemeral mode is enabled
* @return true if workspace config contains `persistVolumes` attribute which is set to false. In
* this case regardless of the PVC strategy, workspace volumes would be created as `emptyDir`.
* When a workspace Pod is removed for any reason, the data in the `emptyDir` volume is
* deleted forever
*/
public boolean isEphemeral(Workspace workspace) {
Devfile devfile = workspace.getDevfile();
if (devfile != null) {
return isEphemeral(devfile.getAttributes());
}

return isEphemeral(workspace.getConfig().getAttributes());
}

/**
* Change workspace attributes such that future calls to {@link #isEphemeral(Map)} will return
* true.
*
* @param workspaceAttributes workspace config or devfile attributes to which ephemeral mode
* configuration should be provisioned
*/
public void makeEphemeral(Map<String, String> workspaceAttributes) {
workspaceAttributes.put(PERSIST_VOLUMES_ATTRIBUTE, "false");
}

public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class PerWorkspacePVCStrategy extends CommonPVCStrategy {
private final String pvcAccessMode;
private final String pvcQuantity;
private final String pvcStorageClassName;
private final EphemeralWorkspaceAdapter ephemeralWorkspaceAdapter;

@Inject
public PerWorkspacePVCStrategy(
Expand Down Expand Up @@ -80,6 +81,7 @@ public PerWorkspacePVCStrategy(
this.pvcAccessMode = pvcAccessMode;
this.pvcQuantity = pvcQuantity;
this.pvcStorageClassName = pvcStorageClassName;
this.ephemeralWorkspaceAdapter = ephemeralWorkspaceAdapter;
}

@Override
Expand All @@ -94,7 +96,7 @@ protected PersistentVolumeClaim createCommonPVC(String workspaceId) {

@Override
public void cleanup(Workspace workspace) throws InfrastructureException {
if (EphemeralWorkspaceUtility.isEphemeral(workspace)) {
if (ephemeralWorkspaceAdapter.isEphemeral(workspace)) {
return;
}
final String workspaceId = workspace.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public UniqueWorkspacePVCStrategy(
public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity)
throws InfrastructureException {
final String workspaceId = identity.getWorkspaceId();
if (EphemeralWorkspaceUtility.isEphemeral(k8sEnv.getAttributes())) {
if (ephemeralWorkspaceAdapter.isEphemeral(k8sEnv.getAttributes())) {
ephemeralWorkspaceAdapter.provision(k8sEnv, identity);
return;
}
Expand Down Expand Up @@ -133,7 +133,7 @@ public void prepare(KubernetesEnvironment k8sEnv, RuntimeIdentity identity, long

TracingTags.WORKSPACE_ID.set(workspaceId);

if (EphemeralWorkspaceUtility.isEphemeral(k8sEnv.getAttributes())) {
if (ephemeralWorkspaceAdapter.isEphemeral(k8sEnv.getAttributes())) {
return;
}

Expand All @@ -158,7 +158,7 @@ public void prepare(KubernetesEnvironment k8sEnv, RuntimeIdentity identity, long

@Override
public void cleanup(Workspace workspace) throws InfrastructureException {
if (EphemeralWorkspaceUtility.isEphemeral(workspace)) {
if (ephemeralWorkspaceAdapter.isEphemeral(workspace)) {
return;
}
factory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespaceFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc.EphemeralWorkspaceUtility;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc.EphemeralWorkspaceAdapter;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc.WorkspaceVolumesStrategy;
import org.eclipse.che.workspace.infrastructure.kubernetes.util.RuntimeEventsPublisher;
import org.eclipse.che.workspace.infrastructure.kubernetes.util.UnrecoverablePodEventListenerFactory;
Expand Down Expand Up @@ -62,6 +62,7 @@ public class PluginBrokerManager<E extends KubernetesEnvironment> {
private final KubernetesEnvironmentProvisioner<E> environmentProvisioner;
private final UnrecoverablePodEventListenerFactory unrecoverablePodEventListenerFactory;
private final RuntimeEventsPublisher runtimeEventsPublisher;
private final EphemeralWorkspaceAdapter ephemeralWorkspaceAdapter;
private final Tracer tracer;

@Inject
Expand All @@ -75,6 +76,7 @@ public PluginBrokerManager(
UnrecoverablePodEventListenerFactory unrecoverablePodEventListenerFactory,
@Named("che.workspace.plugin_broker.wait_timeout_min") int pluginBrokerWaitingTimeout,
RuntimeEventsPublisher runtimeEventsPublisher,
EphemeralWorkspaceAdapter ephemeralWorkspaceAdapter,
Tracer tracer) {
this.factory = factory;
this.eventService = eventService;
Expand All @@ -85,6 +87,7 @@ public PluginBrokerManager(
this.pluginBrokerWaitingTimeout = pluginBrokerWaitingTimeout;
this.unrecoverablePodEventListenerFactory = unrecoverablePodEventListenerFactory;
this.runtimeEventsPublisher = runtimeEventsPublisher;
this.ephemeralWorkspaceAdapter = ephemeralWorkspaceAdapter;
this.tracer = tracer;
}

Expand All @@ -109,7 +112,7 @@ public List<ChePlugin> getTooling(

E brokerEnvironment = brokerEnvironmentFactory.createForMetadataBroker(pluginFQNs, identity);
if (isEphemeral) {
EphemeralWorkspaceUtility.makeEphemeral(brokerEnvironment.getAttributes());
ephemeralWorkspaceAdapter.makeEphemeral(brokerEnvironment.getAttributes());
}
environmentProvisioner.provision(brokerEnvironment, identity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.eclipse.che.commons.annotation.Traced;
import org.eclipse.che.workspace.infrastructure.kubernetes.StartSynchronizer;
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc.EphemeralWorkspaceUtility;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc.EphemeralWorkspaceAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -44,17 +44,20 @@ public class SidecarToolingProvisioner<E extends KubernetesEnvironment> {
private final KubernetesArtifactsBrokerApplier<E> artifactsBrokerApplier;
private final PluginFQNParser pluginFQNParser;
private final PluginBrokerManager<E> pluginBrokerManager;
private final EphemeralWorkspaceAdapter ephemeralWorkspaceAdapter;

@Inject
public SidecarToolingProvisioner(
Map<String, ChePluginsApplier> workspaceNextAppliers,
KubernetesArtifactsBrokerApplier<E> artifactsBrokerApplier,
PluginFQNParser pluginFQNParser,
PluginBrokerManager<E> pluginBrokerManager) {
PluginBrokerManager<E> pluginBrokerManager,
EphemeralWorkspaceAdapter ephemeralWorkspaceAdapter) {
this.workspaceNextAppliers = ImmutableMap.copyOf(workspaceNextAppliers);
this.artifactsBrokerApplier = artifactsBrokerApplier;
this.pluginFQNParser = pluginFQNParser;
this.pluginBrokerManager = pluginBrokerManager;
this.ephemeralWorkspaceAdapter = ephemeralWorkspaceAdapter;
}

@Traced
Expand All @@ -75,7 +78,7 @@ public void provision(
"Sidecar tooling configuration is not supported with environment type " + recipeType);
}

boolean isEphemeral = EphemeralWorkspaceUtility.isEphemeral(environment.getAttributes());
boolean isEphemeral = ephemeralWorkspaceAdapter.isEphemeral(environment.getAttributes());
List<ChePlugin> chePlugins =
pluginBrokerManager.getTooling(identity, startSynchronizer, pluginFQNs, isEphemeral);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.eclipse.che.api.workspace.server.wsplugins.PluginFQNParser;
import org.eclipse.che.api.workspace.server.wsplugins.model.PluginFQN;
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc.EphemeralWorkspaceUtility;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc.EphemeralWorkspaceAdapter;
import org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.KubernetesArtifactsBrokerApplier;
import org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.PluginBrokerManager;
import org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.SidecarToolingProvisioner;
Expand Down Expand Up @@ -56,6 +56,7 @@ public class SidecarToolingProvisionerTest {
@Mock private KubernetesEnvironment ephemeralEnvironment;
@Mock private RuntimeIdentity runtimeId;
@Mock private ChePluginsApplier chePluginsApplier;
@Mock private EphemeralWorkspaceAdapter ephemeralWorkspaceAdapter;

private static Map<String, String> environmentAttributesBase =
ImmutableMap.of(
Expand All @@ -71,7 +72,6 @@ public void setUp() throws Exception {
Map<String, ChePluginsApplier> workspaceNextAppliers =
ImmutableMap.of(RECIPE_TYPE, chePluginsApplier);
Map<String, String> ephemeralEnvironmentAttributes = new HashMap<>(environmentAttributesBase);
EphemeralWorkspaceUtility.makeEphemeral(ephemeralEnvironmentAttributes);
Map<String, String> nonEphemeralEnvironmentAttributes =
new HashMap<>(environmentAttributesBase);

Expand All @@ -86,7 +86,11 @@ public void setUp() throws Exception {

provisioner =
new SidecarToolingProvisioner<>(
workspaceNextAppliers, artifactsBrokerApplier, pluginFQNParser, brokerManager);
workspaceNextAppliers,
artifactsBrokerApplier,
pluginFQNParser,
brokerManager,
ephemeralWorkspaceAdapter);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public class EphemeralWorkspaceAdapterTest {

@BeforeMethod
public void setup() throws Exception {
ephemeralWorkspaceAdapter = new EphemeralWorkspaceAdapter(pvcProvisioner, subPathPrefixes);
ephemeralWorkspaceAdapter =
new EphemeralWorkspaceAdapter(pvcProvisioner, subPathPrefixes, true);

// ephemeral workspace configuration
lenient().when(ephemeralWorkspace.getId()).thenReturn(EPHEMERAL_WORKSPACE_ID);
Expand All @@ -93,8 +94,20 @@ public void setup() throws Exception {

@Test
public void testIsEphemeralWorkspace() throws Exception {
assertTrue(EphemeralWorkspaceUtility.isEphemeral(ephemeralWorkspace));
assertFalse(EphemeralWorkspaceUtility.isEphemeral(nonEphemeralWorkspace));
ephemeralWorkspaceAdapter =
new EphemeralWorkspaceAdapter(pvcProvisioner, subPathPrefixes, true);

// a little tricky part since attribute indicates whether volumes are persisted while
// method indicates whether workspace is ephemeral
assertFalse(
ephemeralWorkspaceAdapter.isEphemeral(ImmutableMap.of(PERSIST_VOLUMES_ATTRIBUTE, "true")));
assertTrue(
ephemeralWorkspaceAdapter.isEphemeral(ImmutableMap.of(PERSIST_VOLUMES_ATTRIBUTE, "false")));
// any string value except true is propagated as false
assertTrue(
ephemeralWorkspaceAdapter.isEphemeral(ImmutableMap.of(PERSIST_VOLUMES_ATTRIBUTE, "any")));

assertFalse(ephemeralWorkspaceAdapter.isEphemeral(Collections.emptyMap()));
}

@Test
Expand Down
Loading

0 comments on commit 956d17c

Please sign in to comment.