Skip to content

Commit 91d55a4

Browse files
author
Oleksandr Garagatyi
authored
CHE-5901: Rework SPI to simplify an infrastructure implementation (eclipse-che#6047)
Rework SPI to simplify an infrastructure implementation. Move installers evaluation, environment recipe content downloading to SPI level from infra implementation level. Treat a machine with wsagent server dev machine instead of checking wsagent installer only. Improve code coverage. Fix downloading of an environment recipe content from a Github gist. Signed-off-by: Oleksandr Garagatyi <ogaragat@redhat.com>
1 parent 2d0ce24 commit 91d55a4

76 files changed

Lines changed: 1946 additions & 2403 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

infrastructures/docker/src/main/java/org/eclipse/che/workspace/infrastructure/docker/DockerInternalRuntime.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.eclipse.che.api.core.notification.EventService;
3434
import org.eclipse.che.api.workspace.server.DtoConverter;
3535
import org.eclipse.che.api.workspace.server.URLRewriter;
36+
import org.eclipse.che.api.workspace.server.WsAgentMachineFinderUtil;
3637
import org.eclipse.che.api.workspace.server.hc.ServerCheckerFactory;
3738
import org.eclipse.che.api.workspace.server.hc.ServersReadinessChecker;
3839
import org.eclipse.che.api.workspace.server.model.impl.MachineImpl;
@@ -294,7 +295,7 @@ void checkServers() throws InfrastructureException {
294295

295296
private void startMachine(String name, DockerContainerConfig containerConfig)
296297
throws InfrastructureException, InterruptedException {
297-
InternalMachineConfig machineCfg = getContext().getMachineConfigs().get(name);
298+
InternalMachineConfig machineCfg = getContext().getEnvironment().getMachines().get(name);
298299

299300
DockerMachine machine =
300301
containerStarter.startContainer(
@@ -441,18 +442,24 @@ machineName, getContext().getIdentity().getWorkspaceId(), machine.getContainer()
441442
*
442443
* @param machines the active machines map
443444
*/
444-
private void snapshotMachines(Map<String, DockerMachine> machines) {
445+
private void snapshotMachines(Map<String, DockerMachine> machines)
446+
throws InternalInfrastructureException {
445447
List<SnapshotImpl> newSnapshots = new ArrayList<>();
446448
final RuntimeIdentity identity = getContext().getIdentity();
447-
String devMachineName = getContext().getDevMachineName();
449+
// TODO do we need dev machine flag at all?
450+
String devMachineName =
451+
WsAgentMachineFinderUtil.getWsAgentServerMachine(getContext().getEnvironment())
452+
.orElseThrow(
453+
() -> new InternalInfrastructureException("Machine with wsagent is not found"));
454+
448455
for (Map.Entry<String, DockerMachine> dockerMachineEntry : machines.entrySet()) {
449456
SnapshotImpl snapshot =
450457
SnapshotImpl.builder()
451458
.generateId()
452459
.setType("docker") //TODO: do we need that at all?
453460
.setWorkspaceId(identity.getWorkspaceId())
454461
.setDescription(identity.getEnvName())
455-
.setDev(dockerMachineEntry.getKey().equals(devMachineName))
462+
.setDev(devMachineName.equals(dockerMachineEntry.getKey()))
456463
.setEnvName(identity.getEnvName())
457464
.setMachineName(dockerMachineEntry.getKey())
458465
.useCurrentCreationDate()

infrastructures/docker/src/main/java/org/eclipse/che/workspace/infrastructure/docker/DockerRuntimeContext.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
import javax.ws.rs.core.UriBuilder;
2323
import javax.ws.rs.core.UriBuilderException;
2424
import org.eclipse.che.api.core.ValidationException;
25-
import org.eclipse.che.api.core.model.workspace.config.Environment;
2625
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
27-
import org.eclipse.che.api.installer.server.InstallerRegistry;
28-
import org.eclipse.che.api.workspace.server.Utils;
2926
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
27+
import org.eclipse.che.api.workspace.server.spi.InternalEnvironment;
3028
import org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException;
3129
import org.eclipse.che.api.workspace.server.spi.RuntimeContext;
3230
import org.eclipse.che.plugin.docker.client.json.ContainerListEntry;
@@ -47,7 +45,6 @@ public class DockerRuntimeContext extends RuntimeContext {
4745

4846
private final DockerEnvironment dockerEnvironment;
4947
private final List<String> orderedContainers;
50-
private final String devMachineName;
5148
private final String websocketEndpointBase;
5249
private final DockerRuntimeFactory runtimeFactory;
5350
private final DockerContainers containers;
@@ -58,19 +55,17 @@ public class DockerRuntimeContext extends RuntimeContext {
5855
public DockerRuntimeContext(
5956
@Assisted DockerRuntimeInfrastructure infrastructure,
6057
@Assisted RuntimeIdentity identity,
61-
@Assisted Environment environment,
58+
@Assisted InternalEnvironment environment,
6259
@Assisted DockerEnvironment dockerEnv,
6360
@Assisted List<String> containersOrder,
64-
InstallerRegistry installerRegistry,
6561
DockerRuntimeFactory runtimeFactory,
6662
DockerContainers containers,
6763
DockerSharedPool sharedPool,
6864
RuntimeConsistencyChecker consistencyChecker,
6965
@Named("che.websocket.endpoint.base") String websocketEndpointBase)
7066
throws InfrastructureException, ValidationException {
7167

72-
super(environment, identity, infrastructure, installerRegistry);
73-
this.devMachineName = Utils.getDevMachineName(environment);
68+
super(environment, identity, infrastructure);
7469
this.dockerEnvironment = dockerEnv;
7570
this.orderedContainers = ImmutableList.copyOf(containersOrder);
7671
this.websocketEndpointBase = websocketEndpointBase;
@@ -85,11 +80,6 @@ public DockerEnvironment getDockerEnvironment() {
8580
return dockerEnvironment;
8681
}
8782

88-
/** Returns the name of the dev machine. */
89-
public String getDevMachineName() {
90-
return devMachineName;
91-
}
92-
9383
/** Returns the list of the ordered containers, machines must be started in the same order. */
9484
public List<String> getOrderedContainers() {
9585
return orderedContainers;
@@ -107,14 +97,15 @@ public URI getOutputChannel() throws InfrastructureException {
10797

10898
@Override
10999
public DockerInternalRuntime getRuntime() throws InfrastructureException {
100+
RuntimeIdentity identity = getIdentity();
110101
List<ContainerListEntry> runningContainers = containers.find(identity);
111102
if (runningContainers.isEmpty()) {
112103
return runtimeFactory.create(this);
113104
}
114105

115106
DockerInternalRuntime runtime = runtimeFactory.create(this, runningContainers);
116107
try {
117-
consistencyChecker.check(environment, runtime);
108+
consistencyChecker.check(getEnvironment(), runtime);
118109
runtime.checkServers();
119110
} catch (InfrastructureException | ValidationException x) {
120111
LOG.warn(

infrastructures/docker/src/main/java/org/eclipse/che/workspace/infrastructure/docker/DockerRuntimeContextFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212

1313
import java.util.List;
1414
import org.eclipse.che.api.core.ValidationException;
15-
import org.eclipse.che.api.core.model.workspace.config.Environment;
1615
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
1716
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
17+
import org.eclipse.che.api.workspace.server.spi.InternalEnvironment;
1818
import org.eclipse.che.workspace.infrastructure.docker.model.DockerEnvironment;
1919

2020
/** Helps to create {@link DockerRuntimeContext} instances. */
2121
public interface DockerRuntimeContextFactory {
2222
DockerRuntimeContext create(
2323
DockerRuntimeInfrastructure infra,
2424
RuntimeIdentity identity,
25-
Environment environment,
25+
InternalEnvironment environment,
2626
DockerEnvironment dockerEnv,
2727
List<String> containersOrder)
2828
throws InfrastructureException, ValidationException;

infrastructures/docker/src/main/java/org/eclipse/che/workspace/infrastructure/docker/DockerRuntimeInfrastructure.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818
import org.eclipse.che.api.core.model.workspace.config.Environment;
1919
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
2020
import org.eclipse.che.api.core.notification.EventService;
21+
import org.eclipse.che.api.installer.server.InstallerRegistry;
22+
import org.eclipse.che.api.workspace.server.RecipeRetriever;
2123
import org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl;
2224
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
25+
import org.eclipse.che.api.workspace.server.spi.InternalEnvironment;
2326
import org.eclipse.che.api.workspace.server.spi.RuntimeInfrastructure;
2427
import org.eclipse.che.workspace.infrastructure.docker.container.ContainersStartStrategy;
2528
import org.eclipse.che.workspace.infrastructure.docker.container.DockerContainers;
2629
import org.eclipse.che.workspace.infrastructure.docker.environment.DockerConfigSourceSpecificEnvironmentParser;
2730
import org.eclipse.che.workspace.infrastructure.docker.environment.EnvironmentNormalizer;
2831
import org.eclipse.che.workspace.infrastructure.docker.environment.EnvironmentParser;
2932
import org.eclipse.che.workspace.infrastructure.docker.environment.EnvironmentValidator;
33+
import org.eclipse.che.workspace.infrastructure.docker.environment.dockerimage.DockerImageEnvironmentParser;
3034
import org.eclipse.che.workspace.infrastructure.docker.model.DockerEnvironment;
3135

3236
/**
@@ -52,10 +56,12 @@ public DockerRuntimeInfrastructure(
5256
InfrastructureProvisioner infrastructureProvisioner,
5357
EnvironmentNormalizer environmentNormalizer,
5458
Map<String, DockerConfigSourceSpecificEnvironmentParser> environmentParsers,
55-
EventService eventService,
5659
DockerRuntimeContextFactory contextFactory,
57-
DockerContainers containers) {
58-
super("docker", environmentParsers.keySet(), eventService);
60+
DockerContainers containers,
61+
EventService eventService,
62+
InstallerRegistry installerRegistry,
63+
RecipeRetriever recipeRetriever) {
64+
super("docker", environmentParsers.keySet(), eventService, installerRegistry, recipeRetriever);
5965
this.dockerEnvironmentValidator = dockerEnvironmentValidator;
6066
this.dockerEnvironmentParser = dockerEnvironmentParser;
6167
this.startStrategy = startStrategy;
@@ -66,26 +72,36 @@ public DockerRuntimeInfrastructure(
6672
}
6773

6874
@Override
69-
public Environment estimate(Environment environment)
75+
public InternalEnvironment estimate(Environment environment)
76+
throws ValidationException, InfrastructureException {
77+
// workaround that in dockerimage environment image is in location field instead of content
78+
if (DockerImageEnvironmentParser.TYPE.equals(environment.getRecipe().getType())
79+
&& environment.getRecipe().getLocation() != null) {
80+
// move image from location to content
81+
EnvironmentImpl envCopy = new EnvironmentImpl(environment);
82+
envCopy.getRecipe().setContent(environment.getRecipe().getLocation());
83+
envCopy.getRecipe().setLocation(null);
84+
return super.estimate(envCopy);
85+
}
86+
return super.estimate(environment);
87+
}
88+
89+
@Override
90+
public void internalEstimate(InternalEnvironment environment)
7091
throws ValidationException, InfrastructureException {
71-
// TODO spi: get recipe from non-impl specific code
7292
DockerEnvironment dockerEnvironment = dockerEnvironmentParser.parse(environment);
7393
dockerEnvironmentValidator.validate(environment, dockerEnvironment);
7494
// check that order can be resolved
7595
startStrategy.order(dockerEnvironment);
7696
// TODO add an actual estimation of what is missing in the environment
7797
// memory
7898
// machines
79-
80-
return environment;
8199
}
82100

83101
@Override
84-
public DockerRuntimeContext prepare(RuntimeIdentity identity, Environment originEnv)
102+
public DockerRuntimeContext prepare(RuntimeIdentity identity, InternalEnvironment environment)
85103
throws ValidationException, InfrastructureException {
86-
// TODO spi: get recipe from non-impl specific code
87-
// Copy to be able to change env and protect from env changes by method caller
88-
EnvironmentImpl environment = new EnvironmentImpl(originEnv);
104+
89105
DockerEnvironment dockerEnvironment = dockerEnvironmentParser.parse(environment);
90106
dockerEnvironmentValidator.validate(environment, dockerEnvironment);
91107

infrastructures/docker/src/main/java/org/eclipse/che/workspace/infrastructure/docker/InfrastructureProvisioner.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
package org.eclipse.che.workspace.infrastructure.docker;
1212

1313
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
14-
import org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl;
1514
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
15+
import org.eclipse.che.api.workspace.server.spi.InternalEnvironment;
1616
import org.eclipse.che.workspace.infrastructure.docker.model.DockerEnvironment;
1717

1818
/**
@@ -25,10 +25,11 @@ public interface InfrastructureProvisioner {
2525
* Modifies environment config and internal environment representation with everything needed for
2626
* infrastructure of workspace.
2727
*
28-
* @param envConfig configuration of environment
28+
* @param environment configuration of environment
2929
* @param internalEnv internal environment representation
3030
* @throws InfrastructureException if any error occurs
3131
*/
32-
void provision(EnvironmentImpl envConfig, DockerEnvironment internalEnv, RuntimeIdentity identity)
32+
void provision(
33+
InternalEnvironment environment, DockerEnvironment internalEnv, RuntimeIdentity identity)
3334
throws InfrastructureException;
3435
}

infrastructures/docker/src/main/java/org/eclipse/che/workspace/infrastructure/docker/Labels.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import java.util.regex.Pattern;
1919
import org.eclipse.che.api.core.model.workspace.config.ServerConfig;
2020
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
21+
import org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl;
2122
import org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl;
22-
import org.eclipse.che.api.workspace.server.spi.RuntimeIdentityImpl;
2323

2424
/**
2525
* Helps to convert docker infrastructure entities to docker labels and vise-versa.

infrastructures/docker/src/main/java/org/eclipse/che/workspace/infrastructure/docker/RuntimeConsistencyChecker.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
import java.util.Map;
1414
import javax.inject.Singleton;
1515
import org.eclipse.che.api.core.ValidationException;
16-
import org.eclipse.che.api.core.model.workspace.config.Environment;
17-
import org.eclipse.che.api.core.model.workspace.config.MachineConfig;
1816
import org.eclipse.che.api.core.model.workspace.runtime.Machine;
17+
import org.eclipse.che.api.workspace.server.spi.InternalEnvironment;
18+
import org.eclipse.che.api.workspace.server.spi.InternalMachineConfig;
1919

2020
/** Checks whether runtime is consistent with its configuration. */
2121
@Singleton
2222
class RuntimeConsistencyChecker {
23-
void check(Environment environment, DockerInternalRuntime runtime) throws ValidationException {
24-
Map<String, ? extends MachineConfig> configs = environment.getMachines();
23+
void check(InternalEnvironment environment, DockerInternalRuntime runtime)
24+
throws ValidationException {
25+
Map<String, InternalMachineConfig> configs = environment.getMachines();
2526
Map<String, ? extends Machine> machines = runtime.getMachines();
2627
if (configs.size() != machines.size()) {
2728
throw new ValidationException(

infrastructures/docker/src/main/java/org/eclipse/che/workspace/infrastructure/docker/environment/DockerConfigSourceSpecificEnvironmentParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.eclipse.che.api.core.model.workspace.config.Environment;
1515
import org.eclipse.che.api.core.model.workspace.config.Recipe;
1616
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
17+
import org.eclipse.che.api.workspace.server.spi.InternalEnvironment;
1718
import org.eclipse.che.workspace.infrastructure.docker.model.DockerEnvironment;
1819

1920
/**
@@ -33,6 +34,6 @@ public interface DockerConfigSourceSpecificEnvironmentParser {
3334
* @throws InfrastructureException when parsing fails due to some internal server error or
3435
* inability to parse environment due to other reasons
3536
*/
36-
DockerEnvironment parse(Environment environment)
37+
DockerEnvironment parse(InternalEnvironment environment)
3738
throws ValidationException, InfrastructureException;
3839
}

infrastructures/docker/src/main/java/org/eclipse/che/workspace/infrastructure/docker/environment/DockerEnvironmentTypeModule.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ protected void configure() {
2525
MapBinder.newMapBinder(
2626
binder(), String.class, DockerConfigSourceSpecificEnvironmentParser.class);
2727
envParserMapBinder.addBinding("compose").to(ComposeEnvironmentParser.class);
28-
envParserMapBinder.addBinding("dockerfile").to(DockerfileEnvironmentParser.class);
29-
envParserMapBinder.addBinding("dockerimage").to(DockerImageEnvironmentParser.class);
28+
envParserMapBinder
29+
.addBinding(DockerfileEnvironmentParser.TYPE)
30+
.to(DockerfileEnvironmentParser.class);
31+
envParserMapBinder
32+
.addBinding(DockerImageEnvironmentParser.TYPE)
33+
.to(DockerImageEnvironmentParser.class);
3034
}
3135
}

0 commit comments

Comments
 (0)