Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
fixup! fixup! fixup! fixup! fixup! rh-che #541: Login to user project…
Browse files Browse the repository at this point in the history
… using oc CLI in workspace containers
  • Loading branch information
Oleksandr Garagatyi committed Mar 1, 2018
1 parent 267b88d commit a7da2c9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.eclipse.che.inject.DynaModule;
import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftClientFactory;
import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftEnvironmentProvisioner;
import org.eclipse.che.workspace.infrastructure.openshift.project.OpenShiftProjectFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -29,5 +30,6 @@ protected void configure() {

bind(OpenShiftClientFactory.class).to(Fabric8OpenShiftClientFactory.class);
bind(OpenShiftProjectFactory.class).to(Fabric8OpenShiftProjectFactory.class);
bind(OpenShiftEnvironmentProvisioner.class).to(RhCheInfraEnvironmentProvisioner.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.api.workspace.server.spi.provision.env.EnvVarProvider;
import org.eclipse.che.commons.subject.Subject;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc.WorkspaceVolumesStrategy;
import org.eclipse.che.workspace.infrastructure.kubernetes.provision.InstallerServersPortProvisioner;
Expand All @@ -36,14 +37,16 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import static java.lang.String.format;
import static org.slf4j.LoggerFactory.getLogger;

/**
* Adds env vars needed to perform oc login in workspace containers.
*
* TODO remove it when injection of env vars won't hold workspace start request.
* TODO replace with {@link EnvVarProvider} when injection of env vars won't hold workspace start request.
*
* @author Alexander Garagatyi
*/
Expand Down Expand Up @@ -89,71 +92,73 @@ public void provision(OpenShiftEnvironment osEnv, RuntimeIdentity identity) thro

Map<String, String> envVars = new HashMap<>();

Subject subject;
try {
subject = subjectsRegistry.getSubject(identity.getOwnerId());
} catch (NotFoundException e) {
// we can't perform operations without subject, do nothing
LOG.error("Subject for user ID '" + identity.getOwnerId() + "' not found in subjects registry");
return;
getSubjectFuture(identity).thenApply(subject -> addTokenEnvVar(envVars, subject))
.thenApply(subject -> addTenantEnvVars(envVars, subject))
.thenAccept(aVoid -> injectEnvVars(osEnv, envVars))
.exceptionally(this::logError)
.get();
} catch (InterruptedException | ExecutionException e) {
// should not happen
throw new InfrastructureException(e.getMessage(), e);
}
}

private CompletableFuture<Subject> getSubjectFuture(RuntimeIdentity identity) {
return new CompletableFuture<>().thenApply(o -> {
try {
return subjectsRegistry.getSubject(identity.getOwnerId());
} catch (NotFoundException e) {
// we can't perform operations without subject
throw new RuntimeException(
"Subject for user ID '" + identity.getOwnerId() + "' not found in subjects registry");
}
});
}

String token = getOsoToken(subject);
// we can't perform oc login without OSO token, so do nothing
if (token == null) {
return;
private Subject addTokenEnvVar(Map<String, String> envVars, Subject subject) {
try {
String osoToken = openshiftUserTokenProvider.getToken(subject);
if (osoToken == null) {
throw new RuntimeException("OSO token not found for user " + getUserDescription(subject));
} else {
envVars.put(TOKEN_VAR, osoToken);
return subject;
}
} catch (InfrastructureException e) {
throw new RuntimeException(
format("OSO token retrieval for user '%s' failed with error: %s", getUserDescription(subject),
e.getMessage()));
}
envVars.put(TOKEN_VAR, token);
}

UserCheTenantData tenantDataData = getTenantData(subject);
if (tenantDataData == null) {
// we can't perform operations without tenant data, so do nothing
return;
private Void addTenantEnvVars(Map<String, String> envVars, Subject subject) {
try {
UserCheTenantData tenantData = tenantDataProvider.getUserCheTenantData(subject, "user");
envVars.put(CLUSTER_VAR, tenantData.getClusterUrl());
envVars.put(PROJECT_VAR, tenantData.getNamespace());
return null;
} catch (InfrastructureException e) {
throw new RuntimeException(
format("OSO tenant data retrieval for user '%s' failed with error: %s", getUserDescription(subject),
e.getMessage()));
}
envVars.put(CLUSTER_VAR, tenantDataData.getClusterUrl());
envVars.put(PROJECT_VAR, tenantDataData.getNamespace());
}

private void injectEnvVars(OpenShiftEnvironment osEnv,
Map<String, String> envVars) {
Collection<Pod> pods = osEnv.getPods().values();
pods.forEach(pod -> pod.getSpec().getContainers().forEach(container -> {
envVars.forEach((key, value) -> {
container.getEnv().removeIf(envVar -> envVar.getName().equals(key));
container.getEnv().add(new EnvVar(key, value, null));
});
}));

// CompletableFuture<Subject> future;
// CompletableFuture<Void> voidCompletableFuture =
// future.thenApply(subject1 -> subject1).thenApply(subject1 -> envVars).thenAccept();
// voidCompletableFuture.
}

private String getOsoToken(Subject subject) {
String osoToken = null;
try {
osoToken = openshiftUserTokenProvider.getToken(subject);
if (osoToken == null) {
LOG.error("OSO token not found for user " + getUserDescription(subject));
}
} catch (InfrastructureException e) {
LOG.error(
format(
"OSO token retrieval for user '%s' failed with error: %s",
getUserDescription(subject), e.getMessage()));
}
return osoToken;
}

private UserCheTenantData getTenantData(Subject subject) {
UserCheTenantData userCheTenantData = null;
try {
userCheTenantData = tenantDataProvider.getUserCheTenantData(subject, "user");
} catch (InfrastructureException e) {
LOG.error(
format(
"OSO tenant data retrieval for user '%s' failed with error: %s",
getUserDescription(subject), e.getMessage()));
}
return userCheTenantData;
private Void logError(Throwable e) {
LOG.error(e.getLocalizedMessage());
return null;
}

private String getUserDescription(Subject subject) {
Expand Down

0 comments on commit a7da2c9

Please sign in to comment.