Added environment variables configuration to Machine model (#6802)

* CHE-6702 Add env variables to machine config

* CHE-6702 Add setting of env variables into OpenShift machines

* CHE-6702 Add setting of env variables into Docker machines

* CHE-6702 Make env field known for stack validator
6.19.x
Sergii Leshchenko 2017-10-18 18:11:54 +03:00 committed by GitHub
parent 49f9b26280
commit bac6efc5e2
14 changed files with 132 additions and 6 deletions

View File

@ -31,6 +31,9 @@ public interface MachineConfig {
/** Returns mapping of references to configurations of servers deployed into machine. */
Map<String, ? extends ServerConfig> getServers();
/** Returns environment variables of machine. */
Map<String, String> getEnv();
/** Returns attributes of resources of machine. */
Map<String, String> getAttributes();
}

View File

@ -198,7 +198,7 @@ export class StackValidationService {
*/
getMachineValidation(machine: che.IEnvironmentMachine): che.IValidation {
let mandatoryKeys: Array<string> = ['attributes'];
let additionalKeys: Array<string> = ['installers', 'servers', 'source'];
let additionalKeys: Array<string> = ['installers', 'servers', 'source', 'env'];
let validKeys: Array<string> = mandatoryKeys.concat(additionalKeys);
let errors: Array<string> = [];
let isValid: boolean = true;

View File

@ -21,12 +21,14 @@ import org.eclipse.che.api.core.model.workspace.config.ServerConfig;
public class MachineConfigImpl implements MachineConfig {
private List<String> installers;
private Map<String, String> env;
private Map<String, String> attributes;
private Map<String, ServerConfigImpl> servers;
public MachineConfigImpl(
List<String> installers,
Map<String, ? extends ServerConfig> servers,
Map<String, String> env,
Map<String, String> attributes) {
if (installers != null) {
this.installers = new ArrayList<>(installers);
@ -40,13 +42,16 @@ public class MachineConfigImpl implements MachineConfig {
Collectors.toMap(
Map.Entry::getKey, entry -> new ServerConfigImpl(entry.getValue())));
}
if (env != null) {
this.env = new HashMap<>(env);
}
if (attributes != null) {
this.attributes = new HashMap<>(attributes);
}
}
public MachineConfigImpl(MachineConfig machine) {
this(machine.getInstallers(), machine.getServers(), machine.getAttributes());
this(machine.getInstallers(), machine.getServers(), machine.getEnv(), machine.getAttributes());
}
@Override
@ -65,6 +70,14 @@ public class MachineConfigImpl implements MachineConfig {
return servers;
}
@Override
public Map<String, String> getEnv() {
if (env == null) {
env = new HashMap<>();
}
return env;
}
@Override
public Map<String, String> getAttributes() {
if (attributes == null) {
@ -83,6 +96,7 @@ public class MachineConfigImpl implements MachineConfig {
}
final MachineConfigImpl that = (MachineConfigImpl) obj;
return getInstallers().equals(that.getInstallers())
&& getEnv().equals(that.getEnv())
&& getAttributes().equals(that.getAttributes())
&& getServers().equals(that.getServers());
}
@ -91,6 +105,7 @@ public class MachineConfigImpl implements MachineConfig {
public int hashCode() {
int hash = 7;
hash = 31 * hash + getInstallers().hashCode();
hash = 31 * hash + getEnv().hashCode();
hash = 31 * hash + getAttributes().hashCode();
hash = 31 * hash + getServers().hashCode();
return hash;
@ -101,6 +116,8 @@ public class MachineConfigImpl implements MachineConfig {
return "MachineConfigImpl{"
+ "installers="
+ installers
+ ", env="
+ env
+ ", attributes="
+ attributes
+ ", servers="

View File

@ -109,5 +109,7 @@ public class EnvironmentParser {
container.getExpose().add(normalizedPort);
}
container.setExpose(container.getExpose().stream().distinct().collect(Collectors.toList()));
container.getEnvironment().putAll(machineConfig.getEnv());
}
}

View File

@ -14,6 +14,7 @@ import static java.lang.String.format;
import static org.eclipse.che.workspace.infrastructure.openshift.Constants.CHE_POD_NAME_LABEL;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.ObjectMeta;
@ -26,7 +27,9 @@ import io.fabric8.openshift.api.model.Route;
import io.fabric8.openshift.client.OpenShiftClient;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.inject.Inject;
import org.eclipse.che.api.core.ValidationException;
import org.eclipse.che.api.workspace.server.model.impl.WarningImpl;
@ -163,6 +166,10 @@ public class OpenShiftEnvironmentParser {
ServerExposer serverExposer =
new ServerExposer(machineName, containerConfig, openShiftEnvironment);
serverExposer.expose(machineConfig.getServers());
for (Entry<String, String> envEntry : machineConfig.getEnv().entrySet()) {
putEnv(containerConfig.getEnv(), envEntry.getKey(), envEntry.getValue());
}
}
}
}
@ -201,4 +208,9 @@ public class OpenShiftEnvironmentParser {
throw new ValidationException(errorMessage);
}
}
private void putEnv(List<EnvVar> envs, String key, String value) {
envs.removeIf(env -> key.equals(env.getName()));
envs.add(new EnvVar(key, value, null));
}
}

View File

@ -331,6 +331,7 @@ public class FactoryDaoTest {
exMachine1.setServers(ImmutableMap.of("ref1", serverConf1, "ref2", serverConf2));
exMachine1.setInstallers(ImmutableList.of("agent5", "agent4"));
exMachine1.setAttributes(singletonMap("att1", "val"));
exMachine1.setEnv(singletonMap("CHE_ENV", "value"));
final MachineConfigImpl exMachine2 = new MachineConfigImpl();
final ServerConfigImpl serverConf3 = new ServerConfigImpl("2333", "https", "/path1");
@ -338,12 +339,14 @@ public class FactoryDaoTest {
exMachine2.setServers(ImmutableMap.of("ref1", serverConf3, "ref2", serverConf4));
exMachine2.setInstallers(ImmutableList.of("agent2", "agent1"));
exMachine2.setAttributes(singletonMap("att1", "val"));
exMachine2.setEnv(singletonMap("CHE_ENV2", "value"));
final MachineConfigImpl exMachine3 = new MachineConfigImpl();
final ServerConfigImpl serverConf5 = new ServerConfigImpl("2333", "https", "/path3");
exMachine3.setServers(singletonMap("ref1", serverConf5));
exMachine3.setInstallers(ImmutableList.of("agent6", "agent2"));
exMachine3.setAttributes(singletonMap("att1", "val"));
exMachine3.setEnv(singletonMap("CHE_ENV3", "value"));
// Environments
final RecipeImpl recipe1 = new RecipeImpl();

View File

@ -37,6 +37,14 @@ public interface MachineConfigDto extends MachineConfig {
MachineConfigDto withServers(Map<String, ServerConfigDto> servers);
@Override
@FactoryParameter(obligation = OPTIONAL)
Map<String, String> getEnv();
void setEnv(Map<String, String> env);
MachineConfigDto withEnv(Map<String, String> env);
@Override
@FactoryParameter(obligation = OPTIONAL)
Map<String, String> getAttributes();

View File

@ -58,6 +58,15 @@ public class MachineConfigImpl implements MachineConfig {
@Column(name = "attributes")
private Map<String, String> attributes;
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(
name = "externalmachine_env",
joinColumns = @JoinColumn(name = "externalmachine_id")
)
@MapKeyColumn(name = "env_key")
@Column(name = "env_value")
private Map<String, String> env;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = "servers_id")
@MapKeyColumn(name = "servers_key")
@ -68,6 +77,7 @@ public class MachineConfigImpl implements MachineConfig {
public MachineConfigImpl(
List<String> installers,
Map<String, ? extends ServerConfig> servers,
Map<String, String> env,
Map<String, String> attributes) {
if (installers != null) {
this.installers = new ArrayList<>(installers);
@ -81,13 +91,16 @@ public class MachineConfigImpl implements MachineConfig {
Collectors.toMap(
Map.Entry::getKey, entry -> new ServerConfigImpl(entry.getValue())));
}
if (env != null) {
this.env = new HashMap<>(env);
}
if (attributes != null) {
this.attributes = new HashMap<>(attributes);
}
}
public MachineConfigImpl(MachineConfig machine) {
this(machine.getInstallers(), machine.getServers(), machine.getAttributes());
this(machine.getInstallers(), machine.getServers(), machine.getEnv(), machine.getAttributes());
}
@Override
@ -124,6 +137,23 @@ public class MachineConfigImpl implements MachineConfig {
return this;
}
@Override
public Map<String, String> getEnv() {
if (env == null) {
env = new HashMap<>();
}
return env;
}
public void setEnv(Map<String, String> env) {
this.env = env;
}
public MachineConfigImpl withEnv(Map<String, String> env) {
this.env = env;
return this;
}
@Override
public Map<String, String> getAttributes() {
if (attributes == null) {
@ -152,6 +182,7 @@ public class MachineConfigImpl implements MachineConfig {
final MachineConfigImpl that = (MachineConfigImpl) obj;
return Objects.equals(id, that.id)
&& getInstallers().equals(that.getInstallers())
&& getEnv().equals(that.getEnv())
&& getAttributes().equals(that.getAttributes())
&& getServers().equals(that.getServers());
}
@ -161,6 +192,7 @@ public class MachineConfigImpl implements MachineConfig {
int hash = 7;
hash = 31 * hash + Objects.hashCode(id);
hash = 31 * hash + getInstallers().hashCode();
hash = 31 * hash + getEnv().hashCode();
hash = 31 * hash + getAttributes().hashCode();
hash = 31 * hash + getServers().hashCode();
return hash;
@ -173,6 +205,8 @@ public class MachineConfigImpl implements MachineConfig {
+ id
+ ", installers="
+ installers
+ ", env="
+ env
+ ", attributes="
+ attributes
+ ", servers="

View File

@ -35,14 +35,27 @@ public class InternalMachineConfig {
private final List<InstallerImpl> installers;
// set of servers including ones configured by installers
private final Map<String, ServerConfig> servers;
private final Map<String, String> env;
private final Map<String, String> attributes;
InternalMachineConfig(MachineConfig originalConfig, InstallerRegistry installerRegistry)
throws InfrastructureException {
this.installers = new ArrayList<>();
this.servers = new HashMap<>(originalConfig.getServers());
this.attributes = new HashMap<>(originalConfig.getAttributes());
this.servers = new HashMap<>();
if (originalConfig.getServers() != null) {
this.servers.putAll(originalConfig.getServers());
}
this.env = new HashMap<>();
if (originalConfig.getEnv() != null) {
this.env.putAll(originalConfig.getEnv());
}
this.attributes = new HashMap<>();
if (originalConfig.getAttributes() != null) {
this.attributes.putAll(originalConfig.getAttributes());
}
this.installers = new ArrayList<>();
initInstallers(originalConfig.getInstallers(), installerRegistry);
}
@ -60,6 +73,11 @@ public class InternalMachineConfig {
return Collections.unmodifiableList(installers);
}
/** Returns unmodifiable map of machine environment variables. */
public Map<String, String> getEnv() {
return Collections.unmodifiableMap(env);
}
/** Returns unmodifiable map of machine attributes. */
public Map<String, String> getAttributes() {
return Collections.unmodifiableMap(attributes);

View File

@ -556,6 +556,7 @@ public class WorkspaceManagerTest {
new MachineConfigImpl(
singletonList("org.eclipse.che.ws-agent"),
null,
singletonMap("CHE_ENV", "value"),
singletonMap("memoryLimitBytes", "10000"));
EnvironmentImpl environment =
new EnvironmentImpl(

View File

@ -916,6 +916,7 @@ public class WorkspaceServiceTest {
new MachineConfigImpl(
singletonList("org.eclipse.che.ws-agent"),
null,
singletonMap("CHE_ENV", "value"),
singletonMap("memoryLimitBytes", "10000"));
return DtoConverter.asDto(

View File

@ -385,6 +385,7 @@ public class WorkspaceDaoTest {
newMachine.setServers(ImmutableMap.of("ref1", serverConf1, "ref2", serverConf2));
newMachine.setInstallers(ImmutableList.of("agent5", "agent4"));
newMachine.setAttributes(singletonMap("att1", "val"));
newMachine.setAttributes(singletonMap("CHE_ENV", "value"));
final EnvironmentImpl newEnv = new EnvironmentImpl();
newEnv.setMachines(ImmutableMap.of("new-machine", newMachine));
newEnv.setRecipe(newRecipe);
@ -552,6 +553,7 @@ public class WorkspaceDaoTest {
exMachine1.setServers(ImmutableMap.of("ref1", serverConf1, "ref2", serverConf2));
exMachine1.setInstallers(ImmutableList.of("agent5", "agent4"));
exMachine1.setAttributes(singletonMap("att1", "val"));
exMachine1.setEnv(ImmutableMap.of("CHE_ENV1", "value", "CHE_ENV2", "value"));
final MachineConfigImpl exMachine2 = new MachineConfigImpl();
final ServerConfigImpl serverConf3 = new ServerConfigImpl("2333", "https", "path3");
@ -559,12 +561,14 @@ public class WorkspaceDaoTest {
exMachine2.setServers(ImmutableMap.of("ref1", serverConf3, "ref2", serverConf4));
exMachine2.setInstallers(ImmutableList.of("agent2", "agent1"));
exMachine2.setAttributes(singletonMap("att1", "val"));
exMachine2.setEnv(singletonMap("CHE_ENV2", "value"));
final MachineConfigImpl exMachine3 = new MachineConfigImpl();
final ServerConfigImpl serverConf5 = new ServerConfigImpl("2333", "https", "path5");
exMachine3.setServers(singletonMap("ref1", serverConf5));
exMachine3.setInstallers(ImmutableList.of("agent6", "agent2"));
exMachine3.setAttributes(singletonMap("att1", "val"));
exMachine3.setEnv(singletonMap("CHE_ENV3", "value"));
// Environments
final RecipeImpl recipe1 = new RecipeImpl();

View File

@ -0,0 +1,22 @@
--
-- Copyright (c) 2012-2017 Red Hat, Inc.
-- All rights reserved. This program and the accompanying materials
-- are made available under the terms of the Eclipse Public License v1.0
-- which accompanies this distribution, and is available at
-- http://www.eclipse.org/legal/epl-v10.html
--
-- Contributors:
-- Red Hat, Inc. - initial API and implementation
--
-- Machine env ----------------------------------------------
CREATE TABLE externalmachine_env (
externalmachine_id BIGINT,
env_value VARCHAR(255),
env_key VARCHAR(255)
);
--constraints
ALTER TABLE externalmachine_env ADD CONSTRAINT fk_externalmachine_env_externalmachine_id FOREIGN KEY (externalmachine_id) REFERENCES externalmachine (id);
--indexes
CREATE INDEX index_externalmachine_env_externalmachine_id ON externalmachine_env (externalmachine_id);
--------------------------------------------------------------------------------

View File

@ -118,6 +118,7 @@ public final class TestObjectsFactory {
newMachine.setServers(ImmutableMap.of("ref1", serverConf1, "ref2", serverConf2));
newMachine.setInstallers(ImmutableList.of("agent5", "agent4"));
newMachine.setAttributes(singletonMap("att1", "val"));
newMachine.setEnv(singletonMap("CHE_ENV", "value"));
final EnvironmentImpl newEnv = new EnvironmentImpl();
newEnv.setMachines(ImmutableMap.of("new-machine", newMachine));