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.service;
21 import java.io.IOException;
22 import java.lang.invoke.MethodHandles;
23 import java.util.Collection;
24 import org.onap.policy.clamp.controlloop.participant.kubernetes.exception.ServiceException;
25 import org.onap.policy.clamp.controlloop.participant.kubernetes.helm.HelmClient;
26 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Service;
31 import org.springframework.web.multipart.MultipartFile;
34 public class ChartService {
35 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
38 private ChartStore chartStore;
41 private HelmClient helmClient;
44 * Get all the installed charts.
45 * @return list of charts.
47 public Collection<ChartInfo> getAllCharts() {
48 return chartStore.getAllCharts();
52 * Get specific chart info.
53 * @param name name of the app
54 * @param version version of the app
57 public ChartInfo getChart(String name, String version) {
58 return chartStore.getChart(name, version);
63 * @param chartInfo name and version of the app.
64 * @param chartFile Helm chart file
65 * @param overrideFile override file
66 * @return chart details of the helm chart
67 * @throws IOException in case of IO error
68 * @throws ServiceException in case of error
70 public ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile)
71 throws IOException, ServiceException {
72 return chartStore.saveChart(chartInfo, chartFile, overrideFile);
76 * Delete a helm chart.
77 * @param chart name and version of the chart.
79 public void deleteChart(ChartInfo chart) {
80 chartStore.deleteChart(chart);
84 * Install a helm chart.
85 * @param chart name and version.
86 * @throws ServiceException in case of error
87 * @throws IOException in case of IO errors
89 public void installChart(ChartInfo chart) throws ServiceException, IOException {
90 if (chart.getRepository() == null) {
91 String repository = findChartRepo(chart);
92 if (repository == null) {
93 logger.error("Chart repository could not be found. Skipping chart Installation "
94 + "for the chart {} ", chart.getChartName());
97 chart.setRepository(repository);
100 helmClient.installChart(chart);
104 * Finds helm chart repository for a given chart.
105 * @param chart chartInfo.
106 * @return the chart repo as a string
107 * @throws ServiceException in case of error
108 * @throws IOException in case of IO errors
110 public String findChartRepo(ChartInfo chart) throws ServiceException, IOException {
111 logger.info("Fetching helm chart repository for the given chart {} ", chart.getChartName());
112 return helmClient.findChartRepository(chart);
116 * Uninstall a helm chart.
117 * @param chart name and version
118 * @throws ServiceException in case of error.
120 public void uninstallChart(ChartInfo chart) throws ServiceException {
121 logger.info("Uninstalling helm deployment {}", chart.getReleaseName());
122 helmClient.uninstallChart(chart);