summaryrefslogtreecommitdiffstats
path: root/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/extclients/helm/HelmClientImpl.java
blob: b43bda6fea15244f94c67f714828134b548be778 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.so.cnfm.lcm.bpmn.flows.extclients.helm;

import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_DAEMON_SET;
import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_DEPLOYMENT;
import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_JOB;
import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_POD;
import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_REPLICA_SET;
import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_SERVICE;
import static org.onap.so.cnfm.lcm.bpmn.flows.Constants.KIND_STATEFUL_SET;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jvnet.jaxb2_commons.lang.StringUtils;
import org.onap.so.cnfm.lcm.bpmn.flows.exceptions.HelmClientExecuteException;
import org.onap.so.cnfm.lcm.bpmn.flows.utils.PropertiesToYamlConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HelmClientImpl implements HelmClient {
    private static final String DEFAULT_NAMESPACE = "default";
    private static final String KIND_KEY = "kind: ";
    private static final String ANY_UNICODE_NEWLINE = "\\R";
    private static final Logger logger = LoggerFactory.getLogger(HelmClientImpl.class);
    private final PropertiesToYamlConverter propertiesToYamlConverter;

    @Autowired
    public HelmClientImpl(final PropertiesToYamlConverter propertiesToYamlConverter) {
        this.propertiesToYamlConverter = propertiesToYamlConverter;
    }

    private static final Set<String> SUPPORTED_KINDS = Set.of(KIND_JOB, KIND_POD, KIND_SERVICE, KIND_DEPLOYMENT,
            KIND_REPLICA_SET, KIND_DAEMON_SET, KIND_STATEFUL_SET);

    /**
     * Execute a helm install dry run
     *
     * @param releaseName Name of the release given to helm install
     * @param kubeconfig kubernetes configuration file path
     * @param helmChart path of the helm chart to install
     *
     * @throws HelmClientExecuteException when exception occurs on executing command
     */
    @Override
    public void runHelmChartInstallWithDryRunFlag(final String releaseName, final Path kubeconfig, final Path helmChart)
            throws HelmClientExecuteException {
        logger.info("Running dry-run on {} to cluster {} using releaseName: {}", helmChart, kubeconfig, releaseName);
        final ProcessBuilder processBuilder = prepareDryRunCommand(releaseName, kubeconfig, helmChart);
        executeCommand(processBuilder);
        logger.info("Successfully ran dry for Chart {}", helmChart);

    }

    /**
     *
     * @param releaseName Name of the release given to helm install
     * @param kubeconfig kubernetes configuration file path
     * @param helmChart path of the helm chart to install
     *
     * @return Resources for helmChart as a List of strings
     */
    @Override
    public List<String> getKubeKinds(final String releaseName, final Path kubeconfig, final Path helmChart) {
        logger.info("Retrieving kinds from chart {} using releaseName {}", helmChart, releaseName);
        final ProcessBuilder processBuilder = prepareKubeKindCommand(releaseName, kubeconfig, helmChart);
        final String response = executeCommand(processBuilder);
        if (StringUtils.isEmpty(response)) {
            logger.warn("Response is empty: {}", response);
            return Collections.emptyList();
        }
        final List<String> kinds = processKinds(response);

        logger.debug("Found kinds: {}", kinds);
        return kinds;
    }


    @Override
    public List<String> getKubeKindsUsingManifestCommand(final String releaseName, final Path kubeConfig)
            throws HelmClientExecuteException {
        logger.info("Retrieving kinds from helm release history using releaseName {}", releaseName);

        final ProcessBuilder processBuilder = prepareGetKubeKindCommand(releaseName, kubeConfig);
        final String response = executeCommand(processBuilder);
        if (StringUtils.isEmpty(response)) {
            logger.warn("Response is empty: {}", response);
            return Collections.emptyList();
        }
        final List<String> kinds = processKinds(response);

        logger.debug("Kinds found from the helm release history: {}", kinds);
        return kinds;
    }


    /**
     *
     * @param releaseName Name of the release given to helm install
     * @param kubeconfig kubernetes configuration file path
     * @param helmChart path of the helm chart to install
     * @throws HelmClientExecuteException when exception occurs on executing command
     */
    @Override
    public void installHelmChart(final String releaseName, final Path kubeconfig, final Path helmChart,
            final Map<String, String> lifeCycleParams) throws HelmClientExecuteException {
        logger.info("Installing {} to cluster {} using releaseName: {}", helmChart, kubeconfig, releaseName);
        final ProcessBuilder processBuilder =
                prepareInstallCommand(releaseName, kubeconfig, helmChart, lifeCycleParams);
        executeCommand(processBuilder);
        logger.info("Chart {} installed successfully", helmChart);

    }

    /**
     * @param releaseName Name of the release given to helm install
     * @param kubeConfigFilePath kubernetes configuration file path
     * @throws HelmClientExecuteException when exception occurs on executing command
     */
    @Override
    public void unInstallHelmChart(final String releaseName, final Path kubeConfigFilePath)
            throws HelmClientExecuteException {
        logger.info("uninstalling the release {} from cluster {}", releaseName, kubeConfigFilePath);
        final ProcessBuilder processBuilder = prepareUnInstallCommand(releaseName, kubeConfigFilePath);
        final String commandResponse = executeCommand(processBuilder);
        if (!StringUtils.isEmpty(commandResponse) && commandResponse.contains("Release not loaded")) {
            throw new HelmClientExecuteException(
                    "Unable to find the installed Helm chart by using releaseName: " + releaseName);
        }

        logger.info("Release {} uninstalled successfully", releaseName);
    }

    private ProcessBuilder prepareDryRunCommand(final String releaseName, final Path kubeconfig, final Path helmChart) {
        final List<String> helmArguments = List.of("helm", "install", releaseName, "-n", DEFAULT_NAMESPACE,
                helmChart.toString(), "--dry-run", "--kubeconfig", kubeconfig.toString());
        return getProcessBuilder().command(helmArguments);
    }

    private ProcessBuilder prepareInstallCommand(final String releaseName, final Path kubeconfig, final Path helmChart,
            final Map<String, String> lifeCycleParams) {
        final List<String> commands = new ArrayList<>(List.of("helm", "install", releaseName, "-n", DEFAULT_NAMESPACE,
                helmChart.toString(), "--kubeconfig", kubeconfig.toString()));

        if (lifeCycleParams != null && !lifeCycleParams.isEmpty()) {
            final String fileName = helmChart.getParent().resolve("values.yaml").toString();
            createYamlFile(fileName, lifeCycleParams);
            commands.add("-f ".concat(fileName));
        }
        final List<String> helmArguments = List.of("sh", "-c", toString(commands));
        return getProcessBuilder().command(helmArguments);
    }

    private void createYamlFile(final String fileName, final Map<String, String> lifeCycleParams) {
        logger.debug("Will create the runtime values.yaml file.");
        final String yamlContent = propertiesToYamlConverter.getValuesYamlFileContent(lifeCycleParams);
        logger.debug("Yaml file content : {}", yamlContent);
        try {
            Files.write(Paths.get(fileName), yamlContent.getBytes());
        } catch (final IOException ioException) {
            throw new HelmClientExecuteException(
                    "Failed to create the run time life cycle yaml file: {} " + ioException.getMessage(), ioException);
        }
    }

    private ProcessBuilder prepareUnInstallCommand(final String releaseName, final Path kubeConfig) {
        logger.debug("Will remove tis log after checking ubeconfig path: {}", kubeConfig.toFile().getName());
        final List<String> helmArguments = new ArrayList<>(List.of("helm", "uninstall", releaseName, "-n",
                DEFAULT_NAMESPACE, "--kubeconfig", kubeConfig.toString()));
        return getProcessBuilder().command(helmArguments);
    }

    private ProcessBuilder prepareKubeKindCommand(final String releaseName, final Path kubeconfig,
            final Path helmChart) {
        final List<String> commands =
                List.of("helm", "template", releaseName, "-n", DEFAULT_NAMESPACE, helmChart.toString(), "--dry-run",
                        "--kubeconfig", kubeconfig.toString(), "--skip-tests", "| grep kind | uniq");
        final List<String> helmArguments = List.of("sh", "-c", toString(commands));
        return getProcessBuilder().command(helmArguments);
    }

    private ProcessBuilder prepareGetKubeKindCommand(final String releaseName, final Path kubeconfig) {
        final List<String> commands = List.of("helm", "get", "manifest", releaseName, "-n", DEFAULT_NAMESPACE,
                "--kubeconfig", kubeconfig.toString(), "| grep kind | uniq");
        final List<String> helmArguments = List.of("sh", "-c", toString(commands));
        return getProcessBuilder().command(helmArguments);
    }

    private String executeCommand(final ProcessBuilder processBuilder) throws HelmClientExecuteException {
        final String commandStr = toString(processBuilder);

        try {
            logger.debug("Executing cmd: {}", commandStr);
            final Process process = processBuilder.start();

            final InputStreamConsumer errors = new InputStreamConsumer(process.getErrorStream());
            final InputStreamConsumer output = new InputStreamConsumer(process.getInputStream());

            final Thread errorsConsumer = new Thread(errors);
            final Thread outputConsumer = new Thread(output);
            errorsConsumer.start();
            outputConsumer.start();

            process.waitFor();

            errorsConsumer.join();
            outputConsumer.join();

            final int exitValue = process.exitValue();
            if (exitValue != 0) {
                final String stderr = errors.getContent();
                if (!stderr.isEmpty()) {
                    throw new HelmClientExecuteException("Command execution failed: " + commandStr + " " + stderr);
                }
            }

            final String stdout = output.getContent();
            logger.debug("Command <{}> execution, output: {}", commandStr, stdout);
            return stdout;

        } catch (final InterruptedException interruptedException) {
            Thread.currentThread().interrupt();
            throw new HelmClientExecuteException(
                    "Failed to execute the Command: " + commandStr + ", the command was interrupted",
                    interruptedException);
        } catch (final Exception exception) {
            throw new HelmClientExecuteException("Failed to execute the Command: " + commandStr, exception);
        }
    }

    private List<String> processKinds(final String response) {

        logger.debug("Processing kube kinds");

        final List<String> kinds = new ArrayList<>();
        for (final String entry : response.split(ANY_UNICODE_NEWLINE)) {
            if (entry != null) {
                final String line = entry.trim();
                if (!line.isBlank()) {
                    final String kind = line.replace(KIND_KEY, "").trim();
                    if (SUPPORTED_KINDS.contains(kind)) {
                        logger.debug("Found Supported kind: {}", kind);
                        kinds.add(kind);
                    } else {
                        logger.warn("kind: {} is not currently supported", kind);
                    }
                }
            }
        }
        return kinds;
    }

    private String toString(final ProcessBuilder processBuilder) {
        return String.join(" ", processBuilder.command());
    }

    private String toString(final List<String> commands) {
        return String.join(" ", commands);
    }

    ProcessBuilder getProcessBuilder() {
        return new ProcessBuilder();
    }

}