Skip to content

Commit

Permalink
Make chart loading pluggable (quick and dirty version)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcohere committed Aug 27, 2024
1 parent e7c4238 commit 054f970
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
49 changes: 28 additions & 21 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,22 @@ 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,
ActionConfig: actionConfig,
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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 054f970

Please sign in to comment.