Skip to content

Commit

Permalink
Support ENV variables for cheEditor-chePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mshaposhnik authored and sleshchenko committed Dec 11, 2019
1 parent 372393f commit f21f02a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.eclipse.che.api.workspace.server.devfile.Constants.EDITOR_COMPONENT_ALIAS_WORKSPACE_ATTRIBUTE;
import static org.eclipse.che.api.workspace.server.devfile.Constants.PLUGINS_COMPONENTS_ALIASES_WORKSPACE_ATTRIBUTE;
import static org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME;
import static org.eclipse.che.api.workspace.shared.Constants.SIDECAR_ENV_VARIABLES_ATTR_TEMPLATE;
import static org.eclipse.che.api.workspace.shared.Constants.SIDECAR_MEMORY_LIMIT_ATTR_TEMPLATE;

import io.fabric8.kubernetes.api.model.Container;
Expand All @@ -30,6 +31,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Stream;
import org.eclipse.che.api.core.model.workspace.config.ServerConfig;
import org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl;
import org.eclipse.che.api.workspace.server.model.impl.VolumeImpl;
Expand Down Expand Up @@ -79,14 +81,26 @@ public InternalMachineConfig resolve() throws InfrastructureException {
new InternalMachineConfig(
null,
toServers(containerEndpoints),
null,
toEnvVariables(wsAttributes),
toMachineAttributes(pluginId, wsAttributes),
toWorkspaceVolumes(cheContainer));

normalizeMemory(container, machineConfig);
return machineConfig;
}

private Map<String,String> toEnvVariables(Map<String,String> wsAttributes) {
String envVars = wsAttributes
.get(format(SIDECAR_ENV_VARIABLES_ATTR_TEMPLATE, pluginPublisherAndName));
if (isNullOrEmpty(envVars)) {
return null;
}
return Stream.of(envVars.split(","))
// only splitting by 1'st '=' since env value may also contain it
.map(value -> value.split("=", 2))
.collect(toMap(arr -> arr[0], arr -> arr[1]));
}

private Map<String, String> toMachineAttributes(
String pluginId, Map<String, String> wsAttributes) {
Map<String, String> attributes = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public final class Constants {
*/
public static final String SIDECAR_MEMORY_LIMIT_ATTR_TEMPLATE = "sidecar.%s.memory_limit";

public static final String SIDECAR_ENV_VARIABLES_ATTR_TEMPLATE = "sidecar.%s.environment_variables";

/**
* Describes workspace runtimes which perform start/stop of this workspace. Should be set/read
* from {@link Workspace#getAttributes}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.eclipse.che.api.core.model.workspace.config.Command.PLUGIN_ATTRIBUTE;
import static org.eclipse.che.api.workspace.server.devfile.Constants.COMPONENT_ALIAS_COMMAND_ATTRIBUTE;
import static org.eclipse.che.api.workspace.server.devfile.Constants.EDITOR_COMPONENT_ALIAS_WORKSPACE_ATTRIBUTE;
import static org.eclipse.che.api.workspace.server.devfile.Constants.EDITOR_COMPONENT_TYPE;
import static org.eclipse.che.api.workspace.shared.Constants.SIDECAR_ENV_VARIABLES_ATTR_TEMPLATE;
import static org.eclipse.che.api.workspace.shared.Constants.SIDECAR_MEMORY_LIMIT_ATTR_TEMPLATE;
import static org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_EDITOR_ATTRIBUTE;

import java.util.List;
import javax.inject.Inject;
import org.eclipse.che.api.core.model.workspace.devfile.Component;
import org.eclipse.che.api.core.model.workspace.devfile.Env;
import org.eclipse.che.api.workspace.server.devfile.FileContentProvider;
import org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentFQNParser;
import org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier;
Expand Down Expand Up @@ -70,6 +74,7 @@ public void apply(
final String editorId = editorComponent.getId();
final String registryUrl = editorComponent.getRegistryUrl();
final String memoryLimit = editorComponent.getMemoryLimit();
final List<? extends Env> env = editorComponent.getEnv();

final ExtendedPluginFQN fqn = componentFQNParser.evaluateFQN(editorComponent, contentProvider);
if (editorComponentAlias != null) {
Expand Down Expand Up @@ -106,5 +111,14 @@ public void apply(
&& editorComponentAlias.equals(
c.getAttributes().get(COMPONENT_ALIAS_COMMAND_ATTRIBUTE)))
.forEach(c -> c.getAttributes().put(PLUGIN_ATTRIBUTE, fqn.getId()));

if (!env.isEmpty()) {
workspaceConfig.getAttributes()
.put(format(SIDECAR_ENV_VARIABLES_ATTR_TEMPLATE, fqn.getPublisherAndName()),
String.join(",", env.stream().map(
(java.util.function.Function<Env, String>) e -> e.getName() + "=" + e.getValue())
.collect(toList())));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.eclipse.che.api.core.model.workspace.config.Command.PLUGIN_ATTRIBUTE;
import static org.eclipse.che.api.workspace.server.devfile.Constants.COMPONENT_ALIAS_COMMAND_ATTRIBUTE;
import static org.eclipse.che.api.workspace.server.devfile.Constants.PLUGINS_COMPONENTS_ALIASES_WORKSPACE_ATTRIBUTE;
import static org.eclipse.che.api.workspace.server.devfile.Constants.PLUGIN_COMPONENT_TYPE;
import static org.eclipse.che.api.workspace.shared.Constants.SIDECAR_ENV_VARIABLES_ATTR_TEMPLATE;
import static org.eclipse.che.api.workspace.shared.Constants.SIDECAR_MEMORY_LIMIT_ATTR_TEMPLATE;
import static org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_TOOLING_PLUGINS_ATTRIBUTE;

import java.util.List;
import javax.inject.Inject;
import org.eclipse.che.api.core.model.workspace.devfile.Component;
import org.eclipse.che.api.core.model.workspace.devfile.Env;
import org.eclipse.che.api.workspace.server.devfile.FileContentProvider;
import org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentFQNParser;
import org.eclipse.che.api.workspace.server.devfile.convert.component.ComponentToWorkspaceApplier;
Expand Down Expand Up @@ -127,6 +131,15 @@ public void apply(
+ "="
+ pluginComponent.getAlias()));
}

final List<? extends Env> env = pluginComponent.getEnv();
if (!env.isEmpty()) {
workspaceConfig.getAttributes()
.put(format(SIDECAR_ENV_VARIABLES_ATTR_TEMPLATE, fqn.getPublisherAndName()),
String.join(",", env.stream().map(
(java.util.function.Function<Env, String>) e -> e.getName() + "=" + e.getValue())
.collect(toList())));
}
}

private String append(String source, String toAppend) {
Expand Down

0 comments on commit f21f02a

Please sign in to comment.