fbe9fecbb9db5bfc78508308d1acf199825e34ac
[eliot.git] / blueprints / common / eliot-ui / be / src / eliotk8sclient / src / main / java / com / eliot / eliotbe / eliotk8sclient / deploy / kubernetes / PodServiceDeploy.java
1 /*
2  * Copyright 2020 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.eliot.eliotbe.eliotk8sclient.deploy.kubernetes;
18
19 import com.google.gson.Gson;
20 import io.kubernetes.client.ApiException;
21 import io.kubernetes.client.apis.CoreV1Api;
22 import io.kubernetes.client.models.V1Pod;
23 import io.kubernetes.client.models.V1Service;
24 import io.kubernetes.client.util.Yaml;
25 import com.eliot.eliotbe.eliotk8sclient.common.AppConstants;
26 import com.eliot.eliotbe.eliotk8sclient.AppException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Objects;
35
36 class PodServiceDeploy implements IDeploy {
37     private static final Logger logger = LoggerFactory.getLogger(com.eliot.eliotbe.eliotk8sclient.deploy.kubernetes.PodServiceDeploy.class);
38
39     private static final Gson gson = new Gson();
40      /**
41      * KubernetesDeployment constructor.
42      */
43     public PodServiceDeploy() {
44
45     }
46
47     /**
48      * create namespace pod.
49      * @param dstYamlFilePath dstYamlFilePath
50      * @return
51      */
52     @Override
53     public String deploy(String dstYamlFilePath) {
54         String nameSpace = "";
55         logger.info("-------------Kubernetes Deployment via YAML------------");
56         List<Object> list;
57         try {
58             list = Yaml.loadAll(new File(dstYamlFilePath));
59         } catch (IOException e) {
60             logger.error("load yaml file fail : {}",e.getMessage());
61             return AppConstants.EMPTY_STRING;
62         }
63
64         for (Object object : list) {
65             String type = object.getClass().getSimpleName();
66             switch (type) {
67                 case "V1Pod":
68                     nameSpace = handlePodCreateRequest(object);
69                     break;
70                 case "V1Service":
71                     nameSpace = handleServiceCreateRequest(object);
72                     break;
73                 default:
74                     break;
75             }
76         }
77         logger.info("response with both pod and service: {}",nameSpace);
78         return nameSpace;
79     }
80
81     private String handleServiceCreateRequest(Object object) {
82         logger.info("SERVICE create request");
83         CoreV1Api v1service = new CoreV1Api();
84         V1Service serviceResult;
85         try {
86             serviceResult = v1service
87                 .createNamespacedService("default", (V1Service) object, null, null, null);
88             logger.info("After createNamespacedService call with result: {}", serviceResult);
89             StringBuilder nameSpace = new StringBuilder("").append(Objects.requireNonNull(serviceResult.getMetadata())
90                 .getName()).append(AppConstants.NAMEANDNAMESPACE).append(serviceResult.getMetadata()
91                 .getNamespace()).append(AppConstants.POD_TYPE).append("Service").append(AppConstants.K8SPOD);
92             return nameSpace.toString();
93         } catch (ApiException e) {
94             throw new AppException(e.getMessage());
95         }
96     }
97
98     private String handlePodCreateRequest(Object object) {
99         logger.info("POD create request");
100         CoreV1Api v1pod = new CoreV1Api();
101         V1Pod createResult;
102         try {
103             createResult = v1pod.createNamespacedPod("default", (V1Pod) object, null, null, null);
104             logger.info("After createNamespacedPod call with result: {}", createResult);
105             StringBuilder nameSpace = new StringBuilder("").append(Objects.requireNonNull(createResult.getMetadata())
106                 .getName()).append(AppConstants.NAMEANDNAMESPACE).append(createResult.getMetadata().getNamespace())
107                 .append(AppConstants.POD_TYPE).append("Pod").append(AppConstants.K8SPOD);
108             return nameSpace.toString();
109         } catch (ApiException e) {
110             throw new AppException(e.getMessage());
111         }
112     }
113 }