Skip to content

Commit

Permalink
feat: add chart rollback action (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
varnastadeus authored Jul 7, 2022
1 parent 4b1812b commit 823836b
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func NewService(
reflect.TypeOf(&castai.ActionApproveCSR{}): newApproveCSRHandler(log, clientset),
reflect.TypeOf(&castai.ActionChartUpsert{}): newChartUpsertHandler(log, helmClient),
reflect.TypeOf(&castai.ActionChartUninstall{}): newChartUninstallHandler(log, helmClient),
reflect.TypeOf(&castai.ActionChartRollback{}): newChartRollbackHandler(log, helmClient),
reflect.TypeOf(&castai.ActionDisconnectCluster{}): newDisconnectClusterHandler(log, clientset),
reflect.TypeOf(&castai.ActionSendAKSInitData{}): newSendAKSInitDataHandler(log, castaiClient),
reflect.TypeOf(&castai.ActionCheckNodeDeleted{}): newCheckNodeDeletedHandler(log, clientset),
Expand Down
50 changes: 50 additions & 0 deletions actions/chart_rollback_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package actions

import (
"context"
"errors"
"fmt"

"github.com/sirupsen/logrus"

"github.com/castai/cluster-controller/castai"
"github.com/castai/cluster-controller/helm"
)

func newChartRollbackHandler(log logrus.FieldLogger, helm helm.Client) ActionHandler {
return &chartRollbackHandler{
log: log,
helm: helm,
}
}

type chartRollbackHandler struct {
log logrus.FieldLogger
helm helm.Client
}

func (c *chartRollbackHandler) Handle(_ context.Context, data interface{}) error {
req, ok := data.(*castai.ActionChartRollback)
if !ok {
return fmt.Errorf("unexpected type %T for chart rollback handler", data)
}

if err := c.validateRequest(req); err != nil {
return err
}

return c.helm.Rollback(helm.RollbackOptions{
ReleaseName: req.ReleaseName,
Namespace: req.Namespace,
})
}

func (c *chartRollbackHandler) validateRequest(req *castai.ActionChartRollback) error {
if req.ReleaseName == "" {
return errors.New("bad request: releaseName not provided")
}
if req.Namespace == "" {
return errors.New("bad request: namespace not provided")
}
return nil
}
68 changes: 68 additions & 0 deletions actions/chart_rollback_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package actions

import (
"context"
"fmt"
"testing"

"github.com/golang/mock/gomock"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"

"github.com/castai/cluster-controller/castai"
"github.com/castai/cluster-controller/helm"
mock_helm "github.com/castai/cluster-controller/helm/mock"
)

func TestChartRollbackHandler(t *testing.T) {
r := require.New(t)
ctrl := gomock.NewController(t)
helmMock := mock_helm.NewMockClient(ctrl)
ctx := context.Background()

handler := newChartRollbackHandler(logrus.New(), helmMock)

t.Run("successfully rollback chart", func(t *testing.T) {
action := newRollbackAction()

helmMock.EXPECT().Rollback(helm.RollbackOptions{
Namespace: action.Namespace,
ReleaseName: action.ReleaseName,
}).Return(nil)

r.NoError(handler.Handle(ctx, action))
})

t.Run("error when rolling back chart", func(t *testing.T) {
action := newRollbackAction()
someError := fmt.Errorf("some error")

helmMock.EXPECT().Rollback(helm.RollbackOptions{
Namespace: action.Namespace,
ReleaseName: action.ReleaseName,
}).Return(someError)

r.Error(handler.Handle(ctx, action), someError)
})

t.Run("namespace is missing in rollback action", func(t *testing.T) {
action := newRollbackAction()
action.Namespace = ""

r.Error(handler.Handle(ctx, action))
})

t.Run("helm release is missing in rollback action", func(t *testing.T) {
action := newRollbackAction()
action.ReleaseName = ""

r.Error(handler.Handle(ctx, action))
})
}

func newRollbackAction() *castai.ActionChartRollback {
return &castai.ActionChartRollback{
Namespace: "test",
ReleaseName: "new-release",
}
}
9 changes: 9 additions & 0 deletions castai/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ClusterAction struct {
ActionApproveCSR *ActionApproveCSR `json:"actionApproveCsr,omitempty"`
ActionChartUpsert *ActionChartUpsert `json:"actionChartUpsert,omitempty"`
ActionChartUninstall *ActionChartUninstall `json:"actionChartUninstall,omitempty"`
ActionChartRollback *ActionChartRollback `json:"actionChartRollback,omitempty"`
ActionDisconnectCluster *ActionDisconnectCluster `json:"actionDisconnectCluster,omitempty"`
ActionSendAKSInitData *ActionSendAKSInitData `json:"actionSendAksInitData,omitempty"`
ActionCheckNodeDeleted *ActionCheckNodeDeleted `json:"actionCheckNodeDeleted,omitempty"`
Expand Down Expand Up @@ -55,6 +56,9 @@ func (c *ClusterAction) Data() interface{} {
if c.ActionChartUninstall != nil {
return c.ActionChartUninstall
}
if c.ActionChartRollback != nil {
return c.ActionChartRollback
}
if c.ActionDisconnectCluster != nil {
return c.ActionDisconnectCluster
}
Expand Down Expand Up @@ -134,6 +138,11 @@ type ActionChartUninstall struct {
ReleaseName string `json:"releaseName"`
}

type ActionChartRollback struct {
Namespace string `json:"namespace"`
ReleaseName string `json:"releaseName"`
}

type ChartSource struct {
RepoURL string `json:"repoUrl"`
Name string `json:"name"`
Expand Down

0 comments on commit 823836b

Please sign in to comment.