2 * ========================LICENSE_START=================================
3 * Copyright (C) 2021 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.controlloop.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.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29 import org.apache.commons.io.IOUtils;
30 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
31 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
32 import org.onap.policy.clamp.controlloop.participant.kubernetes.service.ChartStore;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
39 * Client to talk with Helm cli. Supports helm3 + version
42 public class HelmClient {
45 private ChartStore chartStore;
47 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
52 * @param chart name and version.
53 * @throws ServiceException incase of error
55 public void installChart(ChartInfo chart) throws ServiceException {
56 var processBuilder = prepareCreateNamespaceCommand(chart.getNamespace());
58 executeCommand(processBuilder);
59 } catch (ServiceException e) {
60 logger.warn("Namespace not created", e);
62 processBuilder = prepareInstallCommand(chart);
63 logger.info("Installing helm chart {} from the repository {} ", chart.getChartName(), chart.getRepository());
64 executeCommand(processBuilder);
65 logger.info("Chart {} installed successfully", chart.getChartName());
69 * Finds helm chart repository for the chart.
71 * @param chart ChartInfo.
72 * @return the chart repository as a string
73 * @throws ServiceException in case of error
74 * @throws IOException in case of IO errors
76 public String findChartRepository(ChartInfo chart) throws ServiceException, IOException {
78 logger.info("Looking for helm chart {} in all the configured helm repositories", chart.getChartName());
79 String repository = null;
81 var process = helmRepoVerifyCommand(chart.getChartName()).start();
83 try (var reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
84 String line = reader.readLine();
85 while (line != null) {
86 if (line.contains(chart.getChartName())) {
87 repository = line.split("/")[0];
88 logger.info("Helm chart located in the repository {} ", repository);
91 line = reader.readLine();
95 var localHelmChartDir = chartStore.getAppPath(chart.getChartName(), chart.getVersion()).toString();
96 logger.info("Chart not found in helm repositories, verifying local repo {} ", localHelmChartDir);
97 if (verifyLocalHelmRepo(localHelmChartDir + "/" + chart.getChartName())) {
98 repository = localHelmChartDir;
107 * @param chart name and version.
108 * @throws ServiceException incase of error
110 public void uninstallChart(ChartInfo chart) throws ServiceException {
111 executeCommand(prepareUnInstallCommand(chart));
114 static String executeCommand(ProcessBuilder processBuilder) throws ServiceException {
115 var commandStr = toString(processBuilder);
118 var process = processBuilder.start();
120 int exitValue = process.exitValue();
122 if (exitValue != 0) {
123 var error = IOUtils.toString(process.getErrorStream(), StandardCharsets.UTF_8);
124 throw new ServiceException("Command execution failed: " + commandStr + " " + error);
126 var output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8);
127 logger.debug("Command <{}> execution, output: {}", commandStr, output);
129 } catch (InterruptedException ie) {
130 Thread.currentThread().interrupt();
131 throw new ServiceException("Failed to execute the Command: " + commandStr + ", the command was interrupted",
133 } catch (Exception exc) {
134 throw new ServiceException("Failed to execute the Command: " + commandStr, exc);
138 private ProcessBuilder prepareInstallCommand(ChartInfo chart) {
141 List<String> helmArguments = new ArrayList<>(
144 "install", chart.getReleaseName(), chart.getRepository() + "/" + chart.getChartName(),
145 "--version", chart.getVersion(),
146 "--namespace", chart.getNamespace()
151 // Verify if values.yaml available for the chart
152 var overrideFile = chartStore.getOverrideFile(chart).getPath();
153 if (verifyLocalHelmRepo(overrideFile)) {
154 logger.info("Override yaml file available for the helm chart");
155 helmArguments.addAll(Arrays.asList("--values", overrideFile));
158 return new ProcessBuilder().command(helmArguments);
161 private ProcessBuilder prepareUnInstallCommand(ChartInfo chart) {
162 return new ProcessBuilder("helm", "delete", chart.getReleaseName(), "--namespace", chart.getNamespace());
165 private ProcessBuilder prepareCreateNamespaceCommand(String namespace) {
166 return new ProcessBuilder().command("kubectl", "create", "namespace", namespace);
169 private ProcessBuilder helmRepoVerifyCommand(String chartName) {
170 return new ProcessBuilder().command("bash", "-c", "helm search repo | grep " + chartName);
173 private ProcessBuilder localRepoVerifyCommand(String localFile) {
174 return new ProcessBuilder().command("bash", "-c", "ls " + localFile);
177 private void updateHelmRepo() throws ServiceException {
178 logger.info("Updating local helm repositories before verifying the chart");
179 List<String> helmArguments = Arrays.asList("helm", "repo", "update");
181 executeCommand(new ProcessBuilder().command(helmArguments));
182 logger.debug("Helm repositories updated successfully");
185 private boolean verifyLocalHelmRepo(String localFile) {
186 var isVerified = false;
187 var processBuilder = localRepoVerifyCommand(localFile);
189 executeCommand(processBuilder);
191 } catch (ServiceException e) {
192 logger.error("Unable to verify file in local repository", e);
197 protected static String toString(ProcessBuilder processBuilder) {
198 return String.join(" ", processBuilder.command());