adb6cf0d1796f3f5f3a4d7603cbdb53f42f25d0a
[policy/clamp.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * Copyright (C) 2021 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.controlloop.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.controlloop.participant.kubernetes.exception.ServiceException;
25 import org.onap.policy.clamp.controlloop.participant.kubernetes.helm.HelmClient;
26 import org.onap.policy.clamp.controlloop.participant.kubernetes.models.ChartInfo;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Service;
31 import org.springframework.web.multipart.MultipartFile;
32
33 @Service
34 public class ChartService {
35     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
36
37     @Autowired
38     private ChartStore chartStore;
39
40     @Autowired
41     private HelmClient helmClient;
42
43     /**
44      * Get all the installed charts.
45      * @return list of charts.
46      */
47     public Collection<ChartInfo> getAllCharts() {
48         return chartStore.getAllCharts();
49     }
50
51     /**
52      * Get specific chart info.
53      * @param name name of the app
54      * @param version version of the app
55      * @return chart
56      */
57     public ChartInfo getChart(String name, String version) {
58         return chartStore.getChart(name, version);
59     }
60
61     /**
62      * Save a helm chart.
63      * @param chartInfo name and version of the app.
64      * @param chartFile Helm chart file
65      * @param overrideFile override file
66      * @return chart details of the helm chart
67      * @throws IOException in case of IO error
68      * @throws ServiceException in case of error
69      */
70     public ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile)
71             throws IOException, ServiceException {
72         return chartStore.saveChart(chartInfo, chartFile, overrideFile);
73     }
74
75     /**
76      * Delete a helm chart.
77      * @param chart name and version of the chart.
78      */
79     public void deleteChart(ChartInfo chart) {
80         chartStore.deleteChart(chart);
81     }
82
83     /**
84      * Install a helm chart.
85      * @param chart name and version.
86      * @throws ServiceException in case of error
87      * @throws IOException in case of IO errors
88      */
89     public void installChart(ChartInfo chart) throws ServiceException, IOException {
90         if (chart.getRepository() == null) {
91             String repository = findChartRepo(chart);
92             if (repository == null) {
93                 logger.error("Chart repository could not be found. Skipping chart Installation "
94                         + "for the chart {} ", chart.getChartName());
95                 return;
96             } else {
97                 chart.setRepository(repository);
98             }
99         }
100         helmClient.installChart(chart);
101     }
102
103     /**
104      * Finds helm chart repository for a given chart.
105      * @param chart chartInfo.
106      * @return the chart repo as a string
107      * @throws ServiceException in case of error
108      * @throws IOException in case of IO errors
109      */
110     public String findChartRepo(ChartInfo chart) throws ServiceException, IOException {
111         logger.info("Fetching helm chart repository for the given chart {} ", chart.getChartName());
112         return helmClient.findChartRepository(chart);
113     }
114
115     /**
116      * Uninstall a helm chart.
117      * @param chart name and version
118      * @throws ServiceException in case of error.
119      */
120     public void uninstallChart(ChartInfo chart) throws ServiceException {
121         logger.info("Uninstalling helm deployment {}", chart.getReleaseName());
122         helmClient.uninstallChart(chart);
123     }
124
125 }