From d73010a2b63277e3fa434a7f13bc81b8416a67d8 Mon Sep 17 00:00:00 2001 From: Eike David Lenz Date: Wed, 23 Jun 2021 11:46:33 +0200 Subject: [PATCH] return release in InstallOrUpgradeChart method (#29) * return release in InstallOrUpgradeChart method * Update client.go Co-authored-by: Henning * return releases too in case of error Co-authored-by: Henning --- client.go | 44 +++++++++++++++++++++++--------------------- client_test.go | 2 +- interface.go | 2 +- mock/interface.go | 7 ++++--- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/client.go b/client.go index 50a8c790..6f658ea2 100644 --- a/client.go +++ b/client.go @@ -198,17 +198,19 @@ func (c *HelmClient) UpdateChartRepos() error { return c.storage.WriteFile(c.Settings.RepositoryConfig, 0o644) } -// InstallOrUpgradeChart triggers the installation of the provided chart. -// If the chart is already installed, trigger an upgrade instead -func (c *HelmClient) InstallOrUpgradeChart(ctx context.Context, spec *ChartSpec) error { +// InstallOrUpgradeChart triggers the installation of the provided +// chart and returns the installed / upgraded release. +// If the chart is already installed, trigger an upgrade instead. +func (c *HelmClient) InstallOrUpgradeChart(ctx context.Context, spec *ChartSpec) (*release.Release, error) { installed, err := c.chartIsInstalled(spec.ReleaseName) if err != nil { - return err + return nil, err } if installed { return c.upgrade(ctx, spec) } + return c.install(spec) } @@ -245,7 +247,7 @@ func (c *HelmClient) UninstallRelease(spec *ChartSpec) error { } // install lints and installs the provided chart -func (c *HelmClient) install(spec *ChartSpec) error { +func (c *HelmClient) install(spec *ChartSpec) (*release.Release, error) { client := action.NewInstall(c.ActionConfig) mergeInstallOptions(spec, client) @@ -255,11 +257,11 @@ func (c *HelmClient) install(spec *ChartSpec) error { helmChart, chartPath, err := c.getChart(spec.ChartName, &client.ChartPathOptions) if err != nil { - return err + return nil, err } if helmChart.Metadata.Type != "" && helmChart.Metadata.Type != "application" { - return fmt.Errorf( + return nil, fmt.Errorf( "chart %q has an unsupported type and is not installable: %q", helmChart.Metadata.Name, helmChart.Metadata.Type, @@ -278,38 +280,38 @@ func (c *HelmClient) install(spec *ChartSpec) error { RepositoryCache: c.Settings.RepositoryCache, } if err := man.Update(); err != nil { - return err + return nil, err } } else { - return err + return nil, err } } } values, err := spec.GetValuesMap() if err != nil { - return err + return nil, err } if c.linting { err = c.lint(chartPath, values) if err != nil { - return err + return nil, err } } rel, err := client.Run(helmChart, values) if err != nil { - return err + return rel, err } log.Printf("release installed successfully: %s/%s-%s", rel.Name, rel.Name, rel.Chart.Metadata.Version) - return nil + return rel, nil } // upgrade upgrades a chart and CRDs -func (c *HelmClient) upgrade(ctx context.Context, spec *ChartSpec) error { +func (c *HelmClient) upgrade(ctx context.Context, spec *ChartSpec) (*release.Release, error) { client := action.NewUpgrade(c.ActionConfig) mergeUpgradeOptions(spec, client) @@ -319,24 +321,24 @@ func (c *HelmClient) upgrade(ctx context.Context, spec *ChartSpec) error { helmChart, chartPath, err := c.getChart(spec.ChartName, &client.ChartPathOptions) if err != nil { - return err + return nil, err } if req := helmChart.Metadata.Dependencies; req != nil { if err := action.CheckDependencies(helmChart, req); err != nil { - return err + return nil, err } } values, err := spec.GetValuesMap() if err != nil { - return err + return nil, err } if c.linting { err = c.lint(chartPath, values) if err != nil { - return err + return nil, err } } @@ -344,18 +346,18 @@ func (c *HelmClient) upgrade(ctx context.Context, spec *ChartSpec) error { log.Printf("updating crds") err = c.upgradeCRDs(ctx, helmChart) if err != nil { - return err + return nil, err } } rel, err := client.Run(spec.ReleaseName, helmChart, values) if err != nil { - return err + return rel, err } log.Printf("release upgrade successfully: %s/%s-%s", rel.Name, rel.Name, rel.Chart.Metadata.Version) - return nil + return rel, nil } // deleteChartFromCache deletes the provided chart from the client's cache diff --git a/client_test.go b/client_test.go index 989780bb..c5309389 100644 --- a/client_test.go +++ b/client_test.go @@ -97,7 +97,7 @@ func ExampleHelmClient_InstallOrUpgradeChart() { Wait: true, } - if err := helmClient.InstallOrUpgradeChart(context.Background(), &chartSpec); err != nil { + if err, _ := helmClient.InstallOrUpgradeChart(context.Background(), &chartSpec); err != nil { panic(err) } } diff --git a/interface.go b/interface.go index c5de9f05..0d7c56b4 100644 --- a/interface.go +++ b/interface.go @@ -12,7 +12,7 @@ import ( type Client interface { AddOrUpdateChartRepo(entry repo.Entry) error UpdateChartRepos() error - InstallOrUpgradeChart(ctx context.Context, spec *ChartSpec) error + InstallOrUpgradeChart(ctx context.Context, spec *ChartSpec) (*release.Release, error) ListDeployedReleases() ([]*release.Release, error) GetRelease(name string) (*release.Release, error) RollbackRelease(spec *ChartSpec, version int) error diff --git a/mock/interface.go b/mock/interface.go index 533fa708..870ca24a 100644 --- a/mock/interface.go +++ b/mock/interface.go @@ -96,11 +96,12 @@ func (mr *MockClientMockRecorder) GetReleaseValues(name, allValues interface{}) } // InstallOrUpgradeChart mocks base method. -func (m *MockClient) InstallOrUpgradeChart(ctx context.Context, spec *helmclient.ChartSpec) error { +func (m *MockClient) InstallOrUpgradeChart(ctx context.Context, spec *helmclient.ChartSpec) (*release.Release, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "InstallOrUpgradeChart", ctx, spec) - ret0, _ := ret[0].(error) - return ret0 + ret0, _ := ret[0].(*release.Release) + ret1, _ := ret[1].(error) + return ret0, ret1 } // InstallOrUpgradeChart indicates an expected call of InstallOrUpgradeChart.