2274dd73c6098c689b063af602210e577689684d
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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===================================
17  */
18
19 package org.onap.policy.clamp.acm.participant.kubernetes.service;
20
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;
35
36 @Service
37 @NoArgsConstructor
38 public class ChartService {
39     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
40
41     private ChartStore chartStore;
42
43     private HelmClient helmClient;
44
45     private HelmRepositoryConfig helmRepositoryConfig;
46
47     /**
48      * Instantiates the ChartService with the specified dependencies.
49      *
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
53      */
54     @Autowired
55     public ChartService(ChartStore chartStore, HelmClient helmClient, HelmRepositoryConfig helmRepositoryConfig) {
56         this.chartStore = chartStore;
57         this.helmClient = helmClient;
58         this.helmRepositoryConfig = helmRepositoryConfig;
59     }
60
61     /**
62      * Get all the installed charts.
63      * @return list of charts.
64      */
65     public Collection<ChartInfo> getAllCharts() {
66         return chartStore.getAllCharts();
67     }
68
69     /**
70      * Get specific chart info.
71      * @param name name of the app
72      * @param version version of the app
73      * @return chart
74      */
75     public ChartInfo getChart(String name, String version) {
76         return chartStore.getChart(name, version);
77     }
78
79     /**
80      * Save a helm chart.
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
87      */
88     public ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile)
89         throws IOException, ServiceException {
90         return chartStore.saveChart(chartInfo, chartFile, overrideFile);
91     }
92
93     /**
94      * Delete a helm chart.
95      * @param chart name and version of the chart.
96      */
97     public void deleteChart(ChartInfo chart) {
98         chartStore.deleteChart(chart);
99     }
100
101     /**
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
107      */
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());
115                 return false;
116             } else {
117                 HelmRepository repo = HelmRepository.builder().repoName(repoName).build();
118                 chart.setRepository(repo);
119             }
120         } else {
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;
129                     break;
130                 }
131             }
132             if (!permittedRepo) {
133                 logger.error("Helm Repository/Protocol is not permitted for {}", chart.getRepository().getAddress());
134                 return false;
135             }
136         }
137         helmClient.installChart(chart);
138         return true;
139     }
140
141
142     /**
143      * Configure remote repository.
144      * @param repo HelmRepository
145      * @throws ServiceException in case of error
146      */
147     public boolean configureRepository(HelmRepository repo) throws ServiceException {
148         return helmClient.addRepository(repo);
149     }
150
151     /**
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
157      */
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);
161     }
162
163     /**
164      * Uninstall a helm chart.
165      * @param chart name and version
166      * @throws ServiceException in case of error.
167      */
168     public void uninstallChart(ChartInfo chart) throws ServiceException {
169         logger.info("Uninstalling helm deployment {}", chart.getReleaseName());
170         helmClient.uninstallChart(chart);
171     }
172 }