Skip to content

Commit

Permalink
Add an ability to list available k8s namespaces
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Leshchenko <sleshche@redhat.com>
  • Loading branch information
sleshchenko committed Sep 13, 2019
1 parent 10c9c24 commit 82ea302
Show file tree
Hide file tree
Showing 14 changed files with 873 additions and 0 deletions.
80 changes: 80 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 @@ -251,6 +258,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.bootstrapper.KubernetesBootstrapperFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.cache.jpa.JpaKubernetesRuntimeCacheModule;
import org.eclipse.che.workspace.infrastructure.kubernetes.devfile.DockerimageComponentToWorkspaceApplier;
Expand Down Expand Up @@ -79,6 +80,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,67 @@
/*
* 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 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")
public class KubernetesNamespaceService extends Service {

private final KubernetesNamespaceFactory namespaceFactory;

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

@GET
@Produces(APPLICATION_JSON)
@ApiOperation(
value = "Get existing k8s namespaces where user is able to create workspaces",
notes = "This operation can be performed only by authorized user",
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,85 @@
/*
* 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);
}
}

@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;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof KubernetesNamespaceMetaImpl)) {
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,27 @@
/*
* 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 {
/** Returns the name of namespace. */
String getName();

/** Returns namespace attributes, which may contains additional info about it like description. */
Map<String, String> getAttributes();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.dto;

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

/** @author Sergii Leshchenko */
public interface KubernetesNamespaceMetaDto extends KubernetesNamespaceMeta {
@Override
String getName();

void setName(String name);

KubernetesNamespaceMetaDto withName(String name);

@Override
Map<String, String> getAttributes();

void setAttributes(Map<String, String> attributes);

KubernetesNamespaceMetaDto withAttributes(Map<String, String> attributes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
package org.eclipse.che.workspace.infrastructure.kubernetes.namespace;

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Collections.singletonList;

import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.fabric8.kubernetes.api.model.Namespace;
import java.util.List;
import java.util.stream.Collectors;
import javax.inject.Named;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesClientFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl;
import org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta;

/**
* Helps to create {@link KubernetesNamespace} instances.
Expand Down Expand Up @@ -56,6 +62,26 @@ public boolean isPredefined() {
return isPredefined;
}

/** Returns lists of existing namespaces where a user is able to run workspaces. */
public List<KubernetesNamespaceMeta> list() throws InfrastructureException {
if (isPredefined) {
Namespace predefinedNamespace =
clientFactory.create().namespaces().withName(namespaceName).get();
return singletonList(
new KubernetesNamespaceMetaImpl(predefinedNamespace.getMetadata().getName()));
} else {
return clientFactory
.create()
.namespaces()
.list()
.getItems()
.stream()
.filter(n -> !"Terminating".equals(n.getStatus().getPhase()))
.map(n -> new KubernetesNamespaceMetaImpl(n.getMetadata().getName()))
.collect(Collectors.toList());
}
}

/**
* Creates a Kubernetes namespace for the specified workspace.
*
Expand Down
Loading

0 comments on commit 82ea302

Please sign in to comment.