From a7da2c9950fcfca5a257495cd170b940290b2c3c Mon Sep 17 00:00:00 2001 From: Oleksandr Garagatyi Date: Thu, 1 Mar 2018 22:42:30 +0200 Subject: [PATCH] fixup! fixup! fixup! fixup! fixup! rh-che #541: Login to user project using oc CLI in workspace containers --- .../multitenant/Fabric8MultiTenantModule.java | 2 + .../RhCheInfraEnvironmentProvisioner.java | 105 +++++++++--------- 2 files changed, 57 insertions(+), 50 deletions(-) diff --git a/plugins/fabric8-multi-tenant-manager/src/main/java/com/redhat/che/multitenant/Fabric8MultiTenantModule.java b/plugins/fabric8-multi-tenant-manager/src/main/java/com/redhat/che/multitenant/Fabric8MultiTenantModule.java index 56e5e0032..fc224d7db 100644 --- a/plugins/fabric8-multi-tenant-manager/src/main/java/com/redhat/che/multitenant/Fabric8MultiTenantModule.java +++ b/plugins/fabric8-multi-tenant-manager/src/main/java/com/redhat/che/multitenant/Fabric8MultiTenantModule.java @@ -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; @@ -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); } } diff --git a/plugins/fabric8-multi-tenant-manager/src/main/java/com/redhat/che/multitenant/RhCheInfraEnvironmentProvisioner.java b/plugins/fabric8-multi-tenant-manager/src/main/java/com/redhat/che/multitenant/RhCheInfraEnvironmentProvisioner.java index 33f18ac85..a7cef8b03 100644 --- a/plugins/fabric8-multi-tenant-manager/src/main/java/com/redhat/che/multitenant/RhCheInfraEnvironmentProvisioner.java +++ b/plugins/fabric8-multi-tenant-manager/src/main/java/com/redhat/che/multitenant/RhCheInfraEnvironmentProvisioner.java @@ -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; @@ -36,6 +37,8 @@ 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; @@ -43,7 +46,7 @@ /** * 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 */ @@ -89,30 +92,61 @@ public void provision(OpenShiftEnvironment osEnv, RuntimeIdentity identity) thro Map 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 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 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 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 envVars) { Collection pods = osEnv.getPods().values(); pods.forEach(pod -> pod.getSpec().getContainers().forEach(container -> { envVars.forEach((key, value) -> { @@ -120,40 +154,11 @@ public void provision(OpenShiftEnvironment osEnv, RuntimeIdentity identity) thro container.getEnv().add(new EnvVar(key, value, null)); }); })); - -// CompletableFuture future; -// CompletableFuture 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) {