Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ability to list available k8s namespaces #14541

Merged
merged 3 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,24 @@ che.infra.kubernetes.ingress.domain=
# Ignored for OpenShift infra. Use `che.infra.openshift.project` instead
che.infra.kubernetes.namespace=

# Defines Kubernetes default namespace in which user's workspaces are created
# if user does not override it.
# It's possible to use <username> and <userid> placeholders (e.g.: che-workspace-<username>).
sleshchenko marked this conversation as resolved.
Show resolved Hide resolved
# In that case, new namespace will be created for each user.
# Is used by OpenShift infra as well to specify Project
#
# BETA It's not fully supported by infra.
# Use che.infra.kubernetes.namespace to configure workspaces' namespace
che.infra.kubernetes.namespace.default=<username>-che

# Defines if a user is able to specify Kubernetes namespace different from default.
# It's NOT RECOMMENDED to configured true without OAuth configured.
# Is used by OpenShift infra as well to allows users choose Project
#
# BETA It's not fully supported by infra.
# Use che.infra.kubernetes.namespace to configure workspaces' namespace
che.infra.kubernetes.namespace.allow_user_defined=false

# Defines Kubernetes Service Account name which should be specified to be bound to all workspaces pods.
# Note that Kubernetes Infrastructure won't create the service account and it should exist.
# OpenShift infrastructure will check if project is predefined(if `che.infra.openshift.project` is not empty):
Expand Down
90 changes: 90 additions & 0 deletions infrastructures/kubernetes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
</parent>
<artifactId>infrastructure-kubernetes</artifactId>
<name>Infrastructure :: Kubernetes</name>
<properties>
<dto-generator-out-directory>${project.build.directory}/generated-sources/dto/</dto-generator-out-directory>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down Expand Up @@ -79,6 +82,10 @@
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
Expand Down Expand Up @@ -185,6 +192,11 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-account</artifactId>
Expand Down Expand Up @@ -215,6 +227,11 @@
<artifactId>org.eclipse.persistence.jpa</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.everrest</groupId>
<artifactId>everrest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
Expand Down Expand Up @@ -243,6 +260,79 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-dto-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.eclipse.che.infrastructure</groupId>
<artifactId>infrastructure-kubernetes</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<configuration>
<dtoPackages>
<package>org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.dto</package>
</dtoPackages>
<outputDirectory>${dto-generator-out-directory}</outputDirectory>
<genClassName>org.eclipse.che.workspace.infrastructure.kubernetes.api.server.dto.DtoServerImpls</genClassName>
<impl>server</impl>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>pre-compile</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-resource</id>
<phase>process-sources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${dto-generator-out-directory}/META-INF</directory>
<targetPath>META-INF</targetPath>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>add-source</id>
<phase>process-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${dto-generator-out-directory}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<!-- Create the test jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.che.api.workspace.shared.Constants;
import org.eclipse.che.workspace.infrastructure.docker.environment.dockerimage.DockerImageEnvironment;
import org.eclipse.che.workspace.infrastructure.docker.environment.dockerimage.DockerImageEnvironmentFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.api.server.KubernetesNamespaceService;
import org.eclipse.che.workspace.infrastructure.kubernetes.cache.jpa.JpaKubernetesRuntimeCacheModule;
import org.eclipse.che.workspace.infrastructure.kubernetes.devfile.DockerimageComponentToWorkspaceApplier;
import org.eclipse.che.workspace.infrastructure.kubernetes.devfile.KubernetesComponentToWorkspaceApplier;
Expand Down Expand Up @@ -78,6 +79,8 @@
public class KubernetesInfraModule extends AbstractModule {
@Override
protected void configure() {
bind(KubernetesNamespaceService.class);

MapBinder<String, InternalEnvironmentFactory> factories =
MapBinder.newMapBinder(binder(), String.class, InternalEnvironmentFactory.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.workspace.infrastructure.kubernetes.api.server;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

import com.google.common.annotations.Beta;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.List;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.eclipse.che.api.core.rest.Service;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.dto.server.DtoFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta;
import org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.dto.KubernetesNamespaceMetaDto;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespaceFactory;

/** @author Sergii Leshchenko */
@Api(
value = "kubernetes-namespace",
description = "Kubernetes REST API for working with Namespaces")
@Path("/kubernetes/namespace")
@Beta
public class KubernetesNamespaceService extends Service {
sleshchenko marked this conversation as resolved.
Show resolved Hide resolved

private final KubernetesNamespaceFactory namespaceFactory;

@Inject
public KubernetesNamespaceService(KubernetesNamespaceFactory namespaceFactory) {
this.namespaceFactory = namespaceFactory;
}

@GET
@Produces(APPLICATION_JSON)
@ApiOperation(
value = "Get k8s namespaces where user is able to create workspaces",
notes =
"This operation can be performed only by authorized user."
+ "This is under beta and may be significant changed",
response = String.class,
responseContainer = "List")
@ApiResponses({
@ApiResponse(code = 200, message = "The namespaces successfully fetched"),
@ApiResponse(code = 500, message = "Internal server error occurred during namespaces fetching")
})
public List<KubernetesNamespaceMetaDto> getNamespaces() throws InfrastructureException {
return namespaceFactory.list().stream().map(this::asDto).collect(Collectors.toList());
}

private KubernetesNamespaceMetaDto asDto(KubernetesNamespaceMeta kubernetesNamespaceMeta) {
return DtoFactory.newDto(KubernetesNamespaceMetaDto.class)
.withName(kubernetesNamespaceMeta.getName())
.withAttributes(kubernetesNamespaceMeta.getAttributes());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta;

/** @author Sergii Leshchenko */
public class KubernetesNamespaceMetaImpl implements KubernetesNamespaceMeta {

private String name;
private Map<String, String> attributes;

public KubernetesNamespaceMetaImpl(String name) {
this.name = name;
}

public KubernetesNamespaceMetaImpl(String name, Map<String, String> attributes) {
this.name = name;
if (attributes != null) {
this.attributes = new HashMap<>(attributes);
}
}

public KubernetesNamespaceMetaImpl(KubernetesNamespaceMeta namespaceMeta) {
this(namespaceMeta.getName(), namespaceMeta.getAttributes());
}

@Override
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public Map<String, String> getAttributes() {
if (attributes == null) {
attributes = new HashMap<>();
}
return attributes;
}

public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
metlos marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof KubernetesNamespaceMetaImpl)) {
sleshchenko marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
KubernetesNamespaceMetaImpl that = (KubernetesNamespaceMetaImpl) o;
return Objects.equals(getName(), that.getName())
&& Objects.equals(getAttributes(), that.getAttributes());
}

@Override
public int hashCode() {
return Objects.hash(getName(), getAttributes());
}

@Override
public String toString() {
return "KubernetesNamespaceMetaImpl{"
+ "name='"
+ name
+ '\''
+ ", attributes="
+ attributes
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.workspace.infrastructure.kubernetes.api.shared;

import java.util.Map;

/**
* Describes meta information about kubernetes namespace.
*
* @author Sergii Leshchenko
*/
public interface KubernetesNamespaceMeta {

/**
* Attribute that shows if k8s namespace is configured as default. Possible values: true/false.
* Absent value should be considered as false.
*/
String DEFAULT_ATTRIBUTE = "default";

/**
* Attributes that contains information about current namespace status. Example values: Active,
* Terminating. Absent value indicates that namespace is not created yet.
*/
String PHASE_ATTRIBUTE = "phase";

/**
* Returns the name of namespace.
*
* <p>Value may be not a name of existing namespace, but predicted name with placeholders inside,
* like <workspaceid>.
*/
String getName();

/** Returns namespace attributes, which may contains additional info about it like description. */
Map<String, String> getAttributes();
}
Loading