-
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.
- Loading branch information
Showing
14 changed files
with
1,710 additions
and
23 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
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,73 @@ | ||
package actions | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
helmdriver "helm.sh/helm/v3/pkg/storage/driver" | ||
|
||
"github.com/castai/cluster-controller/castai" | ||
"github.com/castai/cluster-controller/helm" | ||
) | ||
|
||
func newChartUpsertHandler(log logrus.FieldLogger, helm helm.Client) ActionHandler { | ||
return &chartUpsertHandler{ | ||
log: log, | ||
helm: helm, | ||
} | ||
} | ||
|
||
type chartUpsertHandler struct { | ||
log logrus.FieldLogger | ||
helm helm.Client | ||
} | ||
|
||
func (c *chartUpsertHandler) Handle(ctx context.Context, data interface{}) error { | ||
req, ok := data.(*castai.ActionChartUpsert) | ||
if !ok { | ||
return fmt.Errorf("unexpected type %T for upsert chart handler", data) | ||
} | ||
|
||
if err := c.validateRequest(req); err != nil { | ||
return err | ||
} | ||
|
||
release, err := c.helm.GetRelease(helm.GetReleaseOptions{ | ||
Namespace: req.Namespace, | ||
ReleaseName: req.ReleaseName, | ||
}) | ||
if err != nil { | ||
if !errors.Is(err, helmdriver.ErrReleaseNotFound) { | ||
return fmt.Errorf("getting helm release %q in namespace %q: %w", req.ReleaseName, req.Namespace, err) | ||
} | ||
_, err := c.helm.Install(ctx, helm.InstallOptions{ | ||
ChartSource: &req.ChartSource, | ||
Namespace: req.Namespace, | ||
ReleaseName: req.ReleaseName, | ||
ValuesOverrides: req.ValuesOverrides, | ||
}) | ||
return err | ||
} | ||
|
||
_, err = c.helm.Upgrade(ctx, helm.UpgradeOptions{ | ||
ChartSource: &req.ChartSource, | ||
Release: release, | ||
ValuesOverrides: req.ValuesOverrides, | ||
}) | ||
return err | ||
} | ||
|
||
func (c *chartUpsertHandler) validateRequest(req *castai.ActionChartUpsert) error { | ||
if req.ReleaseName == "" { | ||
return errors.New("bad request: releaseName not provided") | ||
} | ||
if req.Namespace == "" { | ||
return errors.New("bad request: namespace not provided") | ||
} | ||
if err := req.ChartSource.Validate(); err != nil { | ||
return fmt.Errorf("validating chart source: %w", err) | ||
} | ||
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,80 @@ | ||
package actions | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
"helm.sh/helm/v3/pkg/release" | ||
helmdriver "helm.sh/helm/v3/pkg/storage/driver" | ||
|
||
"github.com/castai/cluster-controller/castai" | ||
"github.com/castai/cluster-controller/helm" | ||
mock_helm "github.com/castai/cluster-controller/helm/mock" | ||
) | ||
|
||
func TestChartUpsertHandler(t *testing.T) { | ||
r := require.New(t) | ||
ctrl := gomock.NewController(t) | ||
helmMock := mock_helm.NewMockClient(ctrl) | ||
ctx := context.Background() | ||
|
||
handler := newChartUpsertHandler(logrus.New(), helmMock) | ||
|
||
t.Run("install chart given release is not found", func(t *testing.T) { | ||
action := chartUpsertAction() | ||
|
||
helmMock.EXPECT().GetRelease(helm.GetReleaseOptions{ | ||
Namespace: action.Namespace, | ||
ReleaseName: action.ReleaseName, | ||
}).Return(nil, helmdriver.ErrReleaseNotFound) | ||
|
||
helmMock.EXPECT().Install(ctx, helm.InstallOptions{ | ||
ChartSource: &action.ChartSource, | ||
Namespace: action.Namespace, | ||
ReleaseName: action.ReleaseName, | ||
ValuesOverrides: action.ValuesOverrides, | ||
}).Return(nil, nil) | ||
|
||
r.NoError(handler.Handle(ctx, action)) | ||
}) | ||
|
||
t.Run("upgrade chart given release is found", func(t *testing.T) { | ||
action := chartUpsertAction() | ||
|
||
rel := &release.Release{ | ||
Name: "new-release", | ||
Version: 1, | ||
Namespace: "test", | ||
} | ||
|
||
helmMock.EXPECT().GetRelease(helm.GetReleaseOptions{ | ||
Namespace: action.Namespace, | ||
ReleaseName: action.ReleaseName, | ||
}).Return(rel, nil) | ||
|
||
helmMock.EXPECT().Upgrade(ctx, helm.UpgradeOptions{ | ||
ChartSource: &action.ChartSource, | ||
Release: rel, | ||
ValuesOverrides: action.ValuesOverrides, | ||
}).Return(nil, nil) | ||
|
||
r.NoError(handler.Handle(ctx, action)) | ||
}) | ||
|
||
} | ||
|
||
func chartUpsertAction() *castai.ActionChartUpsert { | ||
return &castai.ActionChartUpsert{ | ||
Namespace: "test", | ||
ReleaseName: "new-release", | ||
ValuesOverrides: map[string]string{"image.tag": "1.0.0"}, | ||
ChartSource: castai.ChartSource{ | ||
RepoURL: "https://my-charts.repo", | ||
Name: "super-chart", | ||
Version: "1.5.0", | ||
}, | ||
} | ||
} |
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
Oops, something went wrong.