NONRTRIC-946: Fix Capifcore intersection panic
[nonrtric/plt/sme.git] / capifcore / internal / helmmanagement / helm.go
index b43ee0f..bf245f0 100644 (file)
@@ -22,11 +22,10 @@ package helmmanagement
 
 import (
        "fmt"
-       "io/fs"
        "os"
        "path/filepath"
+       "strings"
 
-       "github.com/pkg/errors"
        log "github.com/sirupsen/logrus"
        "gopkg.in/yaml.v2"
        "helm.sh/helm/v3/pkg/action"
@@ -41,13 +40,15 @@ 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(s *cli.EnvSettings) *helmManagerImpl {
@@ -56,17 +57,22 @@ func NewHelmManager(s *cli.EnvSettings) *helmManagerImpl {
        }
 }
 
-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
        err := os.MkdirAll(filepath.Dir(repoFile), os.ModePerm)
-       if err != nil && !errors.Is(err, fs.ErrNotExist) {
+       if err != nil && !os.IsExist(err) {
                return err
        }
 
        b, err := os.ReadFile(repoFile)
-       if err != nil {
+       if err != nil && !os.IsNotExist(err) {
                return err
        }
 
@@ -77,6 +83,7 @@ func (hm *helmManagerImpl) AddToRepo(repoName, url string) error {
 
        if f.Has(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
        }
        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)