-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add chart rollback action (#37)
- Loading branch information
1 parent
4b1812b
commit 823836b
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters