From a368937411e8767bc19f50a7968567fb68a6dfeb Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Tue, 13 Sep 2022 15:54:10 +0100 Subject: Fix PodStatus Validator failing in K8sParticipant Fix PodStatus Validator failing in K8sParticipant using service template for Test and Verification of ACM State Management. Issue-ID: POLICY-4355 Change-Id: I63f8ed2c4991422dd43749151387ff54ba7d6071 Signed-off-by: FrancescoFioraEst --- .../kubernetes/helm/PodStatusValidator.java | 65 ++++++++++++---------- .../participant/kubernetes/models/ChartInfo.java | 2 + 2 files changed, 38 insertions(+), 29 deletions(-) (limited to 'participant/participant-impl/participant-impl-kubernetes/src/main') diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java index 8267bda13..67bdc0b40 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/helm/PodStatusValidator.java @@ -1,6 +1,6 @@ /*- * ========================LICENSE_START================================= - * Copyright (C) 2021 Nordix Foundation. All rights reserved. + * Copyright (C) 2021-2022 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. @@ -27,6 +27,7 @@ import java.util.HashMap; import java.util.Map; import lombok.SneakyThrows; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException; import org.onap.policy.clamp.acm.participant.kubernetes.handler.AutomationCompositionElementHandler; import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo; @@ -62,58 +63,64 @@ public class PodStatusValidator implements Runnable { @Override public void run() { logger.info("Polling the status of deployed pods for the chart {}", chart.getChartId().getName()); - Map podStatusMap; - String output = null; + + try { + verifyPodStatus(); + } catch (ServiceException | IOException e) { + throw new ServiceException("Error verifying the status of the pod. Exiting", e); + } + } + + private void verifyPodStatus() throws ServiceException, IOException, InterruptedException { 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)); - AutomationCompositionElementHandler.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()); - AutomationCompositionElementHandler.getPodStatusMap().put(chart.getReleaseName(), podStatusMap); - } - } catch (ServiceException | IOException e) { - throw new ServiceException("Error verifying the status of the pod. Exiting", e); + var output = HelmClient.executeCommand(verifyPodStatusCommand(chart)); + var podStatusMap = mapPodStatus(output); + isVerified = !podStatusMap.isEmpty() + && 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)); + // 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()); + AutomationCompositionElementHandler.getPodStatusMap().put(chart.getReleaseName(), podStatusMap); } } + if (!isVerified) { + throw new ServiceException("Time out Exception verifying the status of the pod"); + } } private ProcessBuilder verifyPodStatusCommand(ChartInfo chart) { - String cmd = "kubectl get pods --namespace " + chart.getNamespace() + " | grep " - + chart.getChartId().getName(); + String cmd = "kubectl get pods --namespace " + chart.getNamespace() + " | grep " + getPodName(); return new ProcessBuilder("sh", "-c", cmd); } + private String getPodName() { + return StringUtils.isNotEmpty(chart.getPodName()) ? chart.getPodName() : chart.getChartId().getName(); + } - private Map mapPodStatus(String output) throws IOException, ServiceException { + private Map mapPodStatus(String output) throws IOException { Map podStatusMap = new HashMap<>(); + var podName = getPodName(); 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())) { + if (line.contains(podName)) { 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"); + if (podStatusMap.isEmpty()) { + logger.warn("Status of Pod {} is empty", podName); } + return podStatusMap; } } diff --git a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartInfo.java b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartInfo.java index b925e782d..e2c4c2e11 100644 --- a/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartInfo.java +++ b/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/models/ChartInfo.java @@ -34,6 +34,8 @@ public class ChartInfo { @NonNull private ToscaConceptIdentifier chartId; + private String podName; + @NonNull private String namespace; -- cgit 1.2.3-korg