From 054f970f2b6eb32178d0df71c88b06252a8849cd Mon Sep 17 00:00:00 2001 From: JJ Jordan Date: Tue, 16 Jul 2024 21:58:25 -0700 Subject: [PATCH] Make chart loading pluggable (quick and dirty version) --- client.go | 49 ++++++++++++++++++++++++++++--------------------- interface.go | 7 ++++++- types.go | 7 +++++++ 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/client.go b/client.go index 020021b..4120ba0 100644 --- a/client.go +++ b/client.go @@ -115,7 +115,8 @@ func newClient(options *Options, clientGetter genericclioptions.RESTClientGetter } actionConfig.RegistryClient = registryClient - return &HelmClient{ + var client *HelmClient + client = &HelmClient{ Settings: settings, Providers: getter.All(settings), storage: &storage, @@ -123,7 +124,13 @@ func newClient(options *Options, clientGetter genericclioptions.RESTClientGetter linting: options.Linting, DebugLog: debugLog, output: options.Output, - }, nil + ChartLoader: &DefaultChartLoader{ + Settings: settings, + debugLog: func(format string, v ...interface{}) { client.DebugLog(format, v...) }, + }, + } + + return client, nil } // setEnvSettings sets the client's environment settings based on the provided client configuration. @@ -756,25 +763,6 @@ func (c *HelmClient) upgradeCRDV1(ctx context.Context, cl *clientset.Clientset, return nil } -// GetChart returns a chart matching the provided chart name and options. -func (c *HelmClient) GetChart(chartName string, chartPathOptions *action.ChartPathOptions) (*chart.Chart, string, error) { - chartPath, err := chartPathOptions.LocateChart(chartName, c.Settings) - if err != nil { - return nil, "", err - } - - helmChart, err := loader.Load(chartPath) - if err != nil { - return nil, "", err - } - - if helmChart.Metadata.Deprecated { - c.DebugLog("WARNING: This chart (%q) is deprecated", helmChart.Metadata.Name) - } - - return helmChart, chartPath, err -} - // RunTests runs the tests that were deployed with the release provided. It returns true // if all the tests ran successfully and false in all other cases. // NOTE: error = nil implies that all tests ran to either success or failure. @@ -849,6 +837,25 @@ func (c *HelmClient) rollbackRelease(spec *ChartSpec) error { return client.Run(spec.ReleaseName) } +// GetChart returns a chart matching the provided chart name and options. +func (l *DefaultChartLoader) GetChart(chartName string, chartPathOptions *action.ChartPathOptions) (*chart.Chart, string, error) { + chartPath, err := chartPathOptions.LocateChart(chartName, l.Settings) + if err != nil { + return nil, "", err + } + + helmChart, err := loader.Load(chartPath) + if err != nil { + return nil, "", err + } + + if helmChart.Metadata.Deprecated { + l.debugLog("WARNING: This chart (%q) is deprecated", helmChart.Metadata.Name) + } + + return helmChart, chartPath, err +} + // updateDependencies checks dependencies for given helmChart and updates dependencies with metadata if dependencyUpdate is true. returns updated HelmChart func updateDependencies(helmChart *chart.Chart, chartPathOptions *action.ChartPathOptions, chartPath string, c *HelmClient, dependencyUpdate bool, spec *ChartSpec) (*chart.Chart, error) { if req := helmChart.Metadata.Dependencies; req != nil { diff --git a/interface.go b/interface.go index 1528386..78e2c2a 100644 --- a/interface.go +++ b/interface.go @@ -34,10 +34,15 @@ type Client interface { LintChart(spec *ChartSpec) error SetDebugLog(debugLog action.DebugLog) ListReleaseHistory(name string, max int) ([]*release.Release, error) - GetChart(chartName string, chartPathOptions *action.ChartPathOptions) (*chart.Chart, string, error) + // ChartLoader is an interface to abstract loading charts. + ChartLoader RunChartTests(releaseName string) (bool, error) } +type ChartLoader interface { + GetChart(chartName string, chartPathOptions *action.ChartPathOptions) (*chart.Chart, string, error) +} + type RollBack interface { RollbackRelease(spec *ChartSpec) error } diff --git a/types.go b/types.go index b5999ff..d41bfd1 100644 --- a/types.go +++ b/types.go @@ -85,6 +85,13 @@ type HelmClient struct { linting bool output io.Writer DebugLog action.DebugLog + + ChartLoader +} + +type DefaultChartLoader struct { + Settings *cli.EnvSettings + debugLog action.DebugLog } func (c *HelmClient) GetSettings() *cli.EnvSettings {