dc4762d9a15c5d7fb9f8aa698b2cdc687ce89299
[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.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;
33
34 @Service
35 public class ChartService {
36     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
37
38     @Autowired
39     private ChartStore chartStore;
40
41     @Autowired
42     private HelmClient helmClient;
43
44     /**
45      * Get all the installed charts.
46      * @return list of charts.
47      */
48     public Collection<ChartInfo> getAllCharts() {
49         return chartStore.getAllCharts();
50     }
51
52     /**
53      * Get specific chart info.
54      * @param name name of the app
55      * @param version version of the app
56      * @return chart
57      */
58     public ChartInfo getChart(String name, String version) {
59         return chartStore.getChart(name, version);
60     }
61
62     /**
63      * Save a helm chart.
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
70      */
71     public ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile)
72         throws IOException, ServiceException {
73         return chartStore.saveChart(chartInfo, chartFile, overrideFile);
74     }
75
76     /**
77      * Delete a helm chart.
78      * @param chart name and version of the chart.
79      */
80     public void deleteChart(ChartInfo chart) {
81         chartStore.deleteChart(chart);
82     }
83
84     /**
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
89      */
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());
96                 return;
97             } else {
98                 HelmRepository repo = HelmRepository.builder().repoName(repoName).build();
99                 chart.setRepository(repo);
100             }
101         } else {
102             // Add remote repository if passed via TOSCA
103             configureRepository(chart.getRepository());
104         }
105         helmClient.installChart(chart);
106     }
107
108
109     /**
110      * Configure remote repository.
111      * @param repo HelmRepository
112      * @throws ServiceException incase of error
113      */
114     public boolean configureRepository(HelmRepository repo) throws ServiceException {
115         return helmClient.addRepository(repo);
116     }
117
118     /**
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
124      */
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);
128     }
129
130     /**
131      * Uninstall a helm chart.
132      * @param chart name and version
133      * @throws ServiceException in case of error.
134      */
135     public void uninstallChart(ChartInfo chart) throws ServiceException {
136         logger.info("Uninstalling helm deployment {}", chart.getReleaseName());
137         helmClient.uninstallChart(chart);
138     }
139 }