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() *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 && !os.IsExist(err) {
68 b, err := ioutil.ReadFile(repoFile)
69 if err != nil && !os.IsNotExist(err) {
74 if err := yaml.Unmarshal(b, &f); err != nil {
79 fmt.Printf("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 fmt.Printf("%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)
144 iCli := action.NewUninstall(actionConfig)
146 resp, err := iCli.Run(chartName)
150 log.Debug("Successfully uninstalled chart: ", resp.Release.Name)
153 func getActionConfig(namespace string) (*action.Configuration, error) {
154 actionConfig := new(action.Configuration)
155 // Create the rest config instance with ServiceAccount values loaded in them
156 config, err := rest.InClusterConfig()
158 // fallback to kubeconfig
159 home, exists := os.LookupEnv("HOME")
163 kubeconfigPath := filepath.Join(home, ".kube", "config")
164 if envvar := os.Getenv("KUBECONFIG"); len(envvar) > 0 {
165 kubeconfigPath = envvar
167 if err := actionConfig.Init(kube.GetConfig(kubeconfigPath, "", namespace), namespace, os.Getenv("HELM_DRIVER"),
168 func(format string, v ...interface{}) {
169 fmt.Sprintf(format, v)
174 // Create the ConfigFlags struct instance with initialized values from ServiceAccount
175 kubeConfig := genericclioptions.NewConfigFlags(false)
176 kubeConfig.APIServer = &config.Host
177 kubeConfig.BearerToken = &config.BearerToken
178 kubeConfig.CAFile = &config.CAFile
179 kubeConfig.Namespace = &namespace
180 if err := actionConfig.Init(kubeConfig, namespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
181 fmt.Sprintf(format, v)
186 return actionConfig, err