Skip to content

Commit 823836b

Browse files
authored
feat: add chart rollback action (#37)
1 parent 4b1812b commit 823836b

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

actions/actions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func NewService(
5656
reflect.TypeOf(&castai.ActionApproveCSR{}): newApproveCSRHandler(log, clientset),
5757
reflect.TypeOf(&castai.ActionChartUpsert{}): newChartUpsertHandler(log, helmClient),
5858
reflect.TypeOf(&castai.ActionChartUninstall{}): newChartUninstallHandler(log, helmClient),
59+
reflect.TypeOf(&castai.ActionChartRollback{}): newChartRollbackHandler(log, helmClient),
5960
reflect.TypeOf(&castai.ActionDisconnectCluster{}): newDisconnectClusterHandler(log, clientset),
6061
reflect.TypeOf(&castai.ActionSendAKSInitData{}): newSendAKSInitDataHandler(log, castaiClient),
6162
reflect.TypeOf(&castai.ActionCheckNodeDeleted{}): newCheckNodeDeletedHandler(log, clientset),

actions/chart_rollback_handler.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package actions
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
8+
"github.com/sirupsen/logrus"
9+
10+
"github.com/castai/cluster-controller/castai"
11+
"github.com/castai/cluster-controller/helm"
12+
)
13+
14+
func newChartRollbackHandler(log logrus.FieldLogger, helm helm.Client) ActionHandler {
15+
return &chartRollbackHandler{
16+
log: log,
17+
helm: helm,
18+
}
19+
}
20+
21+
type chartRollbackHandler struct {
22+
log logrus.FieldLogger
23+
helm helm.Client
24+
}
25+
26+
func (c *chartRollbackHandler) Handle(_ context.Context, data interface{}) error {
27+
req, ok := data.(*castai.ActionChartRollback)
28+
if !ok {
29+
return fmt.Errorf("unexpected type %T for chart rollback handler", data)
30+
}
31+
32+
if err := c.validateRequest(req); err != nil {
33+
return err
34+
}
35+
36+
return c.helm.Rollback(helm.RollbackOptions{
37+
ReleaseName: req.ReleaseName,
38+
Namespace: req.Namespace,
39+
})
40+
}
41+
42+
func (c *chartRollbackHandler) validateRequest(req *castai.ActionChartRollback) error {
43+
if req.ReleaseName == "" {
44+
return errors.New("bad request: releaseName not provided")
45+
}
46+
if req.Namespace == "" {
47+
return errors.New("bad request: namespace not provided")
48+
}
49+
return nil
50+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package actions
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/golang/mock/gomock"
9+
"github.com/sirupsen/logrus"
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/castai/cluster-controller/castai"
13+
"github.com/castai/cluster-controller/helm"
14+
mock_helm "github.com/castai/cluster-controller/helm/mock"
15+
)
16+
17+
func TestChartRollbackHandler(t *testing.T) {
18+
r := require.New(t)
19+
ctrl := gomock.NewController(t)
20+
helmMock := mock_helm.NewMockClient(ctrl)
21+
ctx := context.Background()
22+
23+
handler := newChartRollbackHandler(logrus.New(), helmMock)
24+
25+
t.Run("successfully rollback chart", func(t *testing.T) {
26+
action := newRollbackAction()
27+
28+
helmMock.EXPECT().Rollback(helm.RollbackOptions{
29+
Namespace: action.Namespace,
30+
ReleaseName: action.ReleaseName,
31+
}).Return(nil)
32+
33+
r.NoError(handler.Handle(ctx, action))
34+
})
35+
36+
t.Run("error when rolling back chart", func(t *testing.T) {
37+
action := newRollbackAction()
38+
someError := fmt.Errorf("some error")
39+
40+
helmMock.EXPECT().Rollback(helm.RollbackOptions{
41+
Namespace: action.Namespace,
42+
ReleaseName: action.ReleaseName,
43+
}).Return(someError)
44+
45+
r.Error(handler.Handle(ctx, action), someError)
46+
})
47+
48+
t.Run("namespace is missing in rollback action", func(t *testing.T) {
49+
action := newRollbackAction()
50+
action.Namespace = ""
51+
52+
r.Error(handler.Handle(ctx, action))
53+
})
54+
55+
t.Run("helm release is missing in rollback action", func(t *testing.T) {
56+
action := newRollbackAction()
57+
action.ReleaseName = ""
58+
59+
r.Error(handler.Handle(ctx, action))
60+
})
61+
}
62+
63+
func newRollbackAction() *castai.ActionChartRollback {
64+
return &castai.ActionChartRollback{
65+
Namespace: "test",
66+
ReleaseName: "new-release",
67+
}
68+
}

castai/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type ClusterAction struct {
2525
ActionApproveCSR *ActionApproveCSR `json:"actionApproveCsr,omitempty"`
2626
ActionChartUpsert *ActionChartUpsert `json:"actionChartUpsert,omitempty"`
2727
ActionChartUninstall *ActionChartUninstall `json:"actionChartUninstall,omitempty"`
28+
ActionChartRollback *ActionChartRollback `json:"actionChartRollback,omitempty"`
2829
ActionDisconnectCluster *ActionDisconnectCluster `json:"actionDisconnectCluster,omitempty"`
2930
ActionSendAKSInitData *ActionSendAKSInitData `json:"actionSendAksInitData,omitempty"`
3031
ActionCheckNodeDeleted *ActionCheckNodeDeleted `json:"actionCheckNodeDeleted,omitempty"`
@@ -55,6 +56,9 @@ func (c *ClusterAction) Data() interface{} {
5556
if c.ActionChartUninstall != nil {
5657
return c.ActionChartUninstall
5758
}
59+
if c.ActionChartRollback != nil {
60+
return c.ActionChartRollback
61+
}
5862
if c.ActionDisconnectCluster != nil {
5963
return c.ActionDisconnectCluster
6064
}
@@ -134,6 +138,11 @@ type ActionChartUninstall struct {
134138
ReleaseName string `json:"releaseName"`
135139
}
136140

141+
type ActionChartRollback struct {
142+
Namespace string `json:"namespace"`
143+
ReleaseName string `json:"releaseName"`
144+
}
145+
137146
type ChartSource struct {
138147
RepoURL string `json:"repoUrl"`
139148
Name string `json:"name"`

0 commit comments

Comments
 (0)