Skip to content

Commit

Permalink
Do nothing if installation location cannot be determined, inject envi…
Browse files Browse the repository at this point in the history
…ronment variables into OpenShiftCheInstallationLocation

Signed-off-by: Tom George <tgeorge@redhat.com>
  • Loading branch information
Tom George committed Apr 20, 2020
1 parent 3fa9757 commit 650f205
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 34 deletions.
4 changes: 0 additions & 4 deletions infrastructures/openshift/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
*/
package org.eclipse.che.workspace.infrastructure.openshift.environment;

import javax.annotation.PostConstruct;
import com.google.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* OpenShiftCheInstallationLocation checks the KUBERNETES_NAMESPACE and POD_NAMESPACE environment variables
* to determine what namespace Che is installed in. Users should use this class to retrieve the installation
* namespace name.
* OpenShiftCheInstallationLocation checks the KUBERNETES_NAMESPACE and POD_NAMESPACE environment
* variables to determine what namespace Che is installed in. Users should use this class to
* retrieve the installation namespace name.
*
* @author Tom George
*/
Expand All @@ -28,23 +29,20 @@ public class OpenShiftCheInstallationLocation {

private static final Logger LOG = LoggerFactory.getLogger(OpenShiftCheInstallationLocation.class);

private String installationLocationNamespace;
@Inject(optional = true)
@Named("env.KUBERNETES_NAMESPACE")
private String kubernetesNamespace = null;

@Inject(optional = true)
@Named("env.POD_NAMESPACE")
private String podNamespace = null;

/** @return The name of the namespace where Che is installed */
public String getInstallationLocationNamespace() {
return installationLocationNamespace;
}

@PostConstruct
public void postConstruct() {
String kubernetesNamespace = System.getenv("KUBERNETES_NAMESPACE");
String podNamespace = System.getenv("POD_NAMESPACE");

if (kubernetesNamespace == null && podNamespace == null) {
LOG.info(
"Neither KUBERNETES_NAMESPACE nor POD_NAMESPACE is defined. Unable to determine Che installation location");
LOG.warn(
"Neither KUBERNETES_NAMESPACE nor POD_NAMESPACE is defined. Unable to determine Che installation location");
}
installationLocationNamespace =
kubernetesNamespace == null ? podNamespace : kubernetesNamespace;
return kubernetesNamespace == null ? podNamespace : kubernetesNamespace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftClientFactory;
import org.eclipse.che.workspace.infrastructure.openshift.environment.OpenShiftCheInstallationLocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class creates the necessary role and rolebindings to allow the che serviceaccount to stop
Expand All @@ -28,28 +30,36 @@
public class OpenShiftStopWorkspaceRoleProvisioner {

private final OpenShiftClientFactory clientFactory;
private final OpenShiftCheInstallationLocation installationLocation;
private final String installationLocation;

private static final Logger LOG = LoggerFactory.getLogger(OpenShiftCheInstallationLocation.class);

@Inject
public OpenShiftStopWorkspaceRoleProvisioner(
OpenShiftClientFactory clientFactory, OpenShiftCheInstallationLocation installationLocation) {
this.clientFactory = clientFactory;
this.installationLocation = installationLocation;
this.installationLocation = installationLocation.getInstallationLocationNamespace();
}

public void provision(String projectName) throws InfrastructureException {
OpenShiftClient osClient = clientFactory.createOC();
String stopWorkspacesRoleName = "workspace-stop";
if (osClient.roles().inNamespace(projectName).withName(stopWorkspacesRoleName).get() == null) {
if (installationLocation != null) {
OpenShiftClient osClient = clientFactory.createOC();
String stopWorkspacesRoleName = "workspace-stop";
if (osClient.roles().inNamespace(projectName).withName(stopWorkspacesRoleName).get()
== null) {
osClient
.roles()
.inNamespace(projectName)
.createOrReplace(createStopWorkspacesRole(stopWorkspacesRoleName));
}
osClient
.roles()
.roleBindings()
.inNamespace(projectName)
.createOrReplace(createStopWorkspacesRole(stopWorkspacesRoleName));
.createOrReplace(createStopWorkspacesRoleBinding(projectName));
} else {
LOG.warn(
"Could not determine Che installation location. Did not provision stop workspace Role and RoleBinding.");
}
osClient
.roleBindings()
.inNamespace(projectName)
.createOrReplace(createStopWorkspacesRoleBinding(projectName));
}

protected OpenshiftRole createStopWorkspacesRole(String name) {
Expand Down Expand Up @@ -95,7 +105,7 @@ protected OpenshiftRoleBinding createStopWorkspacesRoleBinding(String projectNam
new ObjectReferenceBuilder()
.withKind("ServiceAccount")
.withName("che")
.withNamespace(installationLocation.getInstallationLocationNamespace())
.withNamespace(installationLocation)
.build())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.inject.ConfigurationException;
import org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace;
import org.eclipse.che.workspace.infrastructure.kubernetes.util.KubernetesSharedPool;
import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftClientConfigFactory;
import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftClientFactory;
Expand Down

0 comments on commit 650f205

Please sign in to comment.