2 * ========================LICENSE_START=================================
3 * Copyright (C) 2021-2022 Nordix Foundation. All rights reserved.
4 * ======================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ========================LICENSE_END===================================
19 package org.onap.policy.clamp.acm.participant.kubernetes.helm;
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.lang.invoke.MethodHandles;
25 import java.nio.charset.StandardCharsets;
26 import java.util.HashMap;
28 import lombok.SneakyThrows;
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
32 import org.onap.policy.clamp.acm.participant.kubernetes.handler.AutomationCompositionElementHandler;
33 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 public class PodStatusValidator implements Runnable {
40 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
42 private final int statusCheckInterval;
44 //Timeout for the thread to exit.
45 private final int timeout;
47 private ChartInfo chart;
50 * Constructor for PodStatusValidator.
51 * @param chart chartInfo
52 * @param timeout timeout for the thread to exit
53 * @param statusCheckInterval Interval to check pod status
55 public PodStatusValidator(ChartInfo chart, int timeout, int statusCheckInterval) {
57 this.timeout = timeout;
58 this.statusCheckInterval = statusCheckInterval;
65 logger.info("Polling the status of deployed pods for the chart {}", chart.getChartId().getName());
69 } catch (ServiceException | IOException e) {
70 throw new ServiceException("Error verifying the status of the pod. Exiting", e);
74 private void verifyPodStatus() throws ServiceException, IOException, InterruptedException {
75 var isVerified = false;
76 long endTime = System.currentTimeMillis() + (timeout * 1000L);
78 while (!isVerified && System.currentTimeMillis() < endTime) {
79 var output = HelmClient.executeCommand(verifyPodStatusCommand(chart));
80 var podStatusMap = mapPodStatus(output);
81 isVerified = !podStatusMap.isEmpty()
82 && podStatusMap.values().stream().allMatch("Running"::equals);
84 logger.info("Waiting for the pods to be active for the chart {}", chart.getChartId().getName());
85 podStatusMap.forEach((key, value) -> logger.info("Pod: {} , state: {}", key, value));
86 // Recheck status of pods in specific intervals.
87 Thread.sleep(statusCheckInterval * 1000L);
89 logger.info("All pods are in running state for the helm chart {}", chart.getChartId().getName());
90 AutomationCompositionElementHandler.getPodStatusMap().put(chart.getReleaseName(), podStatusMap);
94 throw new ServiceException("Time out Exception verifying the status of the pod");
98 private ProcessBuilder verifyPodStatusCommand(ChartInfo chart) {
99 String cmd = "kubectl get pods --namespace " + chart.getNamespace() + " | grep " + getPodName();
100 return new ProcessBuilder("sh", "-c", cmd);
103 private String getPodName() {
104 return StringUtils.isNotEmpty(chart.getPodName()) ? chart.getPodName() : chart.getChartId().getName();
107 private Map<String, String> mapPodStatus(String output) throws IOException {
108 Map<String, String> podStatusMap = new HashMap<>();
109 var podName = getPodName();
110 try (var reader = new BufferedReader(new InputStreamReader(IOUtils.toInputStream(output,
111 StandardCharsets.UTF_8)))) {
112 var line = reader.readLine();
113 while (line != null) {
114 if (line.contains(podName)) {
115 var result = line.split("\\s+");
116 podStatusMap.put(result[0], result[2]);
118 line = reader.readLine();
121 if (podStatusMap.isEmpty()) {
122 logger.warn("Status of Pod {} is empty", podName);