You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by im...@apache.org on 2014/12/24 18:36:33 UTC
[10/12] stratos git commit: Introducing generic methods for creating
kubernetes replication controllers, services and pods
Introducing generic methods for creating kubernetes replication controllers, services and pods
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/11f9cbd1
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/11f9cbd1
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/11f9cbd1
Branch: refs/heads/master
Commit: 11f9cbd15dee2fdba21e893efd9ee34c605bec53
Parents: 025a59c
Author: Imesh Gunaratne <im...@apache.org>
Authored: Wed Dec 24 18:41:59 2014 +0530
Committer: Imesh Gunaratne <im...@apache.org>
Committed: Wed Dec 24 18:41:59 2014 +0530
----------------------------------------------------------------------
...inerClusterContextToKubernetesContainer.java | 151 ---
...erClusterContextToReplicationController.java | 86 --
.../kubernetes/client/KubernetesApiClient.java | 1101 +++++++++++-------
.../kubernetes/client/KubernetesConstants.java | 32 +
.../KubernetesAPIClientInterface.java | 92 +-
.../kubernetes/client/model/Container.java | 22 +-
.../stratos/kubernetes/client/model/Pod.java | 2 +-
.../live/KubernetesApiClientLiveTest.java | 459 +++-----
.../kubernetes/client/unit/PodUnitTest.java | 4 +-
9 files changed, 932 insertions(+), 1017 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/stratos/blob/11f9cbd1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/functions/ContainerClusterContextToKubernetesContainer.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/functions/ContainerClusterContextToKubernetesContainer.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/functions/ContainerClusterContextToKubernetesContainer.java
deleted file mode 100644
index 5665d59..0000000
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/functions/ContainerClusterContextToKubernetesContainer.java
+++ /dev/null
@@ -1,151 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.stratos.cloud.controller.functions;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.stratos.cloud.controller.context.CloudControllerContext;
-import org.apache.stratos.cloud.controller.domain.*;
-import org.apache.stratos.cloud.controller.util.CloudControllerUtil;
-import org.apache.stratos.common.Properties;
-import org.apache.stratos.common.Property;
-import org.apache.stratos.common.beans.NameValuePair;
-import org.apache.stratos.common.constants.StratosConstants;
-import org.apache.stratos.kubernetes.client.model.Container;
-import org.apache.stratos.kubernetes.client.model.EnvironmentVariable;
-import org.apache.stratos.kubernetes.client.model.Port;
-
-import com.google.common.base.Function;
-
-/**
- * Is responsible for converting a MemberContext object to a Kubernetes
- * {@link Container} Object.
- */
-public class ContainerClusterContextToKubernetesContainer implements Function<MemberContext, Container> {
-
- private static final Log log = LogFactory.getLog(ContainerClusterContextToKubernetesContainer.class);
-
- @Override
- public Container apply(MemberContext memberContext) {
- String clusterId = memberContext.getClusterId();
- ClusterContext clusterContext = CloudControllerContext.getInstance().getClusterContext(clusterId);
-
- Container container = new Container();
- container.setName(clusterContext.findContainerHostName());
-
- Cartridge cartridge = CloudControllerContext.getInstance().getCartridge(clusterContext.getCartridgeType());
- if (cartridge == null) {
- String message = "Could not find cartridge: [cartridge-type] " + clusterContext.getCartridgeType();
- log.error(message);
- throw new RuntimeException(message);
- }
-
- Partition partition = memberContext.getPartition();
- if(partition == null) {
- String message = "Partition not found in member context: [member-id] " + memberContext.getMemberId();
- log.error(message);
- throw new RuntimeException(message);
- }
-
- IaasProvider iaasProvider = cartridge.getIaasProviderOfPartition(partition.getId());
- if(iaasProvider == null) {
- String message = "Could not find iaas provider: [partition-id] " + partition.getId();
- log.error(message);
- throw new RuntimeException(message);
- }
- container.setImage(iaasProvider.getImage());
- container.setPorts(getPorts(cartridge));
- container.setEnv(getEnvironmentVariables(memberContext, clusterContext));
- return container;
- }
-
- private Port[] getPorts(Cartridge cartridge) {
- Port[] ports = new Port[cartridge.getPortMappings().size()];
- List<Port> portList = new ArrayList<Port>();
-
- for (PortMapping portMapping : cartridge.getPortMappings()) {
- Port p = new Port();
- p.setName(p.getProtocol() + "-" + p.getContainerPort());
- // In kubernetes transport protocol always be 'tcp'
- p.setProtocol("tcp");
- p.setContainerPort(Integer.parseInt(portMapping.getPort()));
- portList.add(p);
- }
- return portList.toArray(ports);
- }
-
- private EnvironmentVariable[] getEnvironmentVariables(MemberContext memberContext, ClusterContext clusterContext) {
- String kubernetesClusterId = CloudControllerUtil.getProperty(clusterContext.getProperties(),
- StratosConstants.KUBERNETES_CLUSTER_ID);
-
- List<EnvironmentVariable> environmentVariables = new ArrayList<EnvironmentVariable>();
-
- // Set dynamic payload
- List<NameValuePair> payload = memberContext.getDynamicPayload();
- if (payload != null) {
- for (NameValuePair parameter : payload) {
- addToEnvironmentVariables(environmentVariables, parameter.getName(), parameter.getValue());
- }
- }
-
- // Set member properties
- Properties properties = memberContext.getProperties();
- if (properties != null) {
- for (Property property : properties.getProperties()) {
- addToEnvironmentVariables(environmentVariables, property.getName(),
- property.getValue());
- }
- }
-
- // Set kubernetes cluster id
- addToEnvironmentVariables(environmentVariables, StratosConstants.KUBERNETES_CLUSTER_ID,
- kubernetesClusterId);
-
- if(log.isDebugEnabled()) {
- log.debug(String.format("Environment variables: [cluster-id] %s [member-id] %s [variables] %s",
- memberContext.getClusterId(), memberContext.getMemberId(), environmentVariables.toString()));
- }
-
- EnvironmentVariable[] array = new EnvironmentVariable[environmentVariables.size()];
- return environmentVariables.toArray(array);
- }
-
- private void addToEnvironment(List<EnvironmentVariable> envVars, String payload) {
- if (payload != null) {
- String[] entries = payload.split(",");
- for (String entry : entries) {
- String[] var = entry.split("=");
- if (var.length != 2) {
- continue;
- }
- addToEnvironmentVariables(envVars, var[0], var[1]);
- }
- }
- }
-
- private void addToEnvironmentVariables(List<EnvironmentVariable> envVars, String name, String value) {
- EnvironmentVariable var = new EnvironmentVariable();
- var.setName(name);
- var.setValue(value);
- envVars.add(var);
- }
-}
http://git-wip-us.apache.org/repos/asf/stratos/blob/11f9cbd1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/functions/ContainerClusterContextToReplicationController.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/functions/ContainerClusterContextToReplicationController.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/functions/ContainerClusterContextToReplicationController.java
deleted file mode 100644
index 1e66f9e..0000000
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/functions/ContainerClusterContextToReplicationController.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.stratos.cloud.controller.functions;
-
-import org.apache.stratos.cloud.controller.context.CloudControllerContext;
-import org.apache.stratos.cloud.controller.domain.ClusterContext;
-import org.apache.stratos.cloud.controller.domain.ContainerClusterContext;
-import org.apache.stratos.cloud.controller.domain.MemberContext;
-import org.apache.stratos.kubernetes.client.model.Container;
-import org.apache.stratos.kubernetes.client.model.Labels;
-import org.apache.stratos.kubernetes.client.model.Manifest;
-import org.apache.stratos.kubernetes.client.model.Pod;
-import org.apache.stratos.kubernetes.client.model.ReplicationController;
-import org.apache.stratos.kubernetes.client.model.Selector;
-import org.apache.stratos.kubernetes.client.model.State;
-
-import com.google.common.base.Function;
-
-/**
- * Is responsible for converting a {@link org.apache.stratos.cloud.controller.domain.MemberContext} object to a Kubernetes
- * {@link ReplicationController} object:
- * - A replication controller is created for each member and environment variables generated by the member context.
- */
-public class ContainerClusterContextToReplicationController implements
- Function<MemberContext, ReplicationController> {
-
- @Override
- public ReplicationController apply(MemberContext memberContext) {
-
- ReplicationController replicationController = new ReplicationController();
- replicationController.setId(memberContext.getMemberId());
- replicationController.setKind("ReplicationController");
- replicationController.setApiVersion("v1beta1");
-
- State state = new State();
- state.setReplicas(1);
- Selector selector = new Selector();
- selector.setName(memberContext.getClusterId());
- state.setReplicaSelector(selector);
-
- Pod podTemplate = new Pod();
- State podState = new State();
- Manifest manifest = new Manifest();
- manifest.setVersion("v1beta1");
- manifest.setId(memberContext.getClusterId());
- podTemplate.setState(podState);
-
- // Add container to the manifest
- ContainerClusterContextToKubernetesContainer containerFunc = new ContainerClusterContextToKubernetesContainer();
- Container container = containerFunc.apply(memberContext);
- manifest.addContainer(container);
- podState.setManifest(manifest);
-
- // Set pod labels
- Labels podLabels = new Labels();
- podLabels.setName(memberContext.getClusterId());
- podTemplate.setLabels(podLabels);
-
- state.setPodTemplate(podTemplate);
- replicationController.setDesiredState(state);
-
- // Set replication controller labels
- Labels replicatoionControllerLabels = new Labels();
- replicatoionControllerLabels.setName(memberContext.getClusterId());
- replicationController.setLabels(replicatoionControllerLabels);
-
- return replicationController;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/stratos/blob/11f9cbd1/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesApiClient.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesApiClient.java b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesApiClient.java
index 120665b..479de3d 100644
--- a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesApiClient.java
+++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesApiClient.java
@@ -29,471 +29,690 @@ import org.apache.http.client.utils.URIBuilder;
import org.apache.stratos.kubernetes.client.exceptions.KubernetesClientException;
import org.apache.stratos.kubernetes.client.interfaces.KubernetesAPIClientInterface;
import org.apache.stratos.kubernetes.client.model.*;
-import org.apache.stratos.kubernetes.client.rest.KubernetesResponse;
+import org.apache.stratos.kubernetes.client.rest.HttpResponse;
import org.apache.stratos.kubernetes.client.rest.RestClient;
import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
public class KubernetesApiClient implements KubernetesAPIClientInterface {
-
- private static final Log log = LogFactory.getLog(KubernetesApiClient.class);
- private RestClient restClient;
- private String baseURL;
-
- public KubernetesApiClient(String endpointUrl) {
- restClient = new RestClient();
- baseURL = endpointUrl;
- }
-
- @Override
- public Pod getPod(String podId) throws KubernetesClientException{
- try {
- URI uri = new URIBuilder(baseURL+"pods/"+podId).build();
- KubernetesResponse res = restClient.doGet(uri);
-
- handleNullResponse("Pod ["+podId+"] retrieval failed.", res);
-
- if (res.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
- String msg = "Pod ["+podId+"] doesn't exist.";
- log.error(msg);
- throw new KubernetesClientException(msg);
- }
-
- String content = res.getContent();
-
+
+ private static final Log log = LogFactory.getLog(KubernetesApiClient.class);
+ public static final String CONTEXT_PODS = "pods";
+ private RestClient restClient;
+ private String baseURL;
+
+ public KubernetesApiClient(String endpointUrl) {
+ restClient = new RestClient();
+ baseURL = endpointUrl;
+ }
+
+ @Override
+ public void createPod(String podId, String podName, String dockerImage, List<Integer> containerPorts)
+ throws KubernetesClientException {
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Creating pod: [pod-id] %s [pod-name] %s [docker-image] %s [container-ports] %s",
+ podId, podName, dockerImage, containerPorts));
+ }
+
+ // Create pod definition
+ Pod pod = new Pod();
+ pod.setApiVersion(KubernetesConstants.KUBERNETES_API_VERSION);
+ pod.setId(podId);
+ pod.setKind(KubernetesConstants.KIND_POD);
+
+ // Set pod labels
+ Labels podLabels = new Labels();
+ podLabels.setName(podName);
+ pod.setLabels(podLabels);
+
+ State desiredState = new State();
+ Manifest manifest = new Manifest();
+ manifest.setId(podId);
+ manifest.setVersion(KubernetesConstants.KUBERNETES_API_VERSION);
+
+ // Set container template
+ Container containerTemplate = new Container();
+ containerTemplate.setName(podName);
+ containerTemplate.setImage(dockerImage);
+ containerTemplate.setPorts(convertIntPortListToPortList(containerPorts));
+
+ manifest.addContainer(containerTemplate);
+ desiredState.setManifest(manifest);
+ pod.setDesiredState(desiredState);
+
+ // Invoke the api to create the pod
+ createPod(pod);
+
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Pod created successfully: [pod-id] %s", podId));
+ }
+ }
+
+ @Override
+ public Pod getPod(String podId) throws KubernetesClientException {
+ try {
+ URI uri = new URIBuilder(baseURL + "pods/" + podId).build();
+ HttpResponse response = restClient.doGet(uri);
+
+ handleNullResponse(String.format("Could not retrieve pod: [pod-id] %s", podId), response);
+
+ if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
+ return null;
+ }
+
+ String content = response.getContent();
+
+ GsonBuilder gsonBuilder = new GsonBuilder();
+ Gson gson = gsonBuilder.create();
+ return gson.fromJson(content, Pod.class);
+ } catch (KubernetesClientException e) {
+ throw e;
+ } catch (Exception e) {
+ String msg = String.format("Could not retrieve pod: [pod-id] %s", podId);
+ log.error(msg, e);
+ throw new KubernetesClientException(msg, e);
+ }
+ }
+
+ @Override
+ public List<Pod> getPods() throws KubernetesClientException {
+
+ try {
+ URI uri = new URIBuilder(baseURL + "pods").build();
+ HttpResponse response = restClient.doGet(uri);
+
+ handleNullResponse("Pod retrieval failed.", response);
+
+ if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
+ return null;
+ }
+
+ String content = response.getContent();
+
+ GsonBuilder gsonBuilder = new GsonBuilder();
+ Gson gson = gsonBuilder.create();
+ PodList result = gson.fromJson(content, PodList.class);
+
+ List<Pod> podList = new ArrayList<Pod>();
+ if((result != null) && (result.getItems() != null)) {
+ for(Pod pod : result.getItems()) {
+ if(pod != null) {
+ podList.add(pod);
+ }
+ }
+ }
+ return podList;
+ } catch (Exception e) {
+ String msg = "Error while retrieving Pods.";
+ log.error(msg, e);
+ throw new KubernetesClientException(msg, e);
+ }
+ }
+
+ private void createPod(Pod pod) throws KubernetesClientException {
+ try {
+ GsonBuilder gsonBuilder = new GsonBuilder();
+ Gson gson = gsonBuilder.create();
+ String content = gson.toJson(pod);
+ if (log.isDebugEnabled()) {
+ log.debug("Create pod request body: " + content);
+ }
+ URI uri = new URIBuilder(baseURL + CONTEXT_PODS).build();
+ HttpResponse response = restClient.doPost(uri, content);
+ handleNullResponse(String.format("Could not create pod: [pod-id] %s", pod.getId()), response);
+
+ if (response.getStatusCode() == HttpStatus.SC_CONFLICT) {
+ log.warn(String.format("Pod already created: [pod-id] %s", pod.getId()));
+ return;
+ }
+
+ if (response.getStatusCode() != HttpStatus.SC_ACCEPTED &&
+ response.getStatusCode() != HttpStatus.SC_OK) {
+ String msg = String.format("Could not create pod: [pod-id] %s [message] %s", pod.getId());
+ log.error(msg);
+ throw new KubernetesClientException(msg);
+ }
+ } catch (KubernetesClientException e) {
+ throw e;
+ } catch (Exception e) {
+ String msg = String.format("Could not create pod: [pod-id] %s", pod.getId());
+ log.error(msg, e);
+ throw new KubernetesClientException(msg, e);
+ }
+ }
+
+ @Override
+ public void deletePod(String podId) throws KubernetesClientException {
+ try {
+ if(log.isInfoEnabled()) {
+ log.info(String.format("Deleting pod: [pod-id] %s", podId));
+ }
+
+ URI uri = new URIBuilder(baseURL + "pods/" + podId).build();
+ HttpResponse response = restClient.doDelete(uri);
+
+ handleNullResponse("Pod [" + podId + "] deletion failed.", response);
+
+ if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
+ String message = String.format("Pod not found: [pod-id] %s", podId);
+ log.warn(message);
+ return;
+ }
+
+ if (response.getStatusCode() != HttpStatus.SC_ACCEPTED &&
+ response.getStatusCode() != HttpStatus.SC_OK) {
+ String message = String.format("Could not delete pod: [pod-id] %s [message] %s",
+ podId, response.getKubernetesResponse().getMessage());
+ log.error(message);
+ throw new KubernetesClientException(message);
+ }
+
+ if(log.isInfoEnabled()) {
+ log.info(String.format("Pod deleted successfully: [pod-id] %s", podId));
+ }
+ } catch (KubernetesClientException e) {
+ throw e;
+ } catch (Exception e) {
+ String message = String.format("Could not delete pod: [pod-id] %s", podId);
+ log.error(message, e);
+ throw new KubernetesClientException(message, e);
+ }
+ }
+
+ @Override
+ public ReplicationController getReplicationController(String replicationControllerId)
+ throws KubernetesClientException {
+
+ try {
+ URI uri = new URIBuilder(baseURL + "replicationControllers/" + replicationControllerId).build();
+ HttpResponse response = restClient.doGet(uri);
+
+ handleNullResponse(String.format("Could not retrieve replication controller: " +
+ "[replication-controller-id] %s", replicationControllerId), response);
+
+ if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
+ return null;
+ }
+
+ String content = response.getContent();
+
GsonBuilder gsonBuilder = new GsonBuilder();
- Gson gson = gsonBuilder.create();
- return gson.fromJson(content, Pod.class);
- } catch (KubernetesClientException e) {
- throw e;
- } catch (Exception e) {
- String msg = "Error while retrieving Pod info with Pod ID: "+podId;
- log.error(msg, e);
- throw new KubernetesClientException(msg, e);
- }
- }
-
- @Override
- public Pod[] getAllPods() throws KubernetesClientException {
-
- try {
- URI uri = new URIBuilder(baseURL+"pods").build();
- KubernetesResponse res = restClient.doGet(uri);
-
- handleNullResponse("Pod retrieval failed.", res);
-
- if (res.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
- return new Pod[0];
- }
-
- String content = res.getContent();
-
+ Gson gson = gsonBuilder.create();
+ return gson.fromJson(content, ReplicationController.class);
+ } catch (KubernetesClientException e) {
+ throw e;
+ } catch (Exception e) {
+ String msg = "Error while retrieving Replication Controller info with ID: " + replicationControllerId;
+ log.error(msg, e);
+ throw new KubernetesClientException(msg, e);
+ }
+ }
+
+ @Override
+ public List<ReplicationController> getReplicationControllers() throws KubernetesClientException {
+
+ try {
+ URI uri = new URIBuilder(baseURL + "replicationControllers").build();
+ HttpResponse response = restClient.doGet(uri);
+
+ handleNullResponse("Could not retrieve replication controllers", response);
+
+ if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
+ return null;
+ }
+
+ String content = response.getContent();
+
GsonBuilder gsonBuilder = new GsonBuilder();
- Gson gson = gsonBuilder.create();
- PodList podList = gson.fromJson(content, PodList.class);
- return podList.getItems();
-
- } catch (Exception e) {
- String msg = "Error while retrieving Pods.";
- log.error(msg, e);
- throw new KubernetesClientException(msg, e);
- }
- }
-
- @Override
- public void createPod(Pod pod) throws KubernetesClientException {
-
- try {
- GsonBuilder gsonBuilder = new GsonBuilder();
- Gson gson = gsonBuilder.create();
- String content = gson.toJson(pod);
- if (log.isDebugEnabled()) {
- log.debug("CreatePod Request Body : "+content);
- }
- URI uri = new URIBuilder(baseURL+"pods").build();
- KubernetesResponse res = restClient.doPost(uri, content);
-
- handleNullResponse("Pod "+pod+" creation failed.", res);
-
- if (res.getStatusCode() == HttpStatus.SC_CONFLICT) {
- log.warn("Pod already created. "+pod);
- return;
- }
-
- if (res.getStatusCode() != HttpStatus.SC_ACCEPTED &&
- res.getStatusCode() != HttpStatus.SC_OK) {
- String msg = "Pod ["+pod+"] creation failed. Error: "+
- res.getReason();
- log.error(msg);
- throw new KubernetesClientException(msg);
- }
- } catch (KubernetesClientException e) {
- throw e;
- } catch (Exception e) {
- String msg = "Error while creating Pod: "+pod;
- log.error(msg, e);
- throw new KubernetesClientException(msg, e);
- }
- }
-
- @Override
- public void deletePod(String podId) throws KubernetesClientException {
-
- try {
- URI uri = new URIBuilder(baseURL+"pods/"+podId).build();
- KubernetesResponse res = restClient.doDelete(uri);
-
- handleNullResponse("Pod ["+podId+"] deletion failed.", res);
-
- if (res.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
- String msg = "Pod ["+podId+"] doesn't exist.";
- log.error(msg);
- throw new KubernetesClientException(msg);
- }
-
- if (res.getStatusCode() != HttpStatus.SC_ACCEPTED &&
- res.getStatusCode() != HttpStatus.SC_OK) {
- String msg = "Pod ["+podId+"] deletion failed. Error: "+
- res.getReason();
- log.error(msg);
- throw new KubernetesClientException(msg);
- }
- } catch (KubernetesClientException e) {
- throw e;
- } catch (Exception e) {
- String msg = "Error while deleting Pod with ID: "+podId;
- log.error(msg, e);
- throw new KubernetesClientException(msg, e);
- }
- }
-
- @Override
- public ReplicationController getReplicationController(String controllerId)
- throws KubernetesClientException {
-
- try {
- URI uri = new URIBuilder(baseURL+"replicationControllers/"+controllerId).build();
- KubernetesResponse res = restClient.doGet(uri);
-
- handleNullResponse("Replication Controller ["+controllerId+"] retrieval failed.", res);
-
- if (res.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
- String msg = "Replication Controller ["+controllerId+"] doesn't exist.";
- log.error(msg);
- throw new KubernetesClientException(msg);
- }
-
- String content = res.getContent();
-
+ Gson gson = gsonBuilder.create();
+ ReplicationControllerList controllerList = gson.fromJson(content, ReplicationControllerList.class);
+
+ List<ReplicationController> replicationControllers = new ArrayList<ReplicationController>();
+ if((controllerList != null) && (controllerList.getItems() != null)) {
+ for (ReplicationController replicationController : controllerList.getItems()) {
+ if (replicationController != null) {
+ replicationControllers.add(replicationController);
+ }
+ }
+ }
+ return replicationControllers;
+ } catch (Exception e) {
+ String msg = "Error while retrieving Replication Controllers.";
+ log.error(msg, e);
+ throw new KubernetesClientException(msg, e);
+ }
+ }
+
+ @Override
+ public void createReplicationController(String replicationControllerId, String replicationControllerName,
+ String dockerImage, List<Integer> containerPorts,
+ EnvironmentVariable[] environmentVariables, int replicas) throws KubernetesClientException {
+ try {
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Creating replication controller: [replication-controller-id] %s [name] %s " +
+ "[docker-image] %s [container-ports] %s [replicas] %d", replicationControllerId, replicationControllerName,
+ dockerImage, containerPorts, replicas));
+ }
+
+ // Create replication controller
+ ReplicationController replicationController = new ReplicationController();
+ replicationController.setId(replicationControllerId);
+ replicationController.setKind(KubernetesConstants.KIND_REPLICATION_CONTROLLER);
+ replicationController.setApiVersion(KubernetesConstants.KUBERNETES_API_VERSION);
+
+ // Set replication controller state
+ State replicationControllerDesiredState = new State();
+ replicationControllerDesiredState.setReplicas(replicas);
+ Selector replicationControllerSelector = new Selector();
+ replicationControllerSelector.setName(replicationControllerName);
+ replicationControllerDesiredState.setReplicaSelector(replicationControllerSelector);
+
+ // Create pod template
+ Pod podTemplate = new Pod();
+ State desiredPodState = new State();
+
+ Manifest manifest = new Manifest();
+ manifest.setVersion(KubernetesConstants.KUBERNETES_API_VERSION);
+ manifest.setId(replicationControllerId);
+
+ // Create container template
+ Container containerTemplate = new Container();
+ containerTemplate.setName(replicationControllerName);
+ containerTemplate.setImage(dockerImage);
+ if(environmentVariables != null) {
+ containerTemplate.setEnv(environmentVariables);
+ }
+
+ // Set container ports
+ List<Port> ports = convertIntPortListToPortList(containerPorts);
+ containerTemplate.setPorts(ports);
+ manifest.addContainer(containerTemplate);
+
+ desiredPodState.setManifest(manifest);
+ podTemplate.setDesiredState(desiredPodState);
+
+ // Set pod template labels
+ Labels podTemplateLabels = new Labels();
+ podTemplateLabels.setName(replicationControllerName);
+ podTemplate.setLabels(podTemplateLabels);
+
+ replicationControllerDesiredState.setPodTemplate(podTemplate);
+ replicationController.setDesiredState(replicationControllerDesiredState);
+
+ Labels replicationControllerLabels = new Labels();
+ replicationControllerLabels.setName(replicationControllerName);
+ replicationController.setLabels(replicationControllerLabels);
+
+ // Invoke the api to create the replicate controller
+ createReplicationController(replicationController);
+
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Replication controller created successfully: [replication-controller-id] " +
+ "%s [name] %s [docker-image] %s [container-port] %s [replicas] %d",
+ replicationControllerId, replicationControllerName, dockerImage, containerPorts, replicas));
+ }
+ } catch (Exception e) {
+ String message = "Could not create replication controller: [replication-controller-id] " + replicationControllerId;
+ log.error(message, e);
+ throw new KubernetesClientException(message, e);
+ }
+ }
+
+ private List<Port> convertIntPortListToPortList(List<Integer> containerPorts) {
+ List<Port> ports = new ArrayList<Port>();
+ for (int containerPort : containerPorts) {
+ Port port = new Port();
+ port.setContainerPort(containerPort);
+ port.setHostPort(containerPort);
+ ports.add(port);
+ }
+ return ports;
+ }
+
+ private void createReplicationController(ReplicationController replicationController)
+ throws KubernetesClientException {
+
+ try {
GsonBuilder gsonBuilder = new GsonBuilder();
- Gson gson = gsonBuilder.create();
- return gson.fromJson(content, ReplicationController.class);
- } catch (KubernetesClientException e) {
- throw e;
- } catch (Exception e) {
- String msg = "Error while retrieving Replication Controller info with ID: "+controllerId;
- log.error(msg, e);
- throw new KubernetesClientException(msg, e);
- }
- }
-
- @Override
- public ReplicationController[] getAllReplicationControllers()
- throws KubernetesClientException {
-
- try {
- URI uri = new URIBuilder(baseURL+"replicationControllers").build();
- KubernetesResponse res = restClient.doGet(uri);
-
- handleNullResponse("Replication Controller retrieval failed.", res);
-
- if (res.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
- return new ReplicationController[0];
- }
-
- String content = res.getContent();
-
+ Gson gson = gsonBuilder.create();
+ String content = gson.toJson(replicationController);
+ if (log.isDebugEnabled()) {
+ log.debug("CreateReplicationController request body : " + content);
+ }
+
+ URI uri = new URIBuilder(baseURL + "replicationControllers").build();
+ HttpResponse response = restClient.doPost(uri, content);
+
+ handleNullResponse("Could not create replication controller: [replication-controller-id] " +
+ replicationController.getId(), response);
+
+ if (response.getStatusCode() == HttpStatus.SC_CONFLICT) {
+ log.warn("Replication controller already created: [replication-controller-id] " + replicationController.getId());
+ return;
+ }
+
+ if (response.getStatusCode() != HttpStatus.SC_ACCEPTED &&
+ response.getStatusCode() != HttpStatus.SC_OK) {
+ String message = "Could not create replication controller: [replication-controller-id] " +
+ replicationController.getId() + " [message] " + response.getKubernetesResponse().getMessage();
+ log.error(message);
+ throw new KubernetesClientException(message);
+ }
+
+ } catch (KubernetesClientException e) {
+ throw e;
+ } catch (Exception e) {
+ String msg = "Error while creating replication controller: "
+ + replicationController;
+ log.error(msg, e);
+ throw new KubernetesClientException(msg, e);
+ }
+ }
+
+ @Override
+ public void updateReplicationController(ReplicationController replicationController)
+ throws KubernetesClientException {
+
+ try {
GsonBuilder gsonBuilder = new GsonBuilder();
- Gson gson = gsonBuilder.create();
- ReplicationControllerList controllerList = gson.fromJson(content, ReplicationControllerList.class);
-
- return controllerList.getItems();
- } catch (Exception e) {
- String msg = "Error while retrieving Replication Controllers.";
- log.error(msg, e);
- throw new KubernetesClientException(msg, e);
- }
- }
-
- @Override
- public void createReplicationController(ReplicationController controller)
- throws KubernetesClientException {
-
- try {
- GsonBuilder gsonBuilder = new GsonBuilder();
- Gson gson = gsonBuilder.create();
- String content = gson.toJson(controller);
- if (log.isDebugEnabled()) {
- log.debug("CreateReplicationController Request Body : "+content);
- }
-
- URI uri = new URIBuilder(baseURL+"replicationControllers").build();
- KubernetesResponse res = restClient.doPost(uri, content);
-
- handleNullResponse("Replication Controller "+controller+" creation failed.", res);
-
- if (res.getStatusCode() == HttpStatus.SC_CONFLICT) {
- log.warn("Replication Controller already created. "+controller);
+ Gson gson = gsonBuilder.create();
+ String content = gson.toJson(replicationController);
+ if (log.isDebugEnabled()) {
+ log.debug("Update kubernetes replication controller request body: "
+ + content);
+ }
+
+ URI uri = new URIBuilder(baseURL + "replicationControllers/" + replicationController.getId()).build();
+ HttpResponse response = restClient.doPut(uri, content);
+
+ handleNullResponse("Could not update kubernetes replication controller: [replication-controller-id] " +
+ replicationController.getId(), response);
+
+ if (response.getStatusCode() != HttpStatus.SC_ACCEPTED
+ && response.getStatusCode() != HttpStatus.SC_OK) {
+ String message = "Could not update kubernetes replication controller: [replication-controller-id] " +
+ replicationController.getId() + ": " + response.getReason();
+ log.error(message);
+ throw new KubernetesClientException(message);
+ }
+
+ } catch (KubernetesClientException e) {
+ String message = "Could not update kubernetes replication controller: [replication-controller-id] " +
+ replicationController.getId();
+ log.error(message, e);
+ throw e;
+ } catch (Exception e) {
+ String message = "Could not update kubernetes replication controller: [replication-controller-id] " +
+ replicationController.getId();
+ log.error(message, e);
+ throw new KubernetesClientException(message, e);
+ }
+ }
+
+ @Override
+ public void deleteReplicationController(String replicationControllerId)
+ throws KubernetesClientException {
+
+ try {
+ if(log.isInfoEnabled()) {
+ log.info("Deleting replication controller: [replication-controller-id] " +
+ replicationControllerId);
+ }
+
+ URI uri = new URIBuilder(baseURL + "replicationControllers/" + replicationControllerId).build();
+ HttpResponse response = restClient.doDelete(uri);
+
+ handleNullResponse("Could not delete replication controller: [replication-controller-id] " +
+ replicationControllerId, response);
+
+ if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
+ String message = "Replication controller does not exist: [replication-controller-id] "
+ + replicationControllerId;
+ log.warn(message);
return;
}
-
- if (res.getStatusCode() != HttpStatus.SC_ACCEPTED &&
- res.getStatusCode() != HttpStatus.SC_OK) {
- String msg = "Replication Controller [" + controller
- + "] creation failed. Error: "
- + res.getReason();
- log.error(msg);
- throw new KubernetesClientException(msg);
- }
-
- } catch (KubernetesClientException e) {
- throw e;
- } catch (Exception e) {
- String msg = "Error while creating Replication Controller: "
- + controller;
- log.error(msg, e);
- throw new KubernetesClientException(msg, e);
- }
- }
-
- @Override
- public void updateReplicationController(ReplicationController replicationController)
- throws KubernetesClientException {
-
- try {
- GsonBuilder gsonBuilder = new GsonBuilder();
- Gson gson = gsonBuilder.create();
- String content = gson.toJson(replicationController);
- if (log.isDebugEnabled()) {
- log.debug("Update kubernetes replication controller request body: "
- + content);
- }
-
- URI uri = new URIBuilder(baseURL+"replicationControllers/"+replicationController.getId()).build();
- KubernetesResponse res = restClient.doPut(uri, content);
-
- handleNullResponse("Could not update kubernetes replication controller: [replication-controller-id] " +
- replicationController.getId(), res);
-
- if (res.getStatusCode() != HttpStatus.SC_ACCEPTED
- && res.getStatusCode() != HttpStatus.SC_OK) {
- String message = "Could not update kubernetes replication controller: [replication-controller-id] " +
- replicationController.getId() + ": " + res.getReason();
- log.error(message);
- throw new KubernetesClientException(message);
- }
-
- } catch (KubernetesClientException e) {
- String message = "Could not update kubernetes replication controller: [replication-controller-id] " +
- replicationController.getId();
- log.error(message, e);
- throw e;
- } catch (Exception e) {
- String message = "Could not update kubernetes replication controller: [replication-controller-id] " +
- replicationController.getId();
- log.error(message, e);
- throw new KubernetesClientException(message, e);
- }
- }
-
- @Override
- public void deleteReplicationController(String replicationControllerId)
- throws KubernetesClientException {
-
- try {
- URI uri = new URIBuilder(baseURL+"replicationControllers/"+ replicationControllerId).build();
- KubernetesResponse res = restClient.doDelete(uri);
-
- handleNullResponse("Could not delete kubernetes replication controller: [replication-controller-id] " +
- replicationControllerId, res);
-
- if (res.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
- String message = "Replication controller does not exist: [replication-controller-id] "
- + replicationControllerId;
- log.error(message);
- throw new KubernetesClientException(message);
- }
-
- if (res.getStatusCode() != HttpStatus.SC_ACCEPTED &&
- res.getStatusCode() != HttpStatus.SC_OK) {
- String message = "Could not delete kubernetes replication controller: [replication-controller-id] " +
- replicationControllerId + ": " + res.getReason();
- log.error(message);
- throw new KubernetesClientException(message);
- }
- } catch (KubernetesClientException e) {
- String message = "Could not delete kubernetes replication controller: [replication-controller-id] " +
- replicationControllerId;
- log.error(message, e);
- throw e;
- } catch (Exception e) {
- String message = "Could not delete kubernetes replication controller: [replication-controller-id] " +
- replicationControllerId;
- log.error(message, e);
- throw new KubernetesClientException(message, e);
- }
- }
-
- @Override
- public Service getService(String serviceId)
- throws KubernetesClientException {
- try {
- URI uri = new URIBuilder(baseURL+"services/"+serviceId).build();
- KubernetesResponse res = restClient.doGet(uri);
-
- handleNullResponse("Service ["+serviceId+"] retrieval failed.", res);
-
- if (res.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
- String msg = "Service ["+serviceId+"] doesn't exist.";
- log.error(msg);
- throw new KubernetesClientException(msg);
- }
-
- String content = res.getContent();
-
+
+ if (response.getStatusCode() != HttpStatus.SC_ACCEPTED &&
+ response.getStatusCode() != HttpStatus.SC_OK) {
+ String message = "Could not delete replication controller: [replication-controller-id] " +
+ replicationControllerId + ": " + response.getReason();
+ log.error(message);
+ throw new KubernetesClientException(message);
+ }
+
+ if(log.isInfoEnabled()) {
+ log.info("Replication controller deleted successfully: [replication-controller-id] " +
+ replicationControllerId);
+ }
+ } catch (KubernetesClientException e) {
+ String message = "Could not delete replication controller: [replication-controller-id] " +
+ replicationControllerId;
+ log.error(message, e);
+ throw e;
+ } catch (Exception e) {
+ String message = "Could not delete replication controller: [replication-controller-id] " +
+ replicationControllerId;
+ log.error(message, e);
+ throw new KubernetesClientException(message, e);
+ }
+ }
+
+ @Override
+ public void createService(String serviceId, String serviceName, int servicePort,
+ int containerPort, String publicIp) throws KubernetesClientException {
+ try {
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Creating service: [service-id] %s [service-name] %s [service-port] %d " +
+ "[container-port] %d, [public-ip] %s", serviceId, serviceName, servicePort, containerPort, publicIp));
+ }
+
+ // Create service definition
+ Service service = new Service();
+ service.setApiVersion(KubernetesConstants.KUBERNETES_API_VERSION);
+ service.setContainerPort(String.valueOf(containerPort));
+ service.setPort(servicePort);
+ service.setId(serviceId);
+ service.setKind(KubernetesConstants.KIND_SERVICE);
+ service.setPublicIPs(new String[]{publicIp});
+
+ // Set service labels
+ Labels serviceLabels = new Labels();
+ serviceLabels.setName(serviceName);
+ service.setLabels(serviceLabels);
+ service.setName(serviceName);
+
+ // Set service selector
+ Selector selector = new Selector();
+ selector.setName(serviceLabels.getName());
+ service.setSelector(selector);
+
+ // Invoke the api to create the service
+ createService(service);
+
+ if (log.isInfoEnabled()) {
+ log.info(String.format("Service created successfully: [service-id] %s [service-name] %s [service-port] %d " +
+ "[container-port] %d, [public-ip] %s", serviceId, serviceName, servicePort, containerPort, publicIp));
+ }
+ } catch (Exception e) {
+ String message = String.format("Service created successfully: [service-id] %s [service-name] %s [service-port] %d " +
+ "[container-port] %d, [public-ip] %s", serviceId, serviceName, servicePort, containerPort, publicIp);
+ log.error(message, e);
+ throw new KubernetesClientException(message, e);
+ }
+ }
+
+ @Override
+ public Service getService(String serviceId)
+ throws KubernetesClientException {
+ try {
+ URI uri = new URIBuilder(baseURL + "services/" + serviceId).build();
+ HttpResponse response = restClient.doGet(uri);
+
+ handleNullResponse(String.format("Could not retrieve service: [service-id] %s", serviceId), response);
+
+ if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
+ return null;
+ }
+
+ String content = response.getContent();
+
GsonBuilder gsonBuilder = new GsonBuilder();
- Gson gson = gsonBuilder.create();
- return gson.fromJson(content, Service.class);
- } catch (KubernetesClientException e) {
+ Gson gson = gsonBuilder.create();
+ return gson.fromJson(content, Service.class);
+ } catch (KubernetesClientException e) {
throw e;
} catch (Exception e) {
- String msg = "Error while retrieving Service info with Service ID: "+serviceId;
- log.error(msg, e);
- throw new KubernetesClientException(msg, e);
- }
- }
-
- @Override
- public Service[] getAllServices() throws KubernetesClientException {
- try {
-
- URI uri = new URIBuilder(baseURL+"services").build();
- KubernetesResponse res = restClient.doGet(uri);
-
- handleNullResponse("Service retrieval failed.", res);
-
- if (res.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
- return new Service[0];
- }
-
- String content = res.getContent();
-
+ String msg = String.format("Could not retrieve service: [service-id] %s", serviceId);
+ log.error(msg, e);
+ throw new KubernetesClientException(msg, e);
+ }
+ }
+
+ @Override
+ public List<Service> getServices() throws KubernetesClientException {
+ try {
+
+ URI uri = new URIBuilder(baseURL + "services").build();
+ HttpResponse response = restClient.doGet(uri);
+
+ handleNullResponse("Service retrieval failed.", response);
+
+ if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
+ return null;
+ }
+
+ String content = response.getContent();
+
+ GsonBuilder gsonBuilder = new GsonBuilder();
+ Gson gson = gsonBuilder.create();
+ ServiceList result = gson.fromJson(content, ServiceList.class);
+
+ List<Service> serviceList = new ArrayList<Service>();
+ if((result != null) && (result.getItems() != null)) {
+ for(Service pod : result.getItems()) {
+ if(pod != null) {
+ serviceList.add(pod);
+ }
+ }
+ }
+ return serviceList;
+ } catch (Exception e) {
+ String msg = "Error while retrieving Services.";
+ log.error(msg, e);
+ throw new KubernetesClientException(msg, e);
+ }
+ }
+
+ private void createService(Service service) throws KubernetesClientException {
+
+ try {
GsonBuilder gsonBuilder = new GsonBuilder();
- Gson gson = gsonBuilder.create();
- ServiceList serviceList = gson.fromJson(content, ServiceList.class);
- return serviceList.getItems();
- } catch (Exception e) {
- String msg = "Error while retrieving Services.";
- log.error(msg, e);
- throw new KubernetesClientException(msg, e);
- }
- }
-
- @Override
- public void createService(Service service) throws KubernetesClientException {
-
- try {
- GsonBuilder gsonBuilder = new GsonBuilder();
- Gson gson = gsonBuilder.create();
- String content = gson.toJson(service);
- if (log.isDebugEnabled()) {
- log.debug("CreateService Request Body : "+content);
- }
-
- URI uri = new URIBuilder(baseURL+"services").build();
- KubernetesResponse res = restClient.doPost(uri, content);
-
- handleNullResponse("Service "+service+" creation failed.", res);
-
- if (res.getStatusCode() == HttpStatus.SC_CONFLICT) {
- log.warn("Service already created. "+service);
+ Gson gson = gsonBuilder.create();
+ String content = gson.toJson(service);
+ if (log.isDebugEnabled()) {
+ log.debug("CreateService Request Body : " + content);
+ }
+
+ URI uri = new URIBuilder(baseURL + "services").build();
+ HttpResponse response = restClient.doPost(uri, content);
+
+ handleNullResponse(String.format("Could not create service: [service-id] %s", service.getId()), response);
+
+ if (response.getStatusCode() == HttpStatus.SC_CONFLICT) {
+ log.warn("Service already created: [service-id] " + service.getId());
return;
}
-
- if (res.getStatusCode() != HttpStatus.SC_ACCEPTED &&
- res.getStatusCode() != HttpStatus.SC_OK) {
- String msg = "Service ["+service+"] creation failed. Error: "+
- res.getReason();
- log.error(msg);
- throw new KubernetesClientException(msg);
- }
- } catch (KubernetesClientException e) {
- throw e;
-
- } catch (Exception e) {
- String msg = "Error while creating the Service: "+service;
- log.error(msg, e);
- throw new KubernetesClientException(msg, e);
- }
- }
-
- @Override
- public void deleteService(String serviceId)
- throws KubernetesClientException {
-
- try {
- URI uri = new URIBuilder(baseURL + "services/" + serviceId).build();
- KubernetesResponse res = restClient.doDelete(uri);
-
- handleNullResponse("Service ["+serviceId+"] deletion failed.", res);
-
- if (res.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
- String msg = "Service ["+serviceId+"] doesn't exist.";
- log.error(msg);
- throw new KubernetesClientException(msg);
- }
-
- if (res.getStatusCode() != HttpStatus.SC_ACCEPTED &&
- res.getStatusCode() != HttpStatus.SC_OK) {
- String msg = "Service ["+serviceId+"] deletion failed. Error: "+
- res.getReason();
- log.error(msg);
- throw new KubernetesClientException(msg);
- }
- } catch (KubernetesClientException e) {
- throw e;
-
- } catch (Exception e) {
- String msg = "Error while deleting Service with Service ID: "+serviceId;
- log.error(msg, e);
- throw new KubernetesClientException(msg, e);
- }
- }
+
+ if (response.getStatusCode() != HttpStatus.SC_ACCEPTED &&
+ response.getStatusCode() != HttpStatus.SC_OK) {
+ String msg = String.format("Could not create service: [service-id] %s [message] %s", service.getId(),
+ response.getKubernetesResponse().getMessage());
+ log.error(msg);
+ throw new KubernetesClientException(msg);
+ }
+ } catch (KubernetesClientException e) {
+ throw e;
+
+ } catch (Exception e) {
+ String msg = "Could not create service: [service-id] " + service.getId();
+ log.error(msg, e);
+ throw new KubernetesClientException(msg, e);
+ }
+ }
@Override
- public Pod[] queryPods(Labels[] labels) throws KubernetesClientException {
-
+ public void deleteService(String serviceId)
+ throws KubernetesClientException {
+
+ try {
+ if(log.isInfoEnabled()) {
+ log.info(String.format("Deleting service: [service-id] %s", serviceId));
+ }
+
+ URI uri = new URIBuilder(baseURL + "services/" + serviceId).build();
+ HttpResponse response = restClient.doDelete(uri);
+
+ handleNullResponse(String.format("Could not delete service: [service-id] %s", serviceId), response);
+
+ if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
+ String msg = String.format("Service not found: [service-id] %s", serviceId);
+ log.warn(msg);
+ return;
+ }
+
+ if (response.getStatusCode() != HttpStatus.SC_ACCEPTED &&
+ response.getStatusCode() != HttpStatus.SC_OK) {
+ String msg = String.format("Could not delete service: [service-id] %s [message] %s", serviceId,
+ response.getKubernetesResponse().getMessage());
+ log.error(msg);
+ throw new KubernetesClientException(msg);
+ }
+
+ if(log.isInfoEnabled()) {
+ log.info(String.format("Service deleted successfully: [service-id] %s", serviceId));
+ }
+ } catch (KubernetesClientException e) {
+ throw e;
+
+ } catch (Exception e) {
+ String msg = String.format("Could not delete service: [service-id] %s", serviceId);
+ log.error(msg, e);
+ throw new KubernetesClientException(msg, e);
+ }
+ }
+
+ @Override
+ public List<Pod> queryPods(Labels[] labels) throws KubernetesClientException {
+
try {
String labelQuery = getLabelQuery(labels);
URI uri = new URIBuilder(baseURL + "pods").addParameter("labels", labelQuery).build();
- KubernetesResponse response = restClient.doGet(uri);
-
- handleNullResponse("Pod retrieval failed, kubernetes api response is null", response);
-
+ HttpResponse response = restClient.doGet(uri);
+
+ handleNullResponse(String.format("Could not retrieve pods: [labels] %s", labels), response);
+
if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
- return new Pod[0];
+ return null;
}
-
+
String content = response.getContent();
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
- PodList podList = gson.fromJson(content, PodList.class);
- if (podList == null || podList.getItems() == null) {
- return new Pod[0];
+ PodList result = gson.fromJson(content, PodList.class);
+
+ List<Pod> podList = new ArrayList<Pod>();
+ if((result != null) && (result.getItems() != null)) {
+ for(Pod pod : result.getItems()) {
+ if(pod != null) {
+ podList.add(pod);
+ }
+ }
}
- return podList.getItems();
+ return podList;
} catch (Exception e) {
- String msg = "Error while retrieving Pods.";
+ String msg = String.format("Could not retrieve pods: [labels] %s", labels);
log.error(msg, e);
throw new KubernetesClientException(msg, e);
}
@@ -502,15 +721,15 @@ public class KubernetesApiClient implements KubernetesAPIClientInterface {
private String getLabelQuery(Labels[] labels) {
String query = "";
for (Labels l : labels) {
- query = query.concat("name="+l.getName()+",");
+ query = query.concat("name=" + l.getName() + ",");
}
- return query.endsWith(",") ? query.substring(0, query.length()-1) : query;
+ return query.endsWith(",") ? query.substring(0, query.length() - 1) : query;
}
- private void handleNullResponse(String message, KubernetesResponse res)
+ private void handleNullResponse(String message, HttpResponse response)
throws KubernetesClientException {
- if (res == null) {
- log.error(message+ " Null response received");
+ if (response == null) {
+ log.error(message + ", a null response received");
throw new KubernetesClientException(message);
}
}
http://git-wip-us.apache.org/repos/asf/stratos/blob/11f9cbd1/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesConstants.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesConstants.java b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesConstants.java
new file mode 100644
index 0000000..ed796d0
--- /dev/null
+++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/KubernetesConstants.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.stratos.kubernetes.client;
+
+/**
+ * Kubernetes constants.
+ */
+public class KubernetesConstants {
+
+ public static final String POD_STATUS_RUNNING = "Running";
+ public static final String KUBERNETES_API_VERSION = "v1beta1";
+ public static final String KIND_SERVICE = "Service";
+ public static final String KIND_REPLICATION_CONTROLLER = "ReplicationController";
+ public static final String KIND_POD = "Pod";
+}
http://git-wip-us.apache.org/repos/asf/stratos/blob/11f9cbd1/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/interfaces/KubernetesAPIClientInterface.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/interfaces/KubernetesAPIClientInterface.java b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/interfaces/KubernetesAPIClientInterface.java
index 72eda49..232e1e0 100644
--- a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/interfaces/KubernetesAPIClientInterface.java
+++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/interfaces/KubernetesAPIClientInterface.java
@@ -23,10 +23,21 @@ package org.apache.stratos.kubernetes.client.interfaces;
import org.apache.stratos.kubernetes.client.exceptions.KubernetesClientException;
import org.apache.stratos.kubernetes.client.model.*;
+import java.util.List;
+
public interface KubernetesAPIClientInterface {
-
- /* Pod API */
+
+ /**
+ * Create pod.
+ * @param podId
+ * @param podName
+ * @param dockerImage
+ * @param containerPorts
+ * @throws KubernetesClientException
+ */
+ public void createPod(String podId, String podName, String dockerImage, List<Integer> containerPorts)
+ throws KubernetesClientException;
/**
* Get information of a Pod given the PodID
@@ -35,38 +46,45 @@ public interface KubernetesAPIClientInterface {
* @throws KubernetesClientException
*/
public Pod getPod(String podId) throws KubernetesClientException;
-
+
/**
* Get all Pods
* @return Pods
* @throws KubernetesClientException
*/
- public Pod[] getAllPods() throws KubernetesClientException;
-
+ public List<Pod> getPods() throws KubernetesClientException;
+
/**
* Run a label query and retrieve a sub set of Pods.
* @param labels of labels for the label query
* @return Pods selected Pods by executing the label query.
* @throws KubernetesClientException
*/
- public Pod[] queryPods(Labels[] labels) throws KubernetesClientException;
-
- /**
- * Create a new Pod
- * @param pod Pod to be created
- * @throws KubernetesClientException
- */
- public void createPod(Pod pod) throws KubernetesClientException;
-
+ public List<Pod> queryPods(Labels[] labels) throws KubernetesClientException;
+
/**
* Delete a Pod
* @param podId Id of the Pod to be deleted
* @throws KubernetesClientException
*/
public void deletePod(String podId) throws KubernetesClientException;
-
- /* Replication Controller API */
-
+
+ /**
+ * Create replication controller.
+ * @param replicationControllerId
+ * @param replicationControllerName
+ * @param dockerImage
+ * @param containerPorts
+ * @param replicas
+ * @throws KubernetesClientException
+ */
+ public void createReplicationController(String replicationControllerId,
+ String replicationControllerName,
+ String dockerImage,
+ List<Integer> containerPorts,
+ EnvironmentVariable[] environmentVariables,
+ int replicas) throws KubernetesClientException;
+
/**
* Get a Replication Controller Info
* @param controllerId id of the Replication Controller
@@ -80,15 +98,8 @@ public interface KubernetesAPIClientInterface {
* @return {@link ReplicationController}s
* @throws KubernetesClientException
*/
- public ReplicationController[] getAllReplicationControllers() throws KubernetesClientException;
-
- /**
- * Create a new Replication Controller
- * @param replicationController replication controller to be created
- * @throws KubernetesClientException
- */
- public void createReplicationController(ReplicationController replicationController) throws KubernetesClientException;
-
+ public List<ReplicationController> getReplicationControllers() throws KubernetesClientException;
+
/**
* Update a Replication Controller (update the number of replicas).
* @param replicationController replication controller to be updated
@@ -102,9 +113,19 @@ public interface KubernetesAPIClientInterface {
* @throws KubernetesClientException
*/
public void deleteReplicationController(String replicationControllerId) throws KubernetesClientException;
-
- /* Services API */
-
+
+ /**
+ * Create service.
+ * @param serviceId
+ * @param serviceName
+ * @param servicePort
+ * @param containerPort
+ * @param publicIp
+ * @throws KubernetesClientException
+ */
+ public void createService(String serviceId, String serviceName, int servicePort,
+ int containerPort, String publicIp) throws KubernetesClientException;
+
/**
* Get the Service with the given id.
* @param serviceId id of the service.
@@ -114,21 +135,14 @@ public interface KubernetesAPIClientInterface {
public Service getService(String serviceId) throws KubernetesClientException;
/**
- * Get all the services.
+ * Get services.
* @return array of {@link Service}s
* @throws KubernetesClientException
*/
- public Service[] getAllServices() throws KubernetesClientException;
-
- /**
- * Create a new Kubernetes service.
- * @param service service to be created.
- * @throws KubernetesClientException
- */
- public void createService(Service service) throws KubernetesClientException;
+ public List<Service> getServices() throws KubernetesClientException;
/**
- * Delete a Service.
+ * Delete a service.
* @param serviceId service id to be deleted.
* @throws KubernetesClientException
*/
http://git-wip-us.apache.org/repos/asf/stratos/blob/11f9cbd1/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/model/Container.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/model/Container.java b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/model/Container.java
index 2496523..0501539 100644
--- a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/model/Container.java
+++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/model/Container.java
@@ -20,7 +20,9 @@
*/
package org.apache.stratos.kubernetes.client.model;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
@@ -31,9 +33,13 @@ public class Container {
private String workingDir;
private String[] command;
private VolumeMount[] volumeMounts;
- private Port[] ports;
+ private List<Port> ports;
private EnvironmentVariable[] env;
-
+
+ public Container() {
+ ports = new ArrayList<Port>();
+ }
+
public String getName() {
return name;
}
@@ -74,12 +80,16 @@ public class Container {
this.volumeMounts = ArrayUtils.clone(volumeMounts);
}
- public Port[] getPorts() {
+ public List<Port> getPorts() {
return ports;
}
- public void setPorts(Port[] ports) {
- this.ports = ArrayUtils.clone(ports);
+ public void addPort(Port port) {
+ this.ports.add(port);
+ }
+
+ public void setPorts(List<Port> ports) {
+ this.ports = ports;
}
public EnvironmentVariable[] getEnv() {
@@ -95,7 +105,7 @@ public class Container {
return "Container [name=" + name + ", image=" + image + ", workingDir="
+ workingDir + ", command=" + Arrays.toString(command)
+ ", volumeMounts=" + Arrays.toString(volumeMounts)
- + ", ports=" + Arrays.toString(ports) + ", env="
+ + ", ports=" + ports + ", env="
+ Arrays.toString(env) + "]";
}
}
http://git-wip-us.apache.org/repos/asf/stratos/blob/11f9cbd1/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/model/Pod.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/model/Pod.java b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/model/Pod.java
index a875105..4547db5 100644
--- a/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/model/Pod.java
+++ b/components/org.apache.stratos.kubernetes.client/src/main/java/org/apache/stratos/kubernetes/client/model/Pod.java
@@ -79,7 +79,7 @@ public class Pod {
return desiredState;
}
- public void setState(State desiredState) {
+ public void setDesiredState(State desiredState) {
this.desiredState = desiredState;
}