2 * Copyright 2020 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.eliot.eliotbe.eliotk8sclient.deploy.kubernetes;
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;
34 import java.io.FileNotFoundException;
35 import java.io.IOException;
36 import java.nio.file.Path;
39 public class KubernetesDeploy extends GenericDeploy {
40 private static final Logger logger = LoggerFactory.getLogger(KubernetesDeploy.class);
42 private static final Map<String,Class<?>> kindClassMap = new HashMap<String, Class<?>>() {
43 private static final long serialVersionUID = 3033314593899228898L;
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);
54 * @param packagepath packagepath
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;
63 String yamlContent = readYamlFile(dstYamlFilePath);
64 String yamlStr = null;
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;
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;
84 return dstYamlFilePath;
87 protected String readYamlFile(String pathname) {
88 File file = new File(pathname);
89 StringBuilder fileContents = new StringBuilder((int) file.length());
92 scanner = new Scanner(file, "UTF-8");
93 } catch (FileNotFoundException e) {
94 logger.error("read yaml file fail : {}",e.getMessage());
98 String lineSeparator = System.getProperty("line.separator");
100 while (scanner.hasNextLine()) {
101 fileContents.append(scanner.nextLine() + lineSeparator);
103 return fileContents.toString();
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());
115 logger.error("not found kind : {}",kind);
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)) {
125 Object obj = yamlReader.readValue(ret, Object.class);
126 String data = new ObjectMapper().writeValueAsString(obj);
128 JSONObject json = (JSONObject) new JSONParser().parse(data);
129 if (json.get("kind") == null) {
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());
139 IDeploy deployment = DeployFactory.create();
140 return deployment.deploy(dstYamlFilePath);