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
56 * @throws ServiceException incase of error.
58 public ChartInfo getChart(String name, String version) throws ServiceException {
59 return chartStore.getChart(name, version);
64 * @param chartInfo name and version of the app.
65 * @param chartFile Helm chart file
66 * @return chart details of the helm chart
67 * @throws IOException incase of IO error
68 * @throws ServiceException incase 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 incase of error
88 public void installChart(ChartInfo chart) throws ServiceException, IOException {
89 if (chart.getRepository() == null) {
90 String repository = findChartRepo(chart);
91 if (repository == null) {
92 logger.error("Chart repository could not be found. Skipping chart Installation "
93 + "for the chart {} ", chart.getChartName());
96 chart.setRepository(repository);
99 helmClient.installChart(chart);
103 * Finds helm chart repository for a given chart.
104 * @param chart chartInfo.
105 * @throws ServiceException incase of error
107 public String findChartRepo(ChartInfo chart) throws ServiceException, IOException {
108 logger.info("Fetching helm chart repository for the given chart {} ", chart.getChartName());
109 return helmClient.findChartRepository(chart);
113 * Uninstall a helm chart.
114 * @param chart name and version
115 * @throws ServiceException incase of error.
117 public void uninstallChart(ChartInfo chart) throws ServiceException {
118 logger.info("Uninstalling helm deployment {}", chart.getReleaseName());
119 helmClient.uninstallChart(chart);