2 * ========================LICENSE_START=================================
3 * Copyright (C) 2021-2022 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.acm.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.acm.participant.kubernetes.exception.ServiceException;
25 import org.onap.policy.clamp.acm.participant.kubernetes.helm.HelmClient;
26 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
27 import org.onap.policy.clamp.acm.participant.kubernetes.models.HelmRepository;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Service;
32 import org.springframework.web.multipart.MultipartFile;
35 public class ChartService {
36 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
39 private ChartStore chartStore;
42 private HelmClient helmClient;
45 * Get all the installed charts.
46 * @return list of charts.
48 public Collection<ChartInfo> getAllCharts() {
49 return chartStore.getAllCharts();
53 * Get specific chart info.
54 * @param name name of the app
55 * @param version version of the app
58 public ChartInfo getChart(String name, String version) {
59 return chartStore.getChart(name, version);
64 * @param chartInfo name and version of the app.
65 * @param chartFile Helm chart file
66 * @param overrideFile override file
67 * @return chart details of the helm chart
68 * @throws IOException in case of IO error
69 * @throws ServiceException in case of error
71 public ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile)
72 throws IOException, ServiceException {
73 return chartStore.saveChart(chartInfo, chartFile, overrideFile);
77 * Delete a helm chart.
78 * @param chart name and version of the chart.
80 public void deleteChart(ChartInfo chart) {
81 chartStore.deleteChart(chart);
85 * Install a helm chart.
86 * @param chart name and version.
87 * @throws ServiceException in case of error
88 * @throws IOException in case of IO errors
90 public void installChart(ChartInfo chart) throws ServiceException, IOException {
91 if (chart.getRepository() == null) {
92 String repoName = findChartRepo(chart);
93 if (repoName == null) {
94 logger.error("Chart repository could not be found. Skipping chart Installation "
95 + "for the chart {} ", chart.getChartId().getName());
98 HelmRepository repo = HelmRepository.builder().repoName(repoName).build();
99 chart.setRepository(repo);
102 // Add remote repository if passed via TOSCA
103 configureRepository(chart.getRepository());
105 helmClient.installChart(chart);
110 * Configure remote repository.
111 * @param repo HelmRepository
112 * @throws ServiceException incase of error
114 public boolean configureRepository(HelmRepository repo) throws ServiceException {
115 return helmClient.addRepository(repo);
119 * Finds helm chart repository for a given chart.
120 * @param chart chartInfo.
121 * @return the chart repo as a string
122 * @throws ServiceException in case of error
123 * @throws IOException in case of IO errors
125 public String findChartRepo(ChartInfo chart) throws ServiceException, IOException {
126 logger.info("Fetching helm chart repository for the given chart {} ", chart.getChartId().getName());
127 return helmClient.findChartRepository(chart);
131 * Uninstall a helm chart.
132 * @param chart name and version
133 * @throws ServiceException in case of error.
135 public void uninstallChart(ChartInfo chart) throws ServiceException {
136 logger.info("Uninstalling helm deployment {}", chart.getReleaseName());
137 helmClient.uninstallChart(chart);