fixed log4j issue
[eliot.git] / blueprints / common / eliot-ui / be / src / eliotk8sclient / src / main / java / com / eliot / eliotbe / eliotk8sclient / deploy / kubernetes / KubernetesDeploy.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.eliot.eliotbe.eliotk8sclient.deploy.GenericDeploy;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
22 import io.kubernetes.client.models.*;
23 import io.kubernetes.client.util.Yaml;
24 import org.jose4j.json.internal.json_simple.JSONObject;
25 import org.jose4j.json.internal.json_simple.parser.JSONParser;
26 import org.jose4j.json.internal.json_simple.parser.ParseException;
27 import com.eliot.eliotbe.eliotk8sclient.common.AppConstants;
28 import com.eliot.eliotbe.eliotk8sclient.deploy.GenericDeploy;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.util.StringUtils;
32
33 import java.io.File;
34 import java.io.FileNotFoundException;
35 import java.io.IOException;
36 import java.nio.file.Path;
37 import java.util.*;
38
39 public class KubernetesDeploy extends GenericDeploy {
40     private static final Logger logger = LoggerFactory.getLogger(KubernetesDeploy.class);
41
42     private static final Map<String,Class<?>> kindClassMap = new HashMap<String, Class<?>>() {
43         private static final long serialVersionUID = 3033314593899228898L;
44         {
45             put("Pod",V1Pod.class);
46             put("Deployment",V1Deployment.class);
47             put("PersistentVolume",V1PersistentVolume.class);
48             put("PersistentVolumeClaim",V1PersistentVolumeClaim.class);
49             put("Service",V1Service.class);
50         }
51     };
52     /**
53      * deploy.
54      * @param packagepath packagepath
55      * @return
56      */
57     public String deploy(String dstYamlFilePath) {
58         //String dstYamlFilePath = findDeploymentYamlPath(packagepath);
59         if (StringUtils.isEmpty(dstYamlFilePath)) {
60             logger.error("not found deploy yaml file.");
61             return AppConstants.EMPTY_STRING;
62         }
63         String yamlContent = readYamlFile(dstYamlFilePath);
64         String yamlStr = null;
65         try {
66             logger.info("before kube config installation ");
67             yamlStr = deployByYaml(yamlContent,dstYamlFilePath);
68         } catch (IOException e) {
69             logger.error("invoke method deployYaml fail : {}",e.getMessage());
70             return AppConstants.EMPTY_STRING;
71         }
72         return yamlStr;
73     }
74
75      private String findDeploymentYamlPath(String packagepath) {
76         List<String> result = new ArrayList<>();
77         search(".*\\.yaml", new File(packagepath), result);
78         String dstYamlFilePath = null;
79         for (String path : result) {
80             if (path.contains("Deployment")) {
81                 dstYamlFilePath = path;
82             }
83         }
84         return dstYamlFilePath;
85     }
86
87     protected String readYamlFile(String pathname) {
88         File file = new File(pathname);
89         StringBuilder fileContents = new StringBuilder((int) file.length());
90         Scanner scanner;
91         try {
92             scanner = new Scanner(file, "UTF-8");
93         } catch (FileNotFoundException e) {
94             logger.error("read yaml file fail : {}",e.getMessage());
95             return "";
96         }
97
98         String lineSeparator = System.getProperty("line.separator");
99         try {
100             while (scanner.hasNextLine()) {
101                 fileContents.append(scanner.nextLine() + lineSeparator);
102             }
103             return fileContents.toString();
104         } finally {
105             scanner.close();
106         }
107     }
108
109     protected void addModelMap(String apiVersion, String kind) {
110         if (kindClassMap.containsKey(kind)) {
111             Yaml.addModelMap(apiVersion, kind, kindClassMap.get(kind));
112             logger.info("after addModelMap for {}",kindClassMap.get(kind).getName());
113             return;
114         }
115         logger.error("not found kind : {}",kind);
116     }
117
118     private String deployByYaml(String yamlContent, String dstYamlFilePath) throws IOException {
119         logger.info("Inside updateDeploymentYaml to install pod and service");
120         ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
121         for (String ret : yamlContent.split("---")) {
122             if (StringUtils.isEmpty(ret)) {
123                 continue;
124             }
125             Object obj = yamlReader.readValue(ret, Object.class);
126             String data = new ObjectMapper().writeValueAsString(obj);
127             try {
128                 JSONObject json = (JSONObject) new JSONParser().parse(data);
129                 if (json.get("kind") == null) {
130                     continue;
131                 }
132                 String kind = json.get("kind").toString();
133                 String apiVersion = json.get("apiVersion").toString();
134                 addModelMap(apiVersion,kind);
135             } catch (ParseException e) {
136                 logger.error("parse data fail : {}",e.getMessage());
137             }
138         }
139         IDeploy deployment = DeployFactory.create();
140         return deployment.deploy(dstYamlFilePath);
141     }
142 }