Skip to content

Commit

Permalink
return release in InstallOrUpgradeChart method (#29)
Browse files Browse the repository at this point in the history
* return release in InstallOrUpgradeChart method

* Update client.go

Co-authored-by: Henning <h.surmeier@mittwald.de>

* return releases too in case of error

Co-authored-by: Henning <h.surmeier@mittwald.de>
  • Loading branch information
elenz97 and hensur authored Jun 23, 2021
1 parent 98c47de commit d73010a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
44 changes: 23 additions & 21 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)

Expand All @@ -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,
Expand All @@ -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)

Expand All @@ -319,43 +321,43 @@ 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
}
}

if !spec.SkipCRDs && spec.UpgradeCRDs {
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
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions mock/interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d73010a

Please sign in to comment.