2 * ========================LICENSE_START=================================
3 * Copyright (C) 2021-2022, 2025 OpenInfra Foundation Europe. 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 lombok.NoArgsConstructor;
25 import org.onap.policy.clamp.acm.participant.kubernetes.configurations.HelmRepositoryConfig;
26 import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
27 import org.onap.policy.clamp.acm.participant.kubernetes.helm.HelmClient;
28 import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
29 import org.onap.policy.clamp.acm.participant.kubernetes.models.HelmRepository;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Service;
34 import org.springframework.web.multipart.MultipartFile;
38 public class ChartService {
39 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
41 private ChartStore chartStore;
43 private HelmClient helmClient;
45 private HelmRepositoryConfig helmRepositoryConfig;
48 * Instantiates the ChartService with the specified dependencies.
50 * @param chartStore the store for managing Helm chart information
51 * @param helmClient the client for interacting with Helm
52 * @param helmRepositoryConfig the configuration for Helm repositories
55 public ChartService(ChartStore chartStore, HelmClient helmClient, HelmRepositoryConfig helmRepositoryConfig) {
56 this.chartStore = chartStore;
57 this.helmClient = helmClient;
58 this.helmRepositoryConfig = helmRepositoryConfig;
62 * Get all the installed charts.
63 * @return list of charts.
65 public Collection<ChartInfo> getAllCharts() {
66 return chartStore.getAllCharts();
70 * Get specific chart info.
71 * @param name name of the app
72 * @param version version of the app
75 public ChartInfo getChart(String name, String version) {
76 return chartStore.getChart(name, version);
81 * @param chartInfo name and version of the app.
82 * @param chartFile Helm chart file
83 * @param overrideFile override file
84 * @return chart details of the helm chart
85 * @throws IOException in case of IO error
86 * @throws ServiceException in case of error
88 public ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile)
89 throws IOException, ServiceException {
90 return chartStore.saveChart(chartInfo, chartFile, overrideFile);
94 * Delete a helm chart.
95 * @param chart name and version of the chart.
97 public void deleteChart(ChartInfo chart) {
98 chartStore.deleteChart(chart);
102 * Install a helm chart.
103 * @param chart name and version.
104 * @return boolean flag to indicate success or failure
105 * @throws ServiceException in case of error
106 * @throws IOException in case of IO errors
108 public boolean installChart(ChartInfo chart) throws ServiceException, IOException {
109 boolean permittedRepo = false;
110 if (chart.getRepository() == null) {
111 String repoName = findChartRepo(chart);
112 if (repoName == null) {
113 logger.error("Chart repository could not be found. Skipping chart Installation "
114 + "for the chart {} ", chart.getChartId().getName());
117 HelmRepository repo = HelmRepository.builder().repoName(repoName).build();
118 chart.setRepository(repo);
121 // Add a remote repository if passed via TOSCA
122 // and check whether the repo is permitted
123 for (HelmRepository repo : helmRepositoryConfig.getRepos()) {
124 if (repo.getAddress().equals(chart.getRepository().getAddress())
125 && helmRepositoryConfig.getProtocols()
126 .contains(chart.getRepository().getAddress().split(":")[0])) {
127 configureRepository(chart.getRepository());
128 permittedRepo = true;
132 if (!permittedRepo) {
133 logger.error("Helm Repository/Protocol is not permitted for {}", chart.getRepository().getAddress());
137 helmClient.installChart(chart);
143 * Configure remote repository.
144 * @param repo HelmRepository
145 * @throws ServiceException in case of error
147 public boolean configureRepository(HelmRepository repo) throws ServiceException {
148 return helmClient.addRepository(repo);
152 * Finds helm chart repository for a given chart.
153 * @param chart chartInfo.
154 * @return the chart repo as a string
155 * @throws ServiceException in case of error
156 * @throws IOException in case of IO errors
158 public String findChartRepo(ChartInfo chart) throws ServiceException, IOException {
159 logger.info("Fetching helm chart repository for the given chart {} ", chart.getChartId().getName());
160 return helmClient.findChartRepository(chart);
164 * Uninstall a helm chart.
165 * @param chart name and version
166 * @throws ServiceException in case of error.
168 public void uninstallChart(ChartInfo chart) throws ServiceException {
169 logger.info("Uninstalling helm deployment {}", chart.getReleaseName());
170 helmClient.uninstallChart(chart);