aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-impl/participant-impl-kubernetes/src/main/java/org/onap/policy/clamp/acm/participant/kubernetes/service/ChartService.java
blob: dc4762d9a15c5d7fb9f8aa698b2cdc687ce89299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*-
 * ========================LICENSE_START=================================
 * Copyright (C) 2021-2022 Nordix Foundation. All rights reserved.
 * ======================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================LICENSE_END===================================
 */

package org.onap.policy.clamp.acm.participant.kubernetes.service;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.Collection;
import org.onap.policy.clamp.acm.participant.kubernetes.exception.ServiceException;
import org.onap.policy.clamp.acm.participant.kubernetes.helm.HelmClient;
import org.onap.policy.clamp.acm.participant.kubernetes.models.ChartInfo;
import org.onap.policy.clamp.acm.participant.kubernetes.models.HelmRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class ChartService {
    private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    @Autowired
    private ChartStore chartStore;

    @Autowired
    private HelmClient helmClient;

    /**
     * Get all the installed charts.
     * @return list of charts.
     */
    public Collection<ChartInfo> getAllCharts() {
        return chartStore.getAllCharts();
    }

    /**
     * Get specific chart info.
     * @param name name of the app
     * @param version version of the app
     * @return chart
     */
    public ChartInfo getChart(String name, String version) {
        return chartStore.getChart(name, version);
    }

    /**
     * Save a helm chart.
     * @param chartInfo name and version of the app.
     * @param chartFile Helm chart file
     * @param overrideFile override file
     * @return chart details of the helm chart
     * @throws IOException in case of IO error
     * @throws ServiceException in case of error
     */
    public ChartInfo saveChart(ChartInfo chartInfo, MultipartFile chartFile, MultipartFile overrideFile)
        throws IOException, ServiceException {
        return chartStore.saveChart(chartInfo, chartFile, overrideFile);
    }

    /**
     * Delete a helm chart.
     * @param chart name and version of the chart.
     */
    public void deleteChart(ChartInfo chart) {
        chartStore.deleteChart(chart);
    }

    /**
     * Install a helm chart.
     * @param chart name and version.
     * @throws ServiceException in case of error
     * @throws IOException in case of IO errors
     */
    public void installChart(ChartInfo chart) throws ServiceException, IOException {
        if (chart.getRepository() == null) {
            String repoName = findChartRepo(chart);
            if (repoName == null) {
                logger.error("Chart repository could not be found. Skipping chart Installation "
                    + "for the chart {} ", chart.getChartId().getName());
                return;
            } else {
                HelmRepository repo = HelmRepository.builder().repoName(repoName).build();
                chart.setRepository(repo);
            }
        } else {
            // Add remote repository if passed via TOSCA
            configureRepository(chart.getRepository());
        }
        helmClient.installChart(chart);
    }


    /**
     * Configure remote repository.
     * @param repo HelmRepository
     * @throws ServiceException incase of error
     */
    public boolean configureRepository(HelmRepository repo) throws ServiceException {
        return helmClient.addRepository(repo);
    }

    /**
     * Finds helm chart repository for a given chart.
     * @param chart chartInfo.
     * @return the chart repo as a string
     * @throws ServiceException in case of error
     * @throws IOException in case of IO errors
     */
    public String findChartRepo(ChartInfo chart) throws ServiceException, IOException {
        logger.info("Fetching helm chart repository for the given chart {} ", chart.getChartId().getName());
        return helmClient.findChartRepository(chart);
    }

    /**
     * Uninstall a helm chart.
     * @param chart name and version
     * @throws ServiceException in case of error.
     */
    public void uninstallChart(ChartInfo chart) throws ServiceException {
        logger.info("Uninstalling helm deployment {}", chart.getReleaseName());
        helmClient.uninstallChart(chart);
    }
}