e9cd8a2c31f017e262fa0d77ed42248bad34376c
[policy/clamp.git] /
1 /*-
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
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 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;
34
35 @Service
36 public class ChartService {
37     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
38
39     @Autowired
40     private ChartStore chartStore;
41
42     @Autowired
43     private HelmClient helmClient;
44
45     @Autowired
46     private HelmRepositoryConfig helmRepositoryConfig;
47
48     /**
49      * Get all the installed charts.
50      * @return list of charts.
51      */
52     public Collection<ChartInfo> getAllCharts() {
53         return chartStore.getAllCharts();
54     }
55
56     /**
57      * Get specific chart info.
58      * @param name name of the app
59      * @param version version of the app
60      * @return chart
61      */
62     public ChartInfo getChart(String name, String version) {
63         return chartStore.getChart(name, version);
64     }
65
66     /**
67      * Save a helm chart.
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
74      */
75     public ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile)
76         throws IOException, ServiceException {
77         return chartStore.saveChart(chartInfo, chartFile, overrideFile);
78     }
79
80     /**
81      * Delete a helm chart.
82      * @param chart name and version of the chart.
83      */
84     public void deleteChart(ChartInfo chart) {
85         chartStore.deleteChart(chart);
86     }
87
88     /**
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
94      */
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());
102                 return false;
103             } else {
104                 HelmRepository repo = HelmRepository.builder().repoName(repoName).build();
105                 chart.setRepository(repo);
106             }
107         } else {
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());
114                     whiteListed = true;
115                     break;
116                 }
117             }
118             if (!whiteListed) {
119                 logger.error("Repository is not Whitelisted / plain http in not allowed");
120                 return false;
121             }
122         }
123         helmClient.installChart(chart);
124         return true;
125     }
126
127
128     /**
129      * Configure remote repository.
130      * @param repo HelmRepository
131      * @throws ServiceException incase of error
132      */
133     public boolean configureRepository(HelmRepository repo) throws ServiceException {
134         return helmClient.addRepository(repo);
135     }
136
137     /**
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
143      */
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);
147     }
148
149     /**
150      * Uninstall a helm chart.
151      * @param chart name and version
152      * @throws ServiceException in case of error.
153      */
154     public void uninstallChart(ChartInfo chart) throws ServiceException {
155         logger.info("Uninstalling helm deployment {}", chart.getReleaseName());
156         helmClient.uninstallChart(chart);
157     }
158 }