Skip to content

Commit

Permalink
Delete failed installations (#1320)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Martinez Gotor authored Dec 3, 2019
1 parent 9a74811 commit 06437c9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
26 changes: 21 additions & 5 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ func unlock(name string) {
func (p *Proxy) CreateRelease(name, namespace, values string, ch *chart.Chart) (*release.Release, error) {
lock(name)
defer unlock(name)

// Validate if the release already exists
_, err := p.helmClient.ReleaseContent(name)
if err == nil {
return nil, fmt.Errorf("Release %s already exists", name)
}

log.Printf("Installing release %s into namespace %s", name, namespace)
res, err := p.helmClient.InstallReleaseFromChart(
ch,
Expand All @@ -251,8 +258,13 @@ func (p *Proxy) CreateRelease(name, namespace, values string, ch *chart.Chart) (
helm.InstallTimeout(p.timeout),
)
if err != nil {
return nil, fmt.Errorf("Unable to create the release: %v", err)
errDelete := p.deleteRelease(name, namespace, true)
if errDelete != nil {
return nil, fmt.Errorf("Release %q failed: %v. Unable to purge failed release: %v", name, err, errDelete)
}
return nil, fmt.Errorf("Release %q failed and has been uninstalled: %v", name, err)
}

log.Printf("%s successfully installed in %s", name, namespace)
return res.GetRelease(), nil
}
Expand Down Expand Up @@ -307,10 +319,7 @@ func (p *Proxy) GetRelease(name, namespace string) (*release.Release, error) {
return p.getRelease(name, namespace)
}

// DeleteRelease deletes a release
func (p *Proxy) DeleteRelease(name, namespace string, purge bool) error {
lock(name)
defer unlock(name)
func (p *Proxy) deleteRelease(name, namespace string, purge bool) error {
// Validate that the release actually belongs to the namespace
_, err := p.getRelease(name, namespace)
if err != nil {
Expand All @@ -327,6 +336,13 @@ func (p *Proxy) DeleteRelease(name, namespace string, purge bool) error {
return nil
}

// DeleteRelease deletes a release
func (p *Proxy) DeleteRelease(name, namespace string, purge bool) error {
lock(name)
defer unlock(name)
return p.deleteRelease(name, namespace, purge)
}

// extracted from https://github.com/helm/helm/blob/master/cmd/helm/helm.go#L227
// prettyError unwraps or rewrites certain errors to make them more user-friendly.
func prettyError(err error) error {
Expand Down
26 changes: 25 additions & 1 deletion pkg/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,31 @@ func TestCreateConflictingHelmRelease(t *testing.T) {
if err == nil {
t.Error("Release should fail, an existing release in a different namespace already exists")
}
if !strings.Contains(err.Error(), "name that is still in use") {
if !strings.Contains(err.Error(), "already exists") {
t.Errorf("Unexpected error %v", err)
}
}

func TestCreateExistingHelmRelease(t *testing.T) {
ns := "myns"
rs := "foo"
chartName := "bar"
version := "v1.0.0"
ch := &chart.Chart{
Metadata: &chart.Metadata{Name: chartName, Version: version},
}
app := AppOverview{rs, version, ns, "icon.png", "DEPLOYED", "wordpress", chart.Metadata{
Version: "1.0.0",
Icon: "icon.png",
Name: "wordpress",
}}
proxy := newFakeProxy([]AppOverview{app})

_, err := proxy.CreateRelease(rs, ns, "", ch)
if err == nil {
t.Error("Release should fail, an existing release in a different namespace already exists")
}
if !strings.Contains(err.Error(), "already exists") {
t.Errorf("Unexpected error %v", err)
}
}
Expand Down

0 comments on commit 06437c9

Please sign in to comment.