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

Support ENV variables in cheEditor/chePlugin components of the devfile #15401

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(","))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks on , contained in the envvar value, right? IMHO that could render some JAVA_OPTS invalid, because e.g. settings for -agentlib or -javaagent are usually comma separated.

Copy link
Contributor Author

@mshaposhnik mshaposhnik Dec 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is the problem. Also another example is LDAP namespaces like myDN=OU=MY_OU,DC=MYDC,DC=local

// 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
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
"type": {},
"alias": {},
"id": {},
"env": {},
"reference": {},
"registryUrl": {},
"memoryLimit": {}
Expand All @@ -293,6 +294,7 @@
"type": {},
"alias": {},
"id": {},
"env": {},
"memoryLimit": {},
"reference": {},
"registryUrl": {},
Expand Down Expand Up @@ -344,7 +346,7 @@
"type": {},
"alias": {},
"mountSources": {},
"env": {},
"env": {},
"reference": {
"description": "Describes absolute or devfile-relative location of Kubernetes list yaml file. Applicable only for 'kubernetes' and 'openshift' type components",
"type": "string",
Expand Down Expand Up @@ -431,7 +433,7 @@
"type": {},
"alias": {},
"mountSources": {},
"env": {},
"env": {},
"image": {
"type": "string",
"description": "Specifies the docker image that should be used for component",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ projects:
components:
- type: cheEditor
id: eclipse/chetheia/0.0.3
env:
- name: ENV_VAR
value: value
- alias: mvn-stack
type: chePlugin
id: eclipse/chemaven-jdk8/1.0.0
- type: chePlugin
id: eclipse/chetheia-jdtls/0.0.3
env:
- name: ENV_VAR
value: value
commands:
- name: build
actions:
Expand Down