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