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 AddToRepo(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
53 func NewHelmManager(s *cli.EnvSettings) *helmManagerImpl {
54 return &helmManagerImpl{
59 func (hm *helmManagerImpl) AddToRepo(repoName, url string) error {
60 repoFile := hm.settings.RepositoryConfig
62 //Ensure the file directory exists as it is required for file locking
63 err := os.MkdirAll(filepath.Dir(repoFile), os.ModePerm)
64 if err != nil && !errors.Is(err, fs.ErrNotExist) {
68 b, err := os.ReadFile(repoFile)
74 if err := yaml.Unmarshal(b, &f); err != nil {
79 log.Debugf("repository name (%s) already exists\n", repoName)
88 r, err := repo.NewChartRepository(&c, getter.All(hm.settings))
93 if _, err := r.DownloadIndexFile(); err != nil {
94 err := errors.Wrapf(err, "looks like %q is not a valid chart repository or cannot be reached", url)
100 if err := f.WriteFile(repoFile, 0644); err != nil {
103 log.Debugf("%q has been added to your repositories\n", repoName)
107 func (hm *helmManagerImpl) InstallHelmChart(namespace, repoName, chartName, releaseName string) error {
108 actionConfig, err := getActionConfig(namespace)
113 install := action.NewInstall(actionConfig)
115 cp, err := install.ChartPathOptions.LocateChart(fmt.Sprintf("%s/%s", repoName, chartName), hm.settings)
117 log.Error("Unable to locate chart!")
121 chartRequested, err := loader.Load(cp)
123 log.Error("Unable to load chart path!")
127 install.Namespace = namespace
128 install.ReleaseName = releaseName
129 _, err = install.Run(chartRequested, nil)
131 log.Error("Unable to run chart!")
134 log.Debug("Successfully onboarded ", namespace, repoName, chartName, releaseName)
138 func (hm *helmManagerImpl) UninstallHelmChart(namespace, chartName string) {
139 actionConfig, err := getActionConfig(namespace)
141 log.Error("unable to get action config: ", err)
145 iCli := action.NewUninstall(actionConfig)
147 resp, err := iCli.Run(chartName)
149 log.Error("Unable to uninstall chart ", chartName, err)
152 log.Debug("Successfully uninstalled chart: ", resp.Release.Name)
155 func getActionConfig(namespace string) (*action.Configuration, error) {
156 actionConfig := new(action.Configuration)
157 // Create the rest config instance with ServiceAccount values loaded in them
158 config, err := rest.InClusterConfig()
160 // fallback to kubeconfig
161 home, exists := os.LookupEnv("HOME")
165 kubeconfigPath := filepath.Join(home, ".kube", "config")
166 if envvar := os.Getenv("KUBECONFIG"); len(envvar) > 0 {
167 kubeconfigPath = envvar
169 if err := actionConfig.Init(kube.GetConfig(kubeconfigPath, "", namespace), namespace, os.Getenv("HELM_DRIVER"), log.Debugf); err != nil {
173 // Create the ConfigFlags struct instance with initialized values from ServiceAccount
174 kubeConfig := genericclioptions.NewConfigFlags(false)
175 kubeConfig.APIServer = &config.Host
176 kubeConfig.BearerToken = &config.BearerToken
177 kubeConfig.CAFile = &config.CAFile
178 kubeConfig.Namespace = &namespace
179 if err := actionConfig.Init(kubeConfig, namespace, os.Getenv("HELM_DRIVER"), log.Debugf); err != nil {
183 return actionConfig, err