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.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;
31 import java.io.IOException;
32 import java.util.List;
34 import java.util.Objects;
36 class PodServiceDeploy implements IDeploy {
37 private static final Logger logger = LoggerFactory.getLogger(com.eliot.eliotbe.eliotk8sclient.deploy.kubernetes.PodServiceDeploy.class);
39 private static final Gson gson = new Gson();
41 * KubernetesDeployment constructor.
43 public PodServiceDeploy() {
48 * create namespace pod.
49 * @param dstYamlFilePath dstYamlFilePath
53 public String deploy(String dstYamlFilePath) {
54 String nameSpace = "";
55 logger.info("-------------Kubernetes Deployment via YAML------------");
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;
64 for (Object object : list) {
65 String type = object.getClass().getSimpleName();
68 nameSpace = handlePodCreateRequest(object);
71 nameSpace = handleServiceCreateRequest(object);
77 logger.info("response with both pod and service: {}",nameSpace);
81 private String handleServiceCreateRequest(Object object) {
82 logger.info("SERVICE create request");
83 CoreV1Api v1service = new CoreV1Api();
84 V1Service serviceResult;
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());
98 private String handlePodCreateRequest(Object object) {
99 logger.info("POD create request");
100 CoreV1Api v1pod = new CoreV1Api();
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());