2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
21 package helmmanagement
29 "github.com/pkg/errors"
30 log "github.com/sirupsen/logrus"
32 "helm.sh/helm/v3/pkg/action"
33 "helm.sh/helm/v3/pkg/chart/loader"
34 "helm.sh/helm/v3/pkg/cli"
35 "helm.sh/helm/v3/pkg/getter"
36 "helm.sh/helm/v3/pkg/kube"
37 "helm.sh/helm/v3/pkg/repo"
38 "k8s.io/cli-runtime/pkg/genericclioptions"
39 "k8s.io/client-go/rest"
42 //go:generate mockery --name HelmManager
43 type HelmManager interface {
44 SetUpRepo(repoName, url string) error
45 InstallHelmChart(namespace, repoName, chartName, releaseName string) error
46 UninstallHelmChart(namespace, chartName string)
49 type helmManagerImpl struct {
50 settings *cli.EnvSettings
51 repo *repo.ChartRepository
54 func NewHelmManager(s *cli.EnvSettings) *helmManagerImpl {
55 return &helmManagerImpl{
60 func (hm *helmManagerImpl) SetUpRepo(repoName, url string) error {
61 repoFile := hm.settings.RepositoryConfig
63 //Ensure the file directory exists as it is required for file locking
64 err := os.MkdirAll(filepath.Dir(repoFile), os.ModePerm)
65 if err != nil && !errors.Is(err, fs.ErrNotExist) {
69 b, err := os.ReadFile(repoFile)
75 if err := yaml.Unmarshal(b, &f); err != nil {
80 log.Debugf("repository name (%s) already exists\n", repoName)
91 r, err = repo.NewChartRepository(&c, getter.All(hm.settings))
97 if _, err := r.DownloadIndexFile(); err != nil {
98 err := errors.Wrapf(err, "looks like %q is not a valid chart repository or cannot be reached", url)
104 if err := f.WriteFile(repoFile, 0644); err != nil {
107 log.Debugf("%q has been added to your repositories\n", repoName)
111 func (hm *helmManagerImpl) InstallHelmChart(namespace, repoName, chartName, releaseName string) error {
112 actionConfig, err := getActionConfig(namespace)
117 install := action.NewInstall(actionConfig)
119 cp, err := install.ChartPathOptions.LocateChart(fmt.Sprintf("%s/%s", repoName, chartName), hm.settings)
121 log.Error("Unable to locate chart!")
125 chartRequested, err := loader.Load(cp)
127 log.Error("Unable to load chart path!")
131 install.Namespace = namespace
132 install.ReleaseName = releaseName
133 _, err = install.Run(chartRequested, nil)
135 log.Error("Unable to run chart!")
138 log.Debug("Successfully onboarded ", namespace, repoName, chartName, releaseName)
142 func (hm *helmManagerImpl) UninstallHelmChart(namespace, chartName string) {
143 actionConfig, err := getActionConfig(namespace)
145 log.Error("unable to get action config: ", err)
149 iCli := action.NewUninstall(actionConfig)
151 resp, err := iCli.Run(chartName)
153 log.Error("Unable to uninstall chart ", chartName, err)
156 log.Debug("Successfully uninstalled chart: ", resp.Release.Name)
159 func getActionConfig(namespace string) (*action.Configuration, error) {
160 actionConfig := new(action.Configuration)
161 // Create the rest config instance with ServiceAccount values loaded in them
162 config, err := rest.InClusterConfig()
164 // fallback to kubeconfig
165 home, exists := os.LookupEnv("HOME")
169 kubeconfigPath := filepath.Join(home, ".kube", "config")
170 if envvar := os.Getenv("KUBECONFIG"); len(envvar) > 0 {
171 kubeconfigPath = envvar
173 if err := actionConfig.Init(kube.GetConfig(kubeconfigPath, "", namespace), namespace, os.Getenv("HELM_DRIVER"), log.Debugf); err != nil {
177 // Create the ConfigFlags struct instance with initialized values from ServiceAccount
178 kubeConfig := genericclioptions.NewConfigFlags(false)
179 kubeConfig.APIServer = &config.Host
180 kubeConfig.BearerToken = &config.BearerToken
181 kubeConfig.CAFile = &config.CAFile
182 kubeConfig.Namespace = &namespace
183 if err := actionConfig.Init(kubeConfig, namespace, os.Getenv("HELM_DRIVER"), log.Debugf); err != nil {
187 return actionConfig, err