Skip to content

Commit

Permalink
Atomic behavior (#1493)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Martinez Gotor authored Feb 3, 2020
1 parent 4838811 commit a60d687
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
8 changes: 3 additions & 5 deletions cmd/kubeops/internal/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,9 @@ func TestActions(t *testing.T) {
Action: "create",
Params: map[string]string{"namespace": "default"},
// Expected result
StatusCode: 403,
RemainingReleases: []*release.Release{
createRelease("foo", "foobar", "default", 1, release.StatusFailed),
},
ResponseBody: `{"code":403,"message":"[{\"apiGroup\":\"\",\"resource\":\"secrets\",\"namespace\":\"default\",\"clusterWide\":false,\"verbs\":[\"create\"]}]"}`,
StatusCode: 403,
RemainingReleases: nil,
ResponseBody: `{"code":403,"message":"[{\"apiGroup\":\"\",\"resource\":\"secrets\",\"namespace\":\"default\",\"clusterWide\":false,\"verbs\":[\"create\"]}]"}`,
},
}

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ helm.sh/helm/v3 v3.0.0 h1:or/9cs1GgfcTQeEnR2CVJNw893/rmqIG1KsNHmUiSFw=
helm.sh/helm/v3 v3.0.0/go.mod h1:sI7B9yfvMgxtTPMWdk1jSKJ2aa59UyP9qhPydqW6mgo=
helm.sh/helm/v3 v3.0.2 h1:BggvLisIMrAc+Is5oAHVrlVxgwOOrMN8nddfQbm5gKo=
helm.sh/helm/v3 v3.0.2/go.mod h1:KBxE6XWO57XSNA1PA9CvVLYRY0zWqYQTad84bNXp1lw=
helm.sh/helm/v3 v3.0.3 h1:FwoMFwq6evEuljmP9s08WPScb/QzVLB6ZXR2P/MZP6c=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
15 changes: 11 additions & 4 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,26 @@ func ListReleases(actionConfig *action.Configuration, namespace string, listLimi

// CreateRelease creates a release.
func CreateRelease(actionConfig *action.Configuration, name, namespace, valueString string, ch *chart.Chart) (*release.Release, error) {
// Check if the release already exists
_, err := GetRelease(actionConfig, name)
if err == nil {
return nil, fmt.Errorf("release %s already exists", name)
}
cmd := action.NewInstall(actionConfig)
cmd.ReleaseName = name
cmd.Namespace = namespace
// TODO(andresmgot): Enable Atomic installations once this issue is fixed in Helm
// https://github.com/helm/helm/issues/7426
// cmd.Atomic = true
values, err := getValues([]byte(valueString))
if err != nil {
return nil, err
}
release, err := cmd.Run(ch, values)
if err != nil {
return nil, err
// Simulate the Atomic flag and delete the release if failed
errDelete := DeleteRelease(actionConfig, name, false)
if errDelete != nil {
return nil, fmt.Errorf("Release %q failed: %v. Unable to delete failed release: %v", name, err, errDelete)
}
return nil, fmt.Errorf("Release %q failed and has been uninstalled: %v", name, err)
}
return release, nil
}
Expand Down
33 changes: 22 additions & 11 deletions pkg/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,15 @@ func TestGetRelease(t *testing.T) {

func TestCreateReleases(t *testing.T) {
testCases := []struct {
desc string
releaseName string
namespace string
chartName string
values string
version int
existingReleases []releaseStub
shouldFail bool
desc string
releaseName string
namespace string
chartName string
values string
version int
existingReleases []releaseStub
remainingReleases int
shouldFail bool
}{
{
desc: "install new release",
Expand All @@ -151,7 +152,8 @@ func TestCreateReleases(t *testing.T) {
existingReleases: []releaseStub{
releaseStub{"otherchart", "default", 1, "1.0.0", release.StatusDeployed},
},
shouldFail: false,
remainingReleases: 2,
shouldFail: false,
},
{
desc: "install with an existing name",
Expand All @@ -162,7 +164,8 @@ func TestCreateReleases(t *testing.T) {
existingReleases: []releaseStub{
releaseStub{"mychart", "default", 1, "1.0.0", release.StatusDeployed},
},
shouldFail: true,
remainingReleases: 1,
shouldFail: true,
},
{
desc: "install with same name different version",
Expand All @@ -173,7 +176,8 @@ func TestCreateReleases(t *testing.T) {
existingReleases: []releaseStub{
releaseStub{"mychart", "dev", 2, "1.0.0", release.StatusDeployed},
},
shouldFail: true,
remainingReleases: 1,
shouldFail: true,
},
}

Expand All @@ -195,6 +199,13 @@ func TestCreateReleases(t *testing.T) {
if !tc.shouldFail && rls == nil {
t.Errorf("Should succeed with %v; instead got error %v", tc.desc, err)
}
rlss, err := actionConfig.Releases.ListReleases()
if err != nil {
t.Errorf("Unexpected err %v", err)
}
if len(rlss) != tc.remainingReleases {
t.Errorf("Expecting %d remaining releases, got %d", tc.remainingReleases, len(rlss))
}
})
}
}
Expand Down

0 comments on commit a60d687

Please sign in to comment.