Merge "Adding Capif provider"
[nonrtric/plt/sme.git] / capifcore / internal / helmmanagement / helm.go
index cfed6eb..bf245f0 100644 (file)
@@ -22,11 +22,10 @@ package helmmanagement
 
 import (
        "fmt"
-       "io/ioutil"
        "os"
        "path/filepath"
+       "strings"
 
-       "github.com/pkg/errors"
        log "github.com/sirupsen/logrus"
        "gopkg.in/yaml.v2"
        "helm.sh/helm/v3/pkg/action"
@@ -41,22 +40,29 @@ import (
 
 //go:generate mockery --name HelmManager
 type HelmManager interface {
-       AddToRepo(repoName, url string) error
+       SetUpRepo(repoName, url string) error
        InstallHelmChart(namespace, repoName, chartName, releaseName string) error
        UninstallHelmChart(namespace, chartName string)
 }
 
 type helmManagerImpl struct {
        settings *cli.EnvSettings
+       repo     *repo.ChartRepository
+       setUp    bool
 }
 
-func NewHelmManager() *helmManagerImpl {
+func NewHelmManager(s *cli.EnvSettings) *helmManagerImpl {
        return &helmManagerImpl{
-               settings: cli.New(),
+               settings: s,
        }
 }
 
-func (hm *helmManagerImpl) AddToRepo(repoName, url string) error {
+func (hm *helmManagerImpl) SetUpRepo(repoName, url string) error {
+       if len(strings.TrimSpace(url)) == 0 {
+               log.Info("No ChartMuseum repo set up.")
+               return nil
+       }
+       log.Debugf("Adding %s to Helm Repo\n", url)
        repoFile := hm.settings.RepositoryConfig
 
        //Ensure the file directory exists as it is required for file locking
@@ -65,7 +71,7 @@ func (hm *helmManagerImpl) AddToRepo(repoName, url string) error {
                return err
        }
 
-       b, err := ioutil.ReadFile(repoFile)
+       b, err := os.ReadFile(repoFile)
        if err != nil && !os.IsNotExist(err) {
                return err
        }
@@ -76,7 +82,8 @@ func (hm *helmManagerImpl) AddToRepo(repoName, url string) error {
        }
 
        if f.Has(repoName) {
-               fmt.Printf("repository name (%s) already exists\n", repoName)
+               log.Debugf("repository name (%s) already exists\n", repoName)
+               hm.setUp = true
                return nil
        }
 
@@ -85,26 +92,34 @@ func (hm *helmManagerImpl) AddToRepo(repoName, url string) error {
                URL:  url,
        }
 
-       r, err := repo.NewChartRepository(&c, getter.All(hm.settings))
-       if err != nil {
-               return err
+       r := hm.repo
+       if r == nil {
+               r, err = repo.NewChartRepository(&c, getter.All(hm.settings))
+               if err != nil {
+                       return err
+               }
        }
 
-       if _, err := r.DownloadIndexFile(); err != nil {
-               err := errors.Wrapf(err, "looks like %q is not a valid chart repository or cannot be reached", url)
+       if _, err = r.DownloadIndexFile(); err != nil {
+               log.Errorf("looks like %q is not a valid chart repository or cannot be reached", url)
                return err
        }
 
        f.Update(&c)
 
-       if err := f.WriteFile(repoFile, 0644); err != nil {
+       if err = f.WriteFile(repoFile, 0644); err != nil {
                return err
        }
-       fmt.Printf("%q has been added to your repositories\n", repoName)
+       log.Debugf("%q has been added to your repositories\n", repoName)
+       hm.setUp = true
        return nil
 }
 
 func (hm *helmManagerImpl) InstallHelmChart(namespace, repoName, chartName, releaseName string) error {
+       if !hm.setUp {
+               log.Warnf("Helm repo not added, so chart %s not installed", chartName)
+               return nil
+       }
        actionConfig, err := getActionConfig(namespace)
        if err != nil {
                return err
@@ -114,13 +129,13 @@ func (hm *helmManagerImpl) InstallHelmChart(namespace, repoName, chartName, rele
 
        cp, err := install.ChartPathOptions.LocateChart(fmt.Sprintf("%s/%s", repoName, chartName), hm.settings)
        if err != nil {
-               log.Error("Unable to locate chart!")
+               log.Errorf("Unable to locate chart: %s", chartName)
                return err
        }
 
        chartRequested, err := loader.Load(cp)
        if err != nil {
-               log.Error("Unable to load chart path!")
+               log.Errorf("Unable to load chart path for chart: %s", chartName)
                return err
        }
 
@@ -128,7 +143,7 @@ func (hm *helmManagerImpl) InstallHelmChart(namespace, repoName, chartName, rele
        install.ReleaseName = releaseName
        _, err = install.Run(chartRequested, nil)
        if err != nil {
-               log.Error("Unable to run chart!")
+               log.Errorf("Unable to run chart: %s", chartName)
                return err
        }
        log.Debug("Successfully onboarded ", namespace, repoName, chartName, releaseName)
@@ -138,14 +153,16 @@ func (hm *helmManagerImpl) InstallHelmChart(namespace, repoName, chartName, rele
 func (hm *helmManagerImpl) UninstallHelmChart(namespace, chartName string) {
        actionConfig, err := getActionConfig(namespace)
        if err != nil {
-               fmt.Println(err)
+               log.Error("unable to get action config: ", err)
+               return
        }
 
        iCli := action.NewUninstall(actionConfig)
 
        resp, err := iCli.Run(chartName)
        if err != nil {
-               fmt.Println(err)
+               log.Error("Unable to uninstall chart ", chartName, err)
+               return
        }
        log.Debug("Successfully uninstalled chart: ", resp.Release.Name)
 }
@@ -164,11 +181,8 @@ func getActionConfig(namespace string) (*action.Configuration, error) {
                if envvar := os.Getenv("KUBECONFIG"); len(envvar) > 0 {
                        kubeconfigPath = envvar
                }
-               if err := actionConfig.Init(kube.GetConfig(kubeconfigPath, "", namespace), namespace, os.Getenv("HELM_DRIVER"),
-                       func(format string, v ...interface{}) {
-                               fmt.Sprintf(format, v)
-                       }); err != nil {
-                       fmt.Println(err)
+               if err := actionConfig.Init(kube.GetConfig(kubeconfigPath, "", namespace), namespace, os.Getenv("HELM_DRIVER"), log.Debugf); err != nil {
+                       log.Error(err)
                }
        } else {
                // Create the ConfigFlags struct instance with initialized values from ServiceAccount
@@ -177,10 +191,8 @@ func getActionConfig(namespace string) (*action.Configuration, error) {
                kubeConfig.BearerToken = &config.BearerToken
                kubeConfig.CAFile = &config.CAFile
                kubeConfig.Namespace = &namespace
-               if err := actionConfig.Init(kubeConfig, namespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
-                       fmt.Sprintf(format, v)
-               }); err != nil {
-                       fmt.Println(err)
+               if err := actionConfig.Init(kubeConfig, namespace, os.Getenv("HELM_DRIVER"), log.Debugf); err != nil {
+                       log.Error(err)
                }
        }
        return actionConfig, err