From 4c2884c40f3a2266ee77d17848fb12f87a858d70 Mon Sep 17 00:00:00 2001 From: "M. Mert Yildiran" Date: Mon, 3 Jul 2023 17:15:47 +0300 Subject: [PATCH] :sparkles: Add `KUBESHARK_HELM_CHART_PATH` environment variable to set a local path for the Helm chart --- kubernetes/helm/helm.go | 125 ++++++++++++++++++----------------- misc/version/versionCheck.go | 2 +- 2 files changed, 67 insertions(+), 60 deletions(-) diff --git a/kubernetes/helm/helm.go b/kubernetes/helm/helm.go index 7378ed5ac..21b0f7317 100644 --- a/kubernetes/helm/helm.go +++ b/kubernetes/helm/helm.go @@ -2,11 +2,14 @@ package helm import ( "encoding/json" + "fmt" "os" "path/filepath" "regexp" + "strings" "github.com/kubeshark/kubeshark/config" + "github.com/kubeshark/kubeshark/misc" "github.com/pkg/errors" "github.com/rs/zerolog/log" "helm.sh/helm/v3/pkg/action" @@ -64,70 +67,74 @@ func (h *Helm) Install() (rel *release.Release, err error) { client.Namespace = h.releaseNamespace client.ReleaseName = h.releaseName - var chartURL string - chartURL, err = repo.FindChartInRepoURL(h.repo, h.releaseName, "", "", "", "", getter.All(&cli.EnvSettings{})) - if err != nil { - return - } - - var cp string - cp, err = client.ChartPathOptions.LocateChart(chartURL, settings) - if err != nil { - return - } - - m := &downloader.Manager{ - Out: os.Stdout, - ChartPath: cp, - Keyring: client.ChartPathOptions.Keyring, - SkipUpdate: false, - Getters: getter.All(settings), - RepositoryConfig: settings.RepositoryConfig, - RepositoryCache: settings.RepositoryCache, - Debug: settings.Debug, - } - - dl := downloader.ChartDownloader{ - Out: m.Out, - Verify: m.Verify, - Keyring: m.Keyring, - RepositoryConfig: m.RepositoryConfig, - RepositoryCache: m.RepositoryCache, - RegistryClient: m.RegistryClient, - Getters: m.Getters, - Options: []getter.Option{ - getter.WithInsecureSkipVerifyTLS(false), - }, - } - - repoPath := filepath.Dir(m.ChartPath) - err = os.MkdirAll(repoPath, os.ModePerm) - if err != nil { - return - } - - version := "" - if registry.IsOCI(chartURL) { - chartURL, version, err = parseOCIRef(chartURL) + chartPath := os.Getenv(fmt.Sprintf("%s_HELM_CHART_PATH", strings.ToUpper(misc.Program))) + if chartPath == "" { + var chartURL string + chartURL, err = repo.FindChartInRepoURL(h.repo, h.releaseName, "", "", "", "", getter.All(&cli.EnvSettings{})) if err != nil { return } - dl.Options = append(dl.Options, - getter.WithRegistryClient(m.RegistryClient), - getter.WithTagName(version)) + + var cp string + cp, err = client.ChartPathOptions.LocateChart(chartURL, settings) + if err != nil { + return + } + + m := &downloader.Manager{ + Out: os.Stdout, + ChartPath: cp, + Keyring: client.ChartPathOptions.Keyring, + SkipUpdate: false, + Getters: getter.All(settings), + RepositoryConfig: settings.RepositoryConfig, + RepositoryCache: settings.RepositoryCache, + Debug: settings.Debug, + } + + dl := downloader.ChartDownloader{ + Out: m.Out, + Verify: m.Verify, + Keyring: m.Keyring, + RepositoryConfig: m.RepositoryConfig, + RepositoryCache: m.RepositoryCache, + RegistryClient: m.RegistryClient, + Getters: m.Getters, + Options: []getter.Option{ + getter.WithInsecureSkipVerifyTLS(false), + }, + } + + repoPath := filepath.Dir(m.ChartPath) + err = os.MkdirAll(repoPath, os.ModePerm) + if err != nil { + return + } + + version := "" + if registry.IsOCI(chartURL) { + chartURL, version, err = parseOCIRef(chartURL) + if err != nil { + return + } + dl.Options = append(dl.Options, + getter.WithRegistryClient(m.RegistryClient), + getter.WithTagName(version)) + } + + log.Info(). + Str("url", chartURL). + Str("repo-path", repoPath). + Msg("Downloading Helm chart:") + + if _, _, err = dl.DownloadTo(chartURL, version, repoPath); err != nil { + return + } + + chartPath = m.ChartPath } - - log.Info(). - Str("url", chartURL). - Str("repo-path", repoPath). - Msg("Downloading Helm chart:") - - if _, _, err = dl.DownloadTo(chartURL, version, repoPath); err != nil { - return - } - var chart *chart.Chart - chart, err = loader.Load(m.ChartPath) + chart, err = loader.Load(chartPath) if err != nil { return } diff --git a/misc/version/versionCheck.go b/misc/version/versionCheck.go index 0098c3eae..9e22e6f20 100644 --- a/misc/version/versionCheck.go +++ b/misc/version/versionCheck.go @@ -16,7 +16,7 @@ import ( ) func CheckNewerVersion() { - if os.Getenv("KUBESHARK_DISABLE_VERSION_CHECK") != "" { + if os.Getenv(fmt.Sprintf("%s_DISABLE_VERSION_CHECK", strings.ToUpper(misc.Program))) != "" { return }