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.configurations.HelmRepositoryConfig;
25 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
26 import org.onap.policy.clamp.acm.participant.kubernetes.helm.HelmClient;
27 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
28 import org.onap.policy.clamp.acm.participant.kubernetes.models.HelmRepository;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Service;
33 import org.springframework.web.multipart.MultipartFile;
36 public class ChartService {
37 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
40 private ChartStore chartStore;
43 private HelmClient helmClient;
46 private HelmRepositoryConfig helmRepositoryConfig;
49 * Get all the installed charts.
50 * @return list of charts.
52 public Collection<ChartInfo> getAllCharts() {
53 return chartStore.getAllCharts();
57 * Get specific chart info.
58 * @param name name of the app
59 * @param version version of the app
62 public ChartInfo getChart(String name, String version) {
63 return chartStore.getChart(name, version);
68 * @param chartInfo name and version of the app.
69 * @param chartFile Helm chart file
70 * @param overrideFile override file
71 * @return chart details of the helm chart
72 * @throws IOException in case of IO error
73 * @throws ServiceException in case of error
75 public ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile)
76 throws IOException, ServiceException {
77 return chartStore.saveChart(chartInfo, chartFile, overrideFile);
81 * Delete a helm chart.
82 * @param chart name and version of the chart.
84 public void deleteChart(ChartInfo chart) {
85 chartStore.deleteChart(chart);
89 * Install a helm chart.
90 * @param chart name and version.
91 * @return boolean flag to indicate success or failure
92 * @throws ServiceException in case of error
93 * @throws IOException in case of IO errors
95 public boolean installChart(ChartInfo chart) throws ServiceException, IOException {
96 boolean whiteListed = false;
97 if (chart.getRepository() == null) {
98 String repoName = findChartRepo(chart);
99 if (repoName == null) {
100 logger.error("Chart repository could not be found. Skipping chart Installation "
101 + "for the chart {} ", chart.getChartId().getName());
104 HelmRepository repo = HelmRepository.builder().repoName(repoName).build();
105 chart.setRepository(repo);
108 // Add remote repository if passed via TOSCA
109 // check whether the repo is whitelisted
110 for (HelmRepository repo : helmRepositoryConfig.getRepos()) {
111 if (repo.getAddress().equals(chart.getRepository().getAddress())
112 && chart.getRepository().getAddress().contains("https")) {
113 configureRepository(chart.getRepository());
119 logger.error("Repository is not Whitelisted / plain http in not allowed");
123 helmClient.installChart(chart);
129 * Configure remote repository.
130 * @param repo HelmRepository
131 * @throws ServiceException incase of error
133 public boolean configureRepository(HelmRepository repo) throws ServiceException {
134 return helmClient.addRepository(repo);
138 * Finds helm chart repository for a given chart.
139 * @param chart chartInfo.
140 * @return the chart repo as a string
141 * @throws ServiceException in case of error
142 * @throws IOException in case of IO errors
144 public String findChartRepo(ChartInfo chart) throws ServiceException, IOException {
145 logger.info("Fetching helm chart repository for the given chart {} ", chart.getChartId().getName());
146 return helmClient.findChartRepository(chart);
150 * Uninstall a helm chart.
151 * @param chart name and version
152 * @throws ServiceException in case of error.
154 public void uninstallChart(ChartInfo chart) throws ServiceException {
155 logger.info("Uninstalling helm deployment {}", chart.getReleaseName());
156 helmClient.uninstallChart(chart);