From 7c3c031a503d841080064e127e86e6a6b163d86e Mon Sep 17 00:00:00 2001 From: Furkhat Kasymov Genii Uulu Date: Fri, 12 Apr 2024 16:32:06 +0300 Subject: [PATCH 1/2] ideomatic Go interface in helm package --- actions/chart_upsert_handler_test.go | 5 +-- castai/types.go | 23 ++---------- helm/chart_loader.go | 30 +++++----------- helm/chart_loader_test.go | 9 ++--- helm/client.go | 15 ++++---- helm/client_test.go | 28 +++++++-------- helm/mock/chart_loader.go | 52 ---------------------------- helm/types.go | 22 ++++++++++++ main.go | 2 +- version/mock/version.go | 2 +- 10 files changed, 59 insertions(+), 129 deletions(-) delete mode 100644 helm/mock/chart_loader.go create mode 100644 helm/types.go diff --git a/actions/chart_upsert_handler_test.go b/actions/chart_upsert_handler_test.go index d4090cc9..a8f1ce76 100644 --- a/actions/chart_upsert_handler_test.go +++ b/actions/chart_upsert_handler_test.go @@ -2,9 +2,10 @@ package actions import ( "context" - "github.com/google/uuid" "testing" + "github.com/google/uuid" + "github.com/golang/mock/gomock" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" @@ -108,7 +109,7 @@ func chartUpsertAction() *castai.ActionChartUpsert { Namespace: "test", ReleaseName: "new-release", ValuesOverrides: map[string]string{"image.tag": "1.0.0"}, - ChartSource: castai.ChartSource{ + ChartSource: helm.ChartSource{ RepoURL: "https://my-charts.repo", Name: "super-chart", Version: "1.5.0", diff --git a/castai/types.go b/castai/types.go index 527c62ab..258e1cd9 100644 --- a/castai/types.go +++ b/castai/types.go @@ -1,10 +1,10 @@ package castai import ( - "errors" "fmt" "time" + "github.com/castai/cluster-controller/helm" "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" ) @@ -204,7 +204,7 @@ type ActionChartUpsert struct { Namespace string `json:"namespace"` ReleaseName string `json:"releaseName"` ValuesOverrides map[string]string `json:"valuesOverrides,omitempty"` - ChartSource ChartSource `json:"chartSource"` + ChartSource helm.ChartSource `json:"chartSource"` CreateNamespace bool `json:"createNamespace"` } @@ -219,25 +219,6 @@ type ActionChartRollback struct { Version string `json:"version"` } -type ChartSource struct { - RepoURL string `json:"repoUrl"` - Name string `json:"name"` - Version string `json:"version"` -} - -func (c *ChartSource) Validate() error { - if c.Name == "" { - return errors.New("chart name is not set") - } - if c.RepoURL == "" { - return errors.New("chart repoURL is not set") - } - if c.Version == "" { - return errors.New("chart version is not set") - } - return nil -} - type AKSInitDataRequest struct { CloudConfigBase64 string `json:"cloudConfigBase64"` ProtectedSettingsBase64 string `json:"protectedSettingsBase64"` diff --git a/helm/chart_loader.go b/helm/chart_loader.go index c156ac92..9b083f5c 100644 --- a/helm/chart_loader.go +++ b/helm/chart_loader.go @@ -1,5 +1,3 @@ -//go:generate mockgen -destination ./mock/chart_loader.go . ChartLoader - package helm import ( @@ -15,40 +13,28 @@ import ( "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/getter" "helm.sh/helm/v3/pkg/repo" - - "github.com/castai/cluster-controller/castai" ) -type ChartLoader interface { - Load(ctx context.Context, c *castai.ChartSource) (*chart.Chart, error) -} - -func NewChartLoader() ChartLoader { - return &remoteChartLoader{} -} - -// remoteChartLoader fetches chart from remote source by given url. -type remoteChartLoader struct { -} +type chartLoaderFunc func(ctx context.Context, c *ChartSource) (*chart.Chart, error) -func (cl *remoteChartLoader) Load(ctx context.Context, c *castai.ChartSource) (*chart.Chart, error) { +func loadRemoteChart(ctx context.Context, c *ChartSource) (*chart.Chart, error) { var res *chart.Chart err := backoff.Retry(func() error { var archiveURL string if strings.HasSuffix(c.RepoURL, ".tgz") { archiveURL = c.RepoURL } else { - index, err := cl.downloadHelmIndex(c.RepoURL) + index, err := downloadHelmIndex(c.RepoURL) if err != nil { return err } - archiveURL, err = cl.chartURL(index, c.Name, c.Version) + archiveURL, err = chartURL(index, c.Name, c.Version) if err != nil { return err } } - archiveResp, err := cl.fetchArchive(ctx, archiveURL) + archiveResp, err := fetchArchive(ctx, archiveURL) if err != nil { return err } @@ -67,7 +53,7 @@ func (cl *remoteChartLoader) Load(ctx context.Context, c *castai.ChartSource) (* return res, nil } -func (cl *remoteChartLoader) fetchArchive(ctx context.Context, archiveURL string) (*http.Response, error) { +func fetchArchive(ctx context.Context, archiveURL string) (*http.Response, error) { httpClient := &http.Client{ Timeout: 30 * time.Second, } @@ -90,7 +76,7 @@ func defaultBackoff(ctx context.Context) backoff.BackOffContext { return backoff.WithContext(backoff.WithMaxRetries(backoff.NewConstantBackOff(1*time.Second), 5), ctx) } -func (cl *remoteChartLoader) downloadHelmIndex(repoURL string) (*repo.IndexFile, error) { +func downloadHelmIndex(repoURL string) (*repo.IndexFile, error) { r, err := repo.NewChartRepository(&repo.Entry{URL: repoURL}, getter.All(&cli.EnvSettings{})) if err != nil { return nil, fmt.Errorf("initializing chart repo %s: %w", repoURL, err) @@ -109,7 +95,7 @@ func (cl *remoteChartLoader) downloadHelmIndex(repoURL string) (*repo.IndexFile, return index, nil } -func (cl *remoteChartLoader) chartURL(index *repo.IndexFile, name, version string) (string, error) { +func chartURL(index *repo.IndexFile, name, version string) (string, error) { for _, c := range index.Entries[name] { if c.Version == version && len(c.URLs) > 0 { return c.URLs[0], nil diff --git a/helm/chart_loader_test.go b/helm/chart_loader_test.go index 1f7a731b..5ee9e540 100644 --- a/helm/chart_loader_test.go +++ b/helm/chart_loader_test.go @@ -6,23 +6,20 @@ import ( "time" "github.com/stretchr/testify/require" - - "github.com/castai/cluster-controller/castai" ) -func TestIntegration_ChartLoader(t *testing.T) { +func TestIntegration_loadRemoteChart(t *testing.T) { r := require.New(t) ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel() - chart := &castai.ChartSource{ + chart := &ChartSource{ RepoURL: "https://castai.github.io/helm-charts", Name: "castai-cluster-controller", Version: "0.4.3", } - loader := NewChartLoader() - c, err := loader.Load(ctx, chart) + c, err := loadRemoteChart(ctx, chart) r.NoError(err) r.Equal(chart.Name, c.Name()) r.Equal(chart.Version, c.Metadata.Version) diff --git a/helm/client.go b/helm/client.go index f373e197..5c435827 100644 --- a/helm/client.go +++ b/helm/client.go @@ -20,12 +20,11 @@ import ( "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" - "github.com/castai/cluster-controller/castai" "github.com/castai/cluster-controller/helm/hook" ) type InstallOptions struct { - ChartSource *castai.ChartSource + ChartSource *ChartSource Namespace string CreateNamespace bool ReleaseName string @@ -38,7 +37,7 @@ type UninstallOptions struct { } type UpgradeOptions struct { - ChartSource *castai.ChartSource + ChartSource *ChartSource Release *release.Release ValuesOverrides map[string]string MaxHistory int @@ -54,7 +53,7 @@ type RollbackOptions struct { ReleaseName string } -func NewClient(log logrus.FieldLogger, loader ChartLoader, restConfig *rest.Config) Client { +func NewClient(log logrus.FieldLogger, restConfig *rest.Config) Client { return &client{ log: log, configurationGetter: &configurationGetter{ @@ -63,7 +62,7 @@ func NewClient(log logrus.FieldLogger, loader ChartLoader, restConfig *rest.Conf helmDriver: "secrets", k8sConfig: restConfig, }, - chartLoader: loader, + loadChart: loadRemoteChart, } } @@ -78,11 +77,11 @@ type Client interface { type client struct { log logrus.FieldLogger configurationGetter ConfigurationGetter - chartLoader ChartLoader + loadChart chartLoaderFunc } func (c *client) Install(ctx context.Context, opts InstallOptions) (*release.Release, error) { - ch, err := c.chartLoader.Load(ctx, opts.ChartSource) + ch, err := c.loadChart(ctx, opts.ChartSource) if err != nil { return nil, err } @@ -134,7 +133,7 @@ func (c *client) Uninstall(opts UninstallOptions) (*release.UninstallReleaseResp } func (c *client) Upgrade(ctx context.Context, opts UpgradeOptions) (*release.Release, error) { - ch, err := c.chartLoader.Load(ctx, opts.ChartSource) + ch, err := c.loadChart(ctx, opts.ChartSource) if err != nil { return nil, err } diff --git a/helm/client_test.go b/helm/client_test.go index 4e9ef535..8cbf4cb3 100644 --- a/helm/client_test.go +++ b/helm/client_test.go @@ -15,16 +15,16 @@ import ( "helm.sh/helm/v3/pkg/storage" "helm.sh/helm/v3/pkg/storage/driver" "helm.sh/helm/v3/pkg/time" - - "github.com/castai/cluster-controller/castai" ) func TestClientInstall(t *testing.T) { r := require.New(t) client := &client{ - log: logrus.New(), - chartLoader: &testChartLoader{chart: buildNginxIngressChart()}, + log: logrus.New(), + loadChart: func(_ context.Context, _ *ChartSource) (*chart.Chart, error) { + return buildNginxIngressChart(), nil + }, configurationGetter: &testConfigurationGetter{t: t}, } @@ -51,8 +51,10 @@ func TestClientUpdate(t *testing.T) { currentRelease := buildNginxIngressRelease(release.StatusDeployed) client := &client{ - log: logrus.New(), - chartLoader: &testChartLoader{chart: buildNginxIngressChart()}, + log: logrus.New(), + loadChart: func(_ context.Context, _ *ChartSource) (*chart.Chart, error) { + return buildNginxIngressChart(), nil + }, configurationGetter: &testConfigurationGetter{ t: t, currentRelease: currentRelease, @@ -80,8 +82,10 @@ func TestClientUninstall(t *testing.T) { currentRelease := buildNginxIngressRelease(release.StatusDeployed) client := &client{ - log: logrus.New(), - chartLoader: &testChartLoader{chart: buildNginxIngressChart()}, + log: logrus.New(), + loadChart: func(_ context.Context, _ *ChartSource) (*chart.Chart, error) { + return buildNginxIngressChart(), nil + }, configurationGetter: &testConfigurationGetter{ t: t, currentRelease: currentRelease, @@ -120,14 +124,6 @@ func (c *testConfigurationGetter) Get(_ string) (*action.Configuration, error) { return cfg, nil } -type testChartLoader struct { - chart *chart.Chart -} - -func (t *testChartLoader) Load(_ context.Context, _ *castai.ChartSource) (*chart.Chart, error) { - return t.chart, nil -} - func buildNginxIngressChart() *chart.Chart { return &chart.Chart{ Metadata: &chart.Metadata{ diff --git a/helm/mock/chart_loader.go b/helm/mock/chart_loader.go deleted file mode 100644 index 4cf60f98..00000000 --- a/helm/mock/chart_loader.go +++ /dev/null @@ -1,52 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/castai/cluster-controller/helm (interfaces: ChartLoader) - -// Package mock_helm is a generated GoMock package. -package mock_helm - -import ( - context "context" - reflect "reflect" - - castai "github.com/castai/cluster-controller/castai" - gomock "github.com/golang/mock/gomock" - chart "helm.sh/helm/v3/pkg/chart" -) - -// MockChartLoader is a mock of ChartLoader interface. -type MockChartLoader struct { - ctrl *gomock.Controller - recorder *MockChartLoaderMockRecorder -} - -// MockChartLoaderMockRecorder is the mock recorder for MockChartLoader. -type MockChartLoaderMockRecorder struct { - mock *MockChartLoader -} - -// NewMockChartLoader creates a new mock instance. -func NewMockChartLoader(ctrl *gomock.Controller) *MockChartLoader { - mock := &MockChartLoader{ctrl: ctrl} - mock.recorder = &MockChartLoaderMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockChartLoader) EXPECT() *MockChartLoaderMockRecorder { - return m.recorder -} - -// Load mocks base method. -func (m *MockChartLoader) Load(arg0 context.Context, arg1 *castai.ChartSource) (*chart.Chart, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Load", arg0, arg1) - ret0, _ := ret[0].(*chart.Chart) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Load indicates an expected call of Load. -func (mr *MockChartLoaderMockRecorder) Load(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Load", reflect.TypeOf((*MockChartLoader)(nil).Load), arg0, arg1) -} diff --git a/helm/types.go b/helm/types.go new file mode 100644 index 00000000..ee7136fe --- /dev/null +++ b/helm/types.go @@ -0,0 +1,22 @@ +package helm + +import "errors" + +type ChartSource struct { + RepoURL string `json:"repoUrl"` + Name string `json:"name"` + Version string `json:"version"` +} + +func (c *ChartSource) Validate() error { + if c.Name == "" { + return errors.New("chart name is not set") + } + if c.RepoURL == "" { + return errors.New("chart repoURL is not set") + } + if c.Version == "" { + return errors.New("chart version is not set") + } + return nil +} diff --git a/main.go b/main.go index e8abda23..2c33aa15 100644 --- a/main.go +++ b/main.go @@ -123,7 +123,7 @@ func run( restConfigLeader.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(float32(cfg.KubeClient.QPS), cfg.KubeClient.Burst) restConfigDynamic.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(float32(cfg.KubeClient.QPS), cfg.KubeClient.Burst) - helmClient := helm.NewClient(logger, helm.NewChartLoader(), restconfig) + helmClient := helm.NewClient(logger, restconfig) clientset, err := kubernetes.NewForConfig(restconfig) if err != nil { diff --git a/version/mock/version.go b/version/mock/version.go index 77f2dd73..a0d2f903 100644 --- a/version/mock/version.go +++ b/version/mock/version.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: castai-agent/internal/services/version (interfaces: Interface) +// Source: github.com/castai/cluster-controller/version (interfaces: Interface) // Package mock_version is a generated GoMock package. package mock_version From fd5dec98e3cb946f1f6ce20bc3de249fb01ef764 Mon Sep 17 00:00:00 2001 From: Furkhat Kasymov Genii Uulu Date: Mon, 15 Apr 2024 09:33:14 +0300 Subject: [PATCH 2/2] idiomatic Go interface helm/actions WIP --- actions/actions.go | 82 ++++++----- actions/actions_test.go | 41 +++--- actions/approve_csr_handler.go | 8 +- actions/approve_csr_handler_test.go | 25 ++-- actions/chart_rollback_handler.go | 12 +- actions/chart_rollback_handler_test.go | 23 ++-- actions/chart_uninstall_handler.go | 12 +- actions/chart_uninstall_handler_test.go | 20 +-- actions/chart_upsert_handler.go | 12 +- actions/chart_upsert_handler_test.go | 15 +-- actions/check_node_deleted.go | 11 +- actions/check_node_handler_test.go | 14 +- actions/check_node_status.go | 21 ++- actions/check_node_status_test.go | 65 +++++---- actions/create_event_handler.go | 6 +- actions/create_event_handler_test.go | 12 +- actions/create_handler.go | 7 +- actions/create_handler_test.go | 29 ++-- actions/delete_handler.go | 7 +- actions/delete_handler_test.go | 35 +++-- actions/delete_node_handler.go | 11 +- actions/delete_node_handler_test.go | 23 ++-- actions/disconnect_cluster_handler.go | 6 +- actions/disconnect_cluster_handler_test.go | 10 +- actions/drain_node_handler.go | 11 +- actions/drain_node_handler_test.go | 39 +++--- actions/mock_actions/client_mock.go | 79 +++++++++++ actions/mock_actions/helm_client_mock.go | 111 +++++++++++++++ actions/mock_test.go | 60 +++++++++ actions/patch_handler.go | 8 +- actions/patch_handler_test.go | 23 ++-- actions/patch_node_handler.go | 11 +- actions/patch_node_handler_test.go | 17 ++- actions/send_aks_init_data_handler.go | 11 +- actions/send_aks_init_data_handler_test.go | 13 +- {castai => actions/types}/types.go | 20 +-- castai/client.go | 25 ++-- helm/client.go | 12 +- helm/mock/client.go | 150 --------------------- log/exporter_test.go | 2 +- main.go | 6 +- 41 files changed, 595 insertions(+), 510 deletions(-) create mode 100644 actions/mock_actions/client_mock.go create mode 100644 actions/mock_actions/helm_client_mock.go create mode 100644 actions/mock_test.go rename {castai => actions/types}/types.go (93%) delete mode 100644 helm/mock/client.go diff --git a/actions/actions.go b/actions/actions.go index a08cd9d2..4db0a2dd 100644 --- a/actions/actions.go +++ b/actions/actions.go @@ -1,3 +1,5 @@ +//go:generate mockgen -destination ./mock_actions/helm_client_mock.go github.com/castai/cluster-controller/actions HelmClient +//go:generate mockgen -destination ./mock_actions/client_mock.go github.com/castai/cluster-controller/actions Client package actions import ( @@ -12,10 +14,11 @@ import ( "github.com/cenkalti/backoff/v4" "github.com/sirupsen/logrus" + "helm.sh/helm/v3/pkg/release" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" - "github.com/castai/cluster-controller/castai" + "github.com/castai/cluster-controller/actions/types" "github.com/castai/cluster-controller/health" "github.com/castai/cluster-controller/helm" ) @@ -23,7 +26,10 @@ import ( const ( // actionIDLogField is the log field name for action ID. // This field is used in backend to detect actions ID in logs. - actionIDLogField = "id" + actionIDLogField = "id" + labelNodeID = "provisioner.cast.ai/node-id" + actionCheckNodeStatus_READY types.ActionCheckNodeStatus_Status = "NodeStatus_READY" + actionCheckNodeStatus_DELETED types.ActionCheckNodeStatus_Status = "NodeStatus_DELETED" ) func newUnexpectedTypeErr(value interface{}, expectedType interface{}) error { @@ -46,7 +52,21 @@ type Service interface { } type ActionHandler interface { - Handle(ctx context.Context, action *castai.ClusterAction) error + Handle(ctx context.Context, action *types.ClusterAction) error +} + +type Client interface { + GetActions(ctx context.Context, k8sVersion string) ([]*types.ClusterAction, error) + AckAction(ctx context.Context, actionID string, req *types.AckClusterActionRequest) error + SendAKSInitData(ctx context.Context, req *types.AKSInitDataRequest) error +} + +type HelmClient interface { + Install(ctx context.Context, opts helm.InstallOptions) (*release.Release, error) + Uninstall(opts helm.UninstallOptions) (*release.UninstallReleaseResponse, error) + Upgrade(ctx context.Context, opts helm.UpgradeOptions) (*release.Release, error) + Rollback(opts helm.RollbackOptions) error + GetRelease(opts helm.GetReleaseOptions) (*release.Release, error) } func NewService( @@ -55,41 +75,41 @@ func NewService( k8sVersion string, clientset *kubernetes.Clientset, dynamicClient dynamic.Interface, - castaiClient castai.Client, - helmClient helm.Client, + client Client, + helmClient HelmClient, healthCheck *health.HealthzProvider, ) Service { return &service{ log: log, cfg: cfg, k8sVersion: k8sVersion, - castAIClient: castaiClient, + client: client, startedActions: map[string]struct{}{}, actionHandlers: map[reflect.Type]ActionHandler{ - reflect.TypeOf(&castai.ActionDeleteNode{}): newDeleteNodeHandler(log, clientset), - reflect.TypeOf(&castai.ActionDrainNode{}): newDrainNodeHandler(log, clientset, cfg.Namespace), - reflect.TypeOf(&castai.ActionPatchNode{}): newPatchNodeHandler(log, clientset), - reflect.TypeOf(&castai.ActionCreateEvent{}): newCreateEventHandler(log, clientset), - 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, cfg.Version), - reflect.TypeOf(&castai.ActionDisconnectCluster{}): newDisconnectClusterHandler(log, clientset), - reflect.TypeOf(&castai.ActionSendAKSInitData{}): newSendAKSInitDataHandler(log, castaiClient), - reflect.TypeOf(&castai.ActionCheckNodeDeleted{}): newCheckNodeDeletedHandler(log, clientset), - reflect.TypeOf(&castai.ActionCheckNodeStatus{}): newCheckNodeStatusHandler(log, clientset), - reflect.TypeOf(&castai.ActionPatch{}): newPatchHandler(log, dynamicClient), - reflect.TypeOf(&castai.ActionCreate{}): newCreateHandler(log, dynamicClient), - reflect.TypeOf(&castai.ActionDelete{}): newDeleteHandler(log, dynamicClient), + reflect.TypeOf(&types.ActionDeleteNode{}): newDeleteNodeHandler(log, clientset), + reflect.TypeOf(&types.ActionDrainNode{}): newDrainNodeHandler(log, clientset, cfg.Namespace), + reflect.TypeOf(&types.ActionPatchNode{}): newPatchNodeHandler(log, clientset), + reflect.TypeOf(&types.ActionCreateEvent{}): newCreateEventHandler(log, clientset), + reflect.TypeOf(&types.ActionApproveCSR{}): newApproveCSRHandler(log, clientset), + reflect.TypeOf(&types.ActionChartUpsert{}): newChartUpsertHandler(log, helmClient), + reflect.TypeOf(&types.ActionChartUninstall{}): newChartUninstallHandler(log, helmClient), + reflect.TypeOf(&types.ActionChartRollback{}): newChartRollbackHandler(log, helmClient, cfg.Version), + reflect.TypeOf(&types.ActionDisconnectCluster{}): newDisconnectClusterHandler(log, clientset), + reflect.TypeOf(&types.ActionSendAKSInitData{}): newSendAKSInitDataHandler(log, client), + reflect.TypeOf(&types.ActionCheckNodeDeleted{}): newCheckNodeDeletedHandler(log, clientset), + reflect.TypeOf(&types.ActionCheckNodeStatus{}): newCheckNodeStatusHandler(log, clientset), + reflect.TypeOf(&types.ActionPatch{}): newPatchHandler(log, dynamicClient), + reflect.TypeOf(&types.ActionCreate{}): newCreateHandler(log, dynamicClient), + reflect.TypeOf(&types.ActionDelete{}): newDeleteHandler(log, dynamicClient), }, healthCheck: healthCheck, } } type service struct { - log logrus.FieldLogger - cfg Config - castAIClient castai.Client + log logrus.FieldLogger + cfg Config + client Client k8sVersion string @@ -127,7 +147,7 @@ func (s *service) doWork(ctx context.Context) error { s.log.Info("polling actions") start := time.Now() var ( - actions []*castai.ClusterAction + actions []*types.ClusterAction err error iteration int ) @@ -135,7 +155,7 @@ func (s *service) doWork(ctx context.Context) error { b := backoff.WithContext(backoff.WithMaxRetries(backoff.NewConstantBackOff(5*time.Second), 3), ctx) errR := backoff.Retry(func() error { iteration++ - actions, err = s.castAIClient.GetActions(ctx, s.k8sVersion) + actions, err = s.client.GetActions(ctx, s.k8sVersion) if err != nil { s.log.Errorf("polling actions: get action request failed: iteration: %v %v", iteration, err) return err @@ -158,13 +178,13 @@ func (s *service) doWork(ctx context.Context) error { return nil } -func (s *service) handleActions(ctx context.Context, actions []*castai.ClusterAction) { +func (s *service) handleActions(ctx context.Context, actions []*types.ClusterAction) { for _, action := range actions { if !s.startProcessing(action.ID) { continue } - go func(action *castai.ClusterAction) { + go func(action *types.ClusterAction) { defer s.finishProcessing(action.ID) var err error @@ -211,7 +231,7 @@ func (s *service) startProcessing(actionID string) bool { return true } -func (s *service) handleAction(ctx context.Context, action *castai.ClusterAction) (err error) { +func (s *service) handleAction(ctx context.Context, action *types.ClusterAction) (err error) { actionType := reflect.TypeOf(action.Data()) defer func() { @@ -235,7 +255,7 @@ func (s *service) handleAction(ctx context.Context, action *castai.ClusterAction return nil } -func (s *service) ackAction(ctx context.Context, action *castai.ClusterAction, handleErr error) error { +func (s *service) ackAction(ctx context.Context, action *types.ClusterAction, handleErr error) error { actionType := reflect.TypeOf(action.Data()) s.log.WithFields(logrus.Fields{ actionIDLogField: action.ID, @@ -245,7 +265,7 @@ func (s *service) ackAction(ctx context.Context, action *castai.ClusterAction, h return backoff.RetryNotify(func() error { ctx, cancel := context.WithTimeout(ctx, s.cfg.AckTimeout) defer cancel() - return s.castAIClient.AckAction(ctx, action.ID, &castai.AckClusterActionRequest{ + return s.client.AckAction(ctx, action.ID, &types.AckClusterActionRequest{ Error: getHandlerError(handleErr), }) }, backoff.WithContext( diff --git a/actions/actions_test.go b/actions/actions_test.go index 32e6d73b..bf8729d9 100644 --- a/actions/actions_test.go +++ b/actions/actions_test.go @@ -12,8 +12,7 @@ import ( "github.com/stretchr/testify/require" "go.uber.org/goleak" - "github.com/castai/cluster-controller/castai" - "github.com/castai/cluster-controller/castai/mock" + "github.com/castai/cluster-controller/actions/types" "github.com/castai/cluster-controller/health" ) @@ -33,7 +32,7 @@ func TestActions(t *testing.T) { ClusterID: uuid.New().String(), } - newTestService := func(handler ActionHandler, client castai.Client) *service { + newTestService := func(handler ActionHandler, client Client) *service { svc := NewService( log, cfg, @@ -55,30 +54,30 @@ func TestActions(t *testing.T) { t.Run("poll handle and ack", func(t *testing.T) { r := require.New(t) - apiActions := []*castai.ClusterAction{ + apiActions := []*types.ClusterAction{ { ID: "a1", CreatedAt: time.Now(), - ActionDeleteNode: &castai.ActionDeleteNode{ + ActionDeleteNode: &types.ActionDeleteNode{ NodeName: "n1", }, }, { ID: "a2", CreatedAt: time.Now(), - ActionDrainNode: &castai.ActionDrainNode{ + ActionDrainNode: &types.ActionDrainNode{ NodeName: "n1", }, }, { ID: "a3", CreatedAt: time.Now(), - ActionPatchNode: &castai.ActionPatchNode{ + ActionPatchNode: &types.ActionPatchNode{ NodeName: "n1", }, }, } - client := mock.NewMockAPIClient(apiActions) + client := newMockAPIClient(apiActions) handler := &mockAgentActionHandler{handleDelay: 2 * time.Millisecond} svc := newTestService(handler, client) ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) @@ -102,7 +101,7 @@ func TestActions(t *testing.T) { t.Run("continue polling on api error", func(t *testing.T) { r := require.New(t) - client := mock.NewMockAPIClient([]*castai.ClusterAction{}) + client := newMockAPIClient([]*types.ClusterAction{}) client.GetActionsErr = errors.New("ups") handler := &mockAgentActionHandler{err: errors.New("ups")} svc := newTestService(handler, client) @@ -119,16 +118,16 @@ func TestActions(t *testing.T) { t.Run("do not ack action on context canceled error", func(t *testing.T) { r := require.New(t) - apiActions := []*castai.ClusterAction{ + apiActions := []*types.ClusterAction{ { ID: "a1", CreatedAt: time.Now(), - ActionPatchNode: &castai.ActionPatchNode{ + ActionPatchNode: &types.ActionPatchNode{ NodeName: "n1", }, }, } - client := mock.NewMockAPIClient(apiActions) + client := newMockAPIClient(apiActions) handler := &mockAgentActionHandler{err: context.Canceled} svc := newTestService(handler, client) ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) @@ -145,16 +144,16 @@ func TestActions(t *testing.T) { t.Run("ack with error when action handler failed", func(t *testing.T) { r := require.New(t) - apiActions := []*castai.ClusterAction{ + apiActions := []*types.ClusterAction{ { ID: "a1", CreatedAt: time.Now(), - ActionPatchNode: &castai.ActionPatchNode{ + ActionPatchNode: &types.ActionPatchNode{ NodeName: "n1", }, }, } - client := mock.NewMockAPIClient(apiActions) + client := newMockAPIClient(apiActions) handler := &mockAgentActionHandler{err: errors.New("ups")} svc := newTestService(handler, client) ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) @@ -165,7 +164,7 @@ func TestActions(t *testing.T) { r.Empty(client.Actions) r.Len(client.Acks, 1) r.Equal("a1", client.Acks[0].ActionID) - r.Equal("handling action *castai.ActionPatchNode: ups", *client.Acks[0].Err) + r.Equal("handling action *ActionPatchNode: ups", *client.Acks[0].Err) }() svc.Run(ctx) }) @@ -173,16 +172,16 @@ func TestActions(t *testing.T) { t.Run("ack with error when action handler panic occurred", func(t *testing.T) { r := require.New(t) - apiActions := []*castai.ClusterAction{ + apiActions := []*types.ClusterAction{ { ID: "a1", CreatedAt: time.Now(), - ActionPatchNode: &castai.ActionPatchNode{ + ActionPatchNode: &types.ActionPatchNode{ NodeName: "n1", }, }, } - client := mock.NewMockAPIClient(apiActions) + client := newMockAPIClient(apiActions) handler := &mockAgentActionHandler{panicErr: errors.New("ups")} svc := newTestService(handler, client) ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) @@ -193,7 +192,7 @@ func TestActions(t *testing.T) { r.Empty(client.Actions) r.Len(client.Acks, 1) r.Equal("a1", client.Acks[0].ActionID) - r.Contains(*client.Acks[0].Err, "panic: handling action *castai.ActionPatchNode: ups: goroutine") + r.Contains(*client.Acks[0].Err, "panic: handling action *ActionPatchNode: ups: goroutine") }() svc.Run(ctx) }) @@ -205,7 +204,7 @@ type mockAgentActionHandler struct { handleDelay time.Duration } -func (m *mockAgentActionHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { +func (m *mockAgentActionHandler) Handle(ctx context.Context, action *types.ClusterAction) error { time.Sleep(m.handleDelay) if m.panicErr != nil { panic(m.panicErr) diff --git a/actions/approve_csr_handler.go b/actions/approve_csr_handler.go index 72059920..08ab4ad8 100644 --- a/actions/approve_csr_handler.go +++ b/actions/approve_csr_handler.go @@ -11,7 +11,7 @@ import ( "github.com/sirupsen/logrus" "k8s.io/client-go/kubernetes" - "github.com/castai/cluster-controller/castai" + "github.com/castai/cluster-controller/actions/types" "github.com/castai/cluster-controller/csr" ) @@ -31,15 +31,15 @@ type approveCSRHandler struct { csrFetchInterval time.Duration } -func (h *approveCSRHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionApproveCSR) +func (h *approveCSRHandler) Handle(ctx context.Context, action *types.ClusterAction) error { + req, ok := action.Data().(*types.ActionApproveCSR) if !ok { return fmt.Errorf("unexpected type %T for approve csr handler", action.Data()) } log := h.log.WithFields(logrus.Fields{ "node_name": req.NodeName, "node_id": req.NodeID, - "type": reflect.TypeOf(action.Data().(*castai.ActionApproveCSR)).String(), + "type": reflect.TypeOf(action.Data().(*types.ActionApproveCSR)).String(), actionIDLogField: action.ID, }) diff --git a/actions/approve_csr_handler_test.go b/actions/approve_csr_handler_test.go index 7a4ef30b..dfa784c8 100644 --- a/actions/approve_csr_handler_test.go +++ b/actions/approve_csr_handler_test.go @@ -4,11 +4,12 @@ import ( "context" "errors" "fmt" - "github.com/google/uuid" "sync/atomic" "testing" "time" + "github.com/google/uuid" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" certv1 "k8s.io/api/certificates/v1" @@ -22,7 +23,7 @@ import ( "k8s.io/client-go/kubernetes/fake" ktest "k8s.io/client-go/testing" - "github.com/castai/cluster-controller/castai" + "github.com/castai/cluster-controller/actions/types" "github.com/castai/cluster-controller/csr" ) @@ -57,9 +58,9 @@ func TestApproveCSRHandler(t *testing.T) { return true, approved, nil }) - actionApproveCSR := &castai.ClusterAction{ + actionApproveCSR := &types.ClusterAction{ ID: uuid.New().String(), - ActionApproveCSR: &castai.ActionApproveCSR{NodeName: "gke-am-gcp-cast-5dc4f4ec"}, + ActionApproveCSR: &types.ActionApproveCSR{NodeName: "gke-am-gcp-cast-5dc4f4ec"}, CreatedAt: time.Time{}, } @@ -90,9 +91,9 @@ func TestApproveCSRHandler(t *testing.T) { } client := fake.NewSimpleClientset(csrRes) - actionApproveCSR := &castai.ClusterAction{ + actionApproveCSR := &types.ClusterAction{ ID: uuid.New().String(), - ActionApproveCSR: &castai.ActionApproveCSR{NodeName: "gke-am-gcp-cast-5dc4f4ec"}, + ActionApproveCSR: &types.ActionApproveCSR{NodeName: "gke-am-gcp-cast-5dc4f4ec"}, CreatedAt: time.Time{}, } h := &approveCSRHandler{ @@ -123,9 +124,9 @@ func TestApproveCSRHandler(t *testing.T) { client := fake.NewSimpleClientset(csrRes) client.PrependReactor("list", "certificatesigningrequests", fn) - actionApproveCSR := &castai.ClusterAction{ + actionApproveCSR := &types.ClusterAction{ ID: uuid.New().String(), - ActionApproveCSR: &castai.ActionApproveCSR{NodeName: "gke-am-gcp-cast-5dc4f4ec"}, + ActionApproveCSR: &types.ActionApproveCSR{NodeName: "gke-am-gcp-cast-5dc4f4ec"}, CreatedAt: time.Time{}, } h := &approveCSRHandler{ @@ -182,9 +183,9 @@ AiAHVYZXHxxspoV0hcfn2Pdsl89fIPCOFy/K1PqSUR6QNAIgYdt51ZbQt9rgM2BD return true, approved, nil }) - actionApproveCSR := &castai.ClusterAction{ + actionApproveCSR := &types.ClusterAction{ ID: uuid.New().String(), - ActionApproveCSR: &castai.ActionApproveCSR{NodeName: "gke-am-gcp-cast-5dc4f4ec"}, + ActionApproveCSR: &types.ActionApproveCSR{NodeName: "gke-am-gcp-cast-5dc4f4ec"}, CreatedAt: time.Time{}, } h := &approveCSRHandler{ @@ -214,9 +215,9 @@ AiAHVYZXHxxspoV0hcfn2Pdsl89fIPCOFy/K1PqSUR6QNAIgYdt51ZbQt9rgM2BD watcher.Stop() client.PrependWatchReactor("certificatesigningrequests", ktest.DefaultWatchReactor(watcher, nil)) - actionApproveCSR := &castai.ClusterAction{ + actionApproveCSR := &types.ClusterAction{ ID: uuid.New().String(), - ActionApproveCSR: &castai.ActionApproveCSR{NodeName: "node"}, + ActionApproveCSR: &types.ActionApproveCSR{NodeName: "node"}, CreatedAt: time.Time{}, } diff --git a/actions/chart_rollback_handler.go b/actions/chart_rollback_handler.go index 17c0060c..128e3e9c 100644 --- a/actions/chart_rollback_handler.go +++ b/actions/chart_rollback_handler.go @@ -7,11 +7,11 @@ import ( "github.com/sirupsen/logrus" - "github.com/castai/cluster-controller/castai" + "github.com/castai/cluster-controller/actions/types" "github.com/castai/cluster-controller/helm" ) -func newChartRollbackHandler(log logrus.FieldLogger, helm helm.Client, version string) ActionHandler { +func newChartRollbackHandler(log logrus.FieldLogger, helm HelmClient, version string) ActionHandler { return &chartRollbackHandler{ log: log, helm: helm, @@ -21,12 +21,12 @@ func newChartRollbackHandler(log logrus.FieldLogger, helm helm.Client, version s type chartRollbackHandler struct { log logrus.FieldLogger - helm helm.Client + helm HelmClient version string } -func (c *chartRollbackHandler) Handle(_ context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionChartRollback) +func (c *chartRollbackHandler) Handle(_ context.Context, action *types.ClusterAction) error { + req, ok := action.Data().(*types.ActionChartRollback) if !ok { return fmt.Errorf("unexpected type %T for chart rollback handler", action.Data()) } @@ -46,7 +46,7 @@ func (c *chartRollbackHandler) Handle(_ context.Context, action *castai.ClusterA }) } -func (c *chartRollbackHandler) validateRequest(req *castai.ActionChartRollback) error { +func (c *chartRollbackHandler) validateRequest(req *types.ActionChartRollback) error { if req.ReleaseName == "" { return errors.New("bad request: releaseName not provided") } diff --git a/actions/chart_rollback_handler_test.go b/actions/chart_rollback_handler_test.go index 8989ce70..32a31300 100644 --- a/actions/chart_rollback_handler_test.go +++ b/actions/chart_rollback_handler_test.go @@ -3,28 +3,29 @@ package actions import ( "context" "fmt" - "github.com/google/uuid" "testing" + "github.com/google/uuid" + "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/actions/mock" + "github.com/castai/cluster-controller/actions/types" "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) + helmMock := mock.NewMockHelmClient(ctrl) ctx := context.Background() handler := newChartRollbackHandler(logrus.New(), helmMock, "v0.20.0") t.Run("successfully rollback chart", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartRollback: newRollbackAction(), } @@ -38,7 +39,7 @@ func TestChartRollbackHandler(t *testing.T) { }) t.Run("skip rollback if version mismatch", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartRollback: newRollbackAction(), } @@ -47,7 +48,7 @@ func TestChartRollbackHandler(t *testing.T) { }) t.Run("error when rolling back chart", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartRollback: newRollbackAction(), } @@ -61,7 +62,7 @@ func TestChartRollbackHandler(t *testing.T) { }) t.Run("namespace is missing in rollback action", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartRollback: newRollbackAction(), } @@ -71,7 +72,7 @@ func TestChartRollbackHandler(t *testing.T) { }) t.Run("helm release is missing in rollback action", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartRollback: newRollbackAction(), } @@ -81,8 +82,8 @@ func TestChartRollbackHandler(t *testing.T) { }) } -func newRollbackAction() *castai.ActionChartRollback { - return &castai.ActionChartRollback{ +func newRollbackAction() *types.ActionChartRollback { + return &types.ActionChartRollback{ Namespace: "test", ReleaseName: "new-release", Version: "v0.20.0", diff --git a/actions/chart_uninstall_handler.go b/actions/chart_uninstall_handler.go index 3692da05..4c72cb8e 100644 --- a/actions/chart_uninstall_handler.go +++ b/actions/chart_uninstall_handler.go @@ -7,11 +7,11 @@ import ( "github.com/sirupsen/logrus" - "github.com/castai/cluster-controller/castai" + "github.com/castai/cluster-controller/actions/types" "github.com/castai/cluster-controller/helm" ) -func newChartUninstallHandler(log logrus.FieldLogger, helm helm.Client) ActionHandler { +func newChartUninstallHandler(log logrus.FieldLogger, helm HelmClient) ActionHandler { return &chartUninstallHandler{ log: log, helm: helm, @@ -20,11 +20,11 @@ func newChartUninstallHandler(log logrus.FieldLogger, helm helm.Client) ActionHa type chartUninstallHandler struct { log logrus.FieldLogger - helm helm.Client + helm HelmClient } -func (c *chartUninstallHandler) Handle(_ context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionChartUninstall) +func (c *chartUninstallHandler) Handle(_ context.Context, action *types.ClusterAction) error { + req, ok := action.Data().(*types.ActionChartUninstall) if !ok { return fmt.Errorf("unexpected type %T for upsert uninstall handler", action.Data()) } @@ -39,7 +39,7 @@ func (c *chartUninstallHandler) Handle(_ context.Context, action *castai.Cluster return err } -func (c *chartUninstallHandler) validateRequest(req *castai.ActionChartUninstall) error { +func (c *chartUninstallHandler) validateRequest(req *types.ActionChartUninstall) error { if req.ReleaseName == "" { return errors.New("bad request: releaseName not provided") } diff --git a/actions/chart_uninstall_handler_test.go b/actions/chart_uninstall_handler_test.go index 7985dfae..3db31539 100644 --- a/actions/chart_uninstall_handler_test.go +++ b/actions/chart_uninstall_handler_test.go @@ -3,28 +3,28 @@ package actions import ( "context" "fmt" - "github.com/google/uuid" "testing" + "github.com/google/uuid" + "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/actions/types" "github.com/castai/cluster-controller/helm" - mock_helm "github.com/castai/cluster-controller/helm/mock" ) func TestChartUninstallHandler(t *testing.T) { r := require.New(t) ctrl := gomock.NewController(t) - helmMock := mock_helm.NewMockClient(ctrl) + helmMock := mock.NewMockHelmClient(ctrl) ctx := context.Background() handler := newChartUninstallHandler(logrus.New(), helmMock) t.Run("successfully uninstall chart", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartUninstall: newUninstallAction(), } @@ -38,7 +38,7 @@ func TestChartUninstallHandler(t *testing.T) { }) t.Run("error when uninstalling chart", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartUninstall: newUninstallAction(), } @@ -53,7 +53,7 @@ func TestChartUninstallHandler(t *testing.T) { }) t.Run("namespace is missing in uninstall action", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartUninstall: newUninstallAction(), } @@ -63,7 +63,7 @@ func TestChartUninstallHandler(t *testing.T) { }) t.Run("helm release is missing in uninstall action", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartUninstall: newUninstallAction(), } @@ -73,8 +73,8 @@ func TestChartUninstallHandler(t *testing.T) { }) } -func newUninstallAction() *castai.ActionChartUninstall { - return &castai.ActionChartUninstall{ +func newUninstallAction() *types.ActionChartUninstall { + return &types.ActionChartUninstall{ Namespace: "test", ReleaseName: "new-release", } diff --git a/actions/chart_upsert_handler.go b/actions/chart_upsert_handler.go index dedbb8bb..316a86a7 100644 --- a/actions/chart_upsert_handler.go +++ b/actions/chart_upsert_handler.go @@ -9,11 +9,11 @@ import ( "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/actions/types" "github.com/castai/cluster-controller/helm" ) -func newChartUpsertHandler(log logrus.FieldLogger, helm helm.Client) ActionHandler { +func newChartUpsertHandler(log logrus.FieldLogger, helm HelmClient) ActionHandler { return &chartUpsertHandler{ log: log, helm: helm, @@ -22,11 +22,11 @@ func newChartUpsertHandler(log logrus.FieldLogger, helm helm.Client) ActionHandl type chartUpsertHandler struct { log logrus.FieldLogger - helm helm.Client + helm HelmClient } -func (c *chartUpsertHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionChartUpsert) +func (c *chartUpsertHandler) Handle(ctx context.Context, action *types.ClusterAction) error { + req, ok := action.Data().(*types.ActionChartUpsert) if !ok { return fmt.Errorf("unexpected type %T for upsert chart handler", action.Data()) } @@ -74,7 +74,7 @@ func (c *chartUpsertHandler) Handle(ctx context.Context, action *castai.ClusterA return err } -func (c *chartUpsertHandler) validateRequest(req *castai.ActionChartUpsert) error { +func (c *chartUpsertHandler) validateRequest(req *types.ActionChartUpsert) error { if req.ReleaseName == "" { return errors.New("bad request: releaseName not provided") } diff --git a/actions/chart_upsert_handler_test.go b/actions/chart_upsert_handler_test.go index a8f1ce76..4f77158d 100644 --- a/actions/chart_upsert_handler_test.go +++ b/actions/chart_upsert_handler_test.go @@ -12,21 +12,20 @@ import ( "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/actions/types" "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) + helmMock := mock.NewMockHelmClient(ctrl) ctx := context.Background() handler := newChartUpsertHandler(logrus.New(), helmMock) t.Run("install chart given release is not found", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartUpsert: chartUpsertAction(), } @@ -47,7 +46,7 @@ func TestChartUpsertHandler(t *testing.T) { }) t.Run("upgrade chart given release is found", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartUpsert: chartUpsertAction(), } @@ -77,7 +76,7 @@ func TestChartUpsertHandler(t *testing.T) { }) t.Run("rollback previous release before upgrade", func(t *testing.T) { - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), ActionChartUpsert: chartUpsertAction(), } @@ -104,8 +103,8 @@ func TestChartUpsertHandler(t *testing.T) { }) } -func chartUpsertAction() *castai.ActionChartUpsert { - return &castai.ActionChartUpsert{ +func chartUpsertAction() *types.ActionChartUpsert { + return &types.ActionChartUpsert{ Namespace: "test", ReleaseName: "new-release", ValuesOverrides: map[string]string{"image.tag": "1.0.0"}, diff --git a/actions/check_node_deleted.go b/actions/check_node_deleted.go index 7eac072f..be8d9a22 100644 --- a/actions/check_node_deleted.go +++ b/actions/check_node_deleted.go @@ -7,13 +7,12 @@ import ( "reflect" "time" + "github.com/castai/cluster-controller/actions/types" "github.com/cenkalti/backoff/v4" "github.com/sirupsen/logrus" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" - - "github.com/castai/cluster-controller/castai" ) type checkNodeDeletedConfig struct { @@ -38,8 +37,8 @@ type checkNodeDeletedHandler struct { cfg checkNodeDeletedConfig } -func (h *checkNodeDeletedHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionCheckNodeDeleted) +func (h *checkNodeDeletedHandler) Handle(ctx context.Context, action *types.ClusterAction) error { + req, ok := action.Data().(*types.ActionCheckNodeDeleted) if !ok { return fmt.Errorf("unexpected type %T for check node deleted handler", action.Data()) } @@ -47,7 +46,7 @@ func (h *checkNodeDeletedHandler) Handle(ctx context.Context, action *castai.Clu log := h.log.WithFields(logrus.Fields{ "node_name": req.NodeName, "node_id": req.NodeID, - "type": reflect.TypeOf(action.Data().(*castai.ActionCheckNodeDeleted)).String(), + "type": reflect.TypeOf(action.Data().(*types.ActionCheckNodeDeleted)).String(), actionIDLogField: action.ID, }) log.Info("checking if node is deleted") @@ -63,7 +62,7 @@ func (h *checkNodeDeletedHandler) Handle(ctx context.Context, action *castai.Clu return nil } - currentNodeID, ok := n.Labels[castai.LabelNodeID] + currentNodeID, ok := n.Labels[labelNodeID] if !ok { log.Info("node doesn't have castai node id label") } diff --git a/actions/check_node_handler_test.go b/actions/check_node_handler_test.go index 2cd06505..230e9cc5 100644 --- a/actions/check_node_handler_test.go +++ b/actions/check_node_handler_test.go @@ -2,16 +2,16 @@ package actions import ( "context" - "github.com/google/uuid" "testing" + "github.com/castai/cluster-controller/actions/types" + "github.com/google/uuid" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" - - "github.com/castai/cluster-controller/castai" ) func TestCheckNodeDeletedHandler(t *testing.T) { @@ -35,9 +35,9 @@ func TestCheckNodeDeletedHandler(t *testing.T) { cfg: checkNodeDeletedConfig{}, } - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionCheckNodeDeleted: &castai.ActionCheckNodeDeleted{NodeName: "node1"}, + ActionCheckNodeDeleted: &types.ActionCheckNodeDeleted{NodeName: "node1"}, } err := h.Handle(context.Background(), action) @@ -53,9 +53,9 @@ func TestCheckNodeDeletedHandler(t *testing.T) { cfg: checkNodeDeletedConfig{}, } - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionCheckNodeDeleted: &castai.ActionCheckNodeDeleted{NodeName: "node1"}, + ActionCheckNodeDeleted: &types.ActionCheckNodeDeleted{NodeName: "node1"}, } err := h.Handle(context.Background(), action) diff --git a/actions/check_node_status.go b/actions/check_node_status.go index 790062a7..1df49646 100644 --- a/actions/check_node_status.go +++ b/actions/check_node_status.go @@ -7,6 +7,7 @@ import ( "reflect" "time" + "github.com/castai/cluster-controller/actions/types" "github.com/cenkalti/backoff/v4" "github.com/sirupsen/logrus" corev1 "k8s.io/api/core/v1" @@ -14,8 +15,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" - - "github.com/castai/cluster-controller/castai" ) func newCheckNodeStatusHandler(log logrus.FieldLogger, clientset kubernetes.Interface) ActionHandler { @@ -30,8 +29,8 @@ type checkNodeStatusHandler struct { clientset kubernetes.Interface } -func (h *checkNodeStatusHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionCheckNodeStatus) +func (h *checkNodeStatusHandler) Handle(ctx context.Context, action *types.ClusterAction) error { + req, ok := action.Data().(*types.ActionCheckNodeStatus) if !ok { return fmt.Errorf("unexpected type %T for check node status handler", action.Data()) } @@ -40,15 +39,15 @@ func (h *checkNodeStatusHandler) Handle(ctx context.Context, action *castai.Clus "node_name": req.NodeName, "node_id": req.NodeID, "node_status": req.NodeStatus, - "type": reflect.TypeOf(action.Data().(*castai.ActionCheckNodeStatus)).String(), + "type": reflect.TypeOf(action.Data().(*types.ActionCheckNodeStatus)).String(), actionIDLogField: action.ID, }) switch req.NodeStatus { - case castai.ActionCheckNodeStatus_READY: + case actionCheckNodeStatus_READY: log.Info("checking node ready") return h.checkNodeReady(ctx, log, req) - case castai.ActionCheckNodeStatus_DELETED: + case actionCheckNodeStatus_DELETED: log.Info("checking node deleted") return h.checkNodeDeleted(ctx, log, req) @@ -57,7 +56,7 @@ func (h *checkNodeStatusHandler) Handle(ctx context.Context, action *castai.Clus return fmt.Errorf("unknown status to check provided node=%s status=%s", req.NodeName, req.NodeStatus) } -func (h *checkNodeStatusHandler) checkNodeDeleted(ctx context.Context, log *logrus.Entry, req *castai.ActionCheckNodeStatus) error { +func (h *checkNodeStatusHandler) checkNodeDeleted(ctx context.Context, log *logrus.Entry, req *types.ActionCheckNodeStatus) error { timeout := 10 if req.WaitTimeoutSeconds != nil { timeout = int(*req.WaitTimeoutSeconds) @@ -80,7 +79,7 @@ func (h *checkNodeStatusHandler) checkNodeDeleted(ctx context.Context, log *logr return nil } - currentNodeID, ok := n.Labels[castai.LabelNodeID] + currentNodeID, ok := n.Labels[labelNodeID] if !ok { log.Info("node doesn't have castai node id label") } @@ -102,7 +101,7 @@ func (h *checkNodeStatusHandler) checkNodeDeleted(ctx context.Context, log *logr }, b) } -func (h *checkNodeStatusHandler) checkNodeReady(ctx context.Context, log *logrus.Entry, req *castai.ActionCheckNodeStatus) error { +func (h *checkNodeStatusHandler) checkNodeReady(ctx context.Context, log *logrus.Entry, req *types.ActionCheckNodeStatus) error { timeout := 9 * time.Minute watchObject := metav1.SingleObject(metav1.ObjectMeta{Name: req.NodeName}) if req.WaitTimeoutSeconds != nil { @@ -131,7 +130,7 @@ func (h *checkNodeStatusHandler) checkNodeReady(ctx context.Context, log *logrus func isNodeReady(node *corev1.Node, castNodeID string) bool { // if node has castai node id label, check if it matches the one we are waiting for // if it doesn't match, we can skip this node - if val, ok := node.Labels[castai.LabelNodeID]; ok { + if val, ok := node.Labels[labelNodeID]; ok { if val != "" && val != castNodeID { return false } diff --git a/actions/check_node_status_test.go b/actions/check_node_status_test.go index dc96b187..97bf7e9b 100644 --- a/actions/check_node_status_test.go +++ b/actions/check_node_status_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/castai/cluster-controller/actions/types" "github.com/google/uuid" "github.com/sirupsen/logrus" @@ -15,8 +16,6 @@ import ( "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/kubernetes/fake" k8stest "k8s.io/client-go/testing" - - "github.com/castai/cluster-controller/castai" ) func TestCheckStatus_Deleted(t *testing.T) { @@ -30,7 +29,7 @@ func TestCheckStatus_Deleted(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ Name: nodeName, Labels: map[string]string{ - castai.LabelNodeID: "old-node-id", + labelNodeID: "old-node-id", }, }, } @@ -41,11 +40,11 @@ func TestCheckStatus_Deleted(t *testing.T) { clientset: clientset, } - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionCheckNodeStatus: &castai.ActionCheckNodeStatus{ + ActionCheckNodeStatus: &types.ActionCheckNodeStatus{ NodeName: "node1", - NodeStatus: castai.ActionCheckNodeStatus_DELETED, + NodeStatus: actionCheckNodeStatus_DELETED, NodeID: "old-node-id", }, } @@ -69,11 +68,11 @@ func TestCheckStatus_Deleted(t *testing.T) { clientset: clientset, } - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionCheckNodeStatus: &castai.ActionCheckNodeStatus{ + ActionCheckNodeStatus: &types.ActionCheckNodeStatus{ NodeName: "node1", - NodeStatus: castai.ActionCheckNodeStatus_DELETED, + NodeStatus: actionCheckNodeStatus_DELETED, NodeID: "old-node-id", }, } @@ -91,11 +90,11 @@ func TestCheckStatus_Deleted(t *testing.T) { clientset: clientset, } - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionCheckNodeStatus: &castai.ActionCheckNodeStatus{ + ActionCheckNodeStatus: &types.ActionCheckNodeStatus{ NodeName: "node1", - NodeStatus: castai.ActionCheckNodeStatus_DELETED, + NodeStatus: actionCheckNodeStatus_DELETED, NodeID: "old-node-id", }, } @@ -110,7 +109,7 @@ func TestCheckStatus_Deleted(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ Name: "node1", Labels: map[string]string{ - castai.LabelNodeID: "old-node-id", + labelNodeID: "old-node-id", }, }, } @@ -121,11 +120,11 @@ func TestCheckStatus_Deleted(t *testing.T) { clientset: clientset, } - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionCheckNodeStatus: &castai.ActionCheckNodeStatus{ + ActionCheckNodeStatus: &types.ActionCheckNodeStatus{ NodeName: "node1", - NodeStatus: castai.ActionCheckNodeStatus_DELETED, + NodeStatus: actionCheckNodeStatus_DELETED, NodeID: "im-a-different-node", }, } @@ -157,11 +156,11 @@ func TestCheckStatus_Ready(t *testing.T) { }() timeout := int32(1) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionCheckNodeStatus: &castai.ActionCheckNodeStatus{ + ActionCheckNodeStatus: &types.ActionCheckNodeStatus{ NodeName: "node1", - NodeStatus: castai.ActionCheckNodeStatus_READY, + NodeStatus: actionCheckNodeStatus_READY, WaitTimeoutSeconds: &timeout, }, } @@ -194,11 +193,11 @@ func TestCheckStatus_Ready(t *testing.T) { } timeout := int32(60) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionCheckNodeStatus: &castai.ActionCheckNodeStatus{ + ActionCheckNodeStatus: &types.ActionCheckNodeStatus{ NodeName: "node1", - NodeStatus: castai.ActionCheckNodeStatus_READY, + NodeStatus: actionCheckNodeStatus_READY, WaitTimeoutSeconds: &timeout, }, } @@ -249,11 +248,11 @@ func TestCheckStatus_Ready(t *testing.T) { } timeout := int32(60) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionCheckNodeStatus: &castai.ActionCheckNodeStatus{ + ActionCheckNodeStatus: &types.ActionCheckNodeStatus{ NodeName: "node1", - NodeStatus: castai.ActionCheckNodeStatus_READY, + NodeStatus: actionCheckNodeStatus_READY, WaitTimeoutSeconds: &timeout, }, } @@ -302,11 +301,11 @@ func TestCheckStatus_Ready(t *testing.T) { clientset: clientset, } - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionCheckNodeStatus: &castai.ActionCheckNodeStatus{ + ActionCheckNodeStatus: &types.ActionCheckNodeStatus{ NodeName: "node1", - NodeStatus: castai.ActionCheckNodeStatus_READY, + NodeStatus: actionCheckNodeStatus_READY, }, } @@ -322,7 +321,7 @@ func TestCheckStatus_Ready(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ Name: nodeName, Labels: map[string]string{ - castai.LabelNodeID: "old-node-id", + labelNodeID: "old-node-id", }, }, Status: v1.NodeStatus{ @@ -342,11 +341,11 @@ func TestCheckStatus_Ready(t *testing.T) { } timeout := int32(60) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionCheckNodeStatus: &castai.ActionCheckNodeStatus{ + ActionCheckNodeStatus: &types.ActionCheckNodeStatus{ NodeName: "node1", - NodeStatus: castai.ActionCheckNodeStatus_READY, + NodeStatus: actionCheckNodeStatus_READY, WaitTimeoutSeconds: &timeout, NodeID: "new-node-id", }, @@ -371,7 +370,7 @@ func TestCheckStatus_Ready(t *testing.T) { time.Sleep(1 * time.Second) newNode := node.DeepCopy() - newNode.Labels[castai.LabelNodeID] = "new-node-id" + newNode.Labels[labelNodeID] = "new-node-id" _, _ = clientset.CoreV1().Nodes().Create(context.Background(), newNode, metav1.CreateOptions{}) diff --git a/actions/create_event_handler.go b/actions/create_event_handler.go index cc5643dd..0ae5b08e 100644 --- a/actions/create_event_handler.go +++ b/actions/create_event_handler.go @@ -14,7 +14,7 @@ import ( "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/client-go/kubernetes" - "github.com/castai/cluster-controller/castai" + actiontypes "github.com/castai/cluster-controller/actions/types" ) func newCreateEventHandler(log logrus.FieldLogger, clientset kubernetes.Interface) ActionHandler { @@ -29,8 +29,8 @@ type createEventHandler struct { clientset kubernetes.Interface } -func (h *createEventHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionCreateEvent) +func (h *createEventHandler) Handle(ctx context.Context, action *actiontypes.ClusterAction) error { + req, ok := action.Data().(*actiontypes.ActionCreateEvent) if !ok { return fmt.Errorf("unexpected type %T for create event handler", action.Data()) } diff --git a/actions/create_event_handler_test.go b/actions/create_event_handler_test.go index 50455acd..8edb74be 100644 --- a/actions/create_event_handler_test.go +++ b/actions/create_event_handler_test.go @@ -15,7 +15,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes/fake" - "github.com/castai/cluster-controller/castai" + actiontypes "github.com/castai/cluster-controller/actions/types" ) func TestCreateEvent(t *testing.T) { @@ -24,16 +24,16 @@ func TestCreateEvent(t *testing.T) { tests := []struct { name string - action *castai.ClusterAction + action *actiontypes.ClusterAction actionCount int object runtime.Object expectedEvent *corev1.Event }{ { name: "create single pod event", - action: &castai.ClusterAction{ + action: &actiontypes.ClusterAction{ ID: uuid.New().String(), - ActionCreateEvent: &castai.ActionCreateEvent{ + ActionCreateEvent: &actiontypes.ActionCreateEvent{ Reporter: "autoscaler.cast.ai", ObjectRef: podObjReference(testPod(id)), EventTime: time.Now(), @@ -57,9 +57,9 @@ func TestCreateEvent(t *testing.T) { }, { name: "create several pod events", - action: &castai.ClusterAction{ + action: &actiontypes.ClusterAction{ ID: "", - ActionCreateEvent: &castai.ActionCreateEvent{ + ActionCreateEvent: &actiontypes.ActionCreateEvent{ Reporter: "provisioning.cast.ai", ObjectRef: podObjReference(testPod(id)), EventTime: time.Now(), diff --git a/actions/create_handler.go b/actions/create_handler.go index 4f757911..129ab6d7 100644 --- a/actions/create_handler.go +++ b/actions/create_handler.go @@ -6,6 +6,7 @@ import ( "fmt" "reflect" + actiontypes "github.com/castai/cluster-controller/actions/types" jsonpatch "github.com/evanphx/json-patch" "github.com/sirupsen/logrus" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -14,8 +15,6 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/dynamic" - - "github.com/castai/cluster-controller/castai" ) type createHandler struct { @@ -30,8 +29,8 @@ func newCreateHandler(log logrus.FieldLogger, client dynamic.Interface) ActionHa } } -func (h *createHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionCreate) +func (h *createHandler) Handle(ctx context.Context, action *actiontypes.ClusterAction) error { + req, ok := action.Data().(*actiontypes.ActionCreate) if !ok { return newUnexpectedTypeErr(action.Data(), req) } diff --git a/actions/create_handler_test.go b/actions/create_handler_test.go index d930bb4b..945723da 100644 --- a/actions/create_handler_test.go +++ b/actions/create_handler_test.go @@ -5,6 +5,7 @@ import ( "errors" "testing" + "github.com/castai/cluster-controller/actions/types" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" appsv1 "k8s.io/api/apps/v1" @@ -14,8 +15,6 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic/fake" "sigs.k8s.io/controller-runtime/pkg/client" - - "github.com/castai/cluster-controller/castai" ) func Test_newCreateHandler(t *testing.T) { @@ -25,29 +24,29 @@ func Test_newCreateHandler(t *testing.T) { tests := map[string]struct { objs []runtime.Object - action *castai.ClusterAction + action *types.ClusterAction convertFn func(i map[string]interface{}) client.Object err error want *appsv1.Deployment }{ "should return error when action is of a different type": { - action: &castai.ClusterAction{ - ActionDeleteNode: &castai.ActionDeleteNode{}, + action: &types.ClusterAction{ + ActionDeleteNode: &types.ActionDeleteNode{}, }, - err: newUnexpectedTypeErr(&castai.ActionDeleteNode{}, &castai.ActionCreate{}), + err: newUnexpectedTypeErr(&types.ActionDeleteNode{}, &types.ActionCreate{}), }, "should return error when object is not provided": { - action: &castai.ClusterAction{ - ActionCreate: &castai.ActionCreate{ - GroupVersionResource: castai.GroupVersionResource{}, + action: &types.ClusterAction{ + ActionCreate: &types.ActionCreate{ + GroupVersionResource: types.GroupVersionResource{}, }, }, err: errors.New("no object provided"), }, "should create new deployment": { - action: &castai.ClusterAction{ - ActionCreate: &castai.ActionCreate{ - GroupVersionResource: castai.GroupVersionResource{ + action: &types.ClusterAction{ + ActionCreate: &types.ActionCreate{ + GroupVersionResource: types.GroupVersionResource{ Group: appsv1.SchemeGroupVersion.Group, Version: appsv1.SchemeGroupVersion.Version, Resource: "deployments", @@ -63,9 +62,9 @@ func Test_newCreateHandler(t *testing.T) { }, }, "should patch already existing resource": { - action: &castai.ClusterAction{ - ActionCreate: &castai.ActionCreate{ - GroupVersionResource: castai.GroupVersionResource{ + action: &types.ClusterAction{ + ActionCreate: &types.ActionCreate{ + GroupVersionResource: types.GroupVersionResource{ Group: appsv1.SchemeGroupVersion.Group, Version: appsv1.SchemeGroupVersion.Version, Resource: "deployments", diff --git a/actions/delete_handler.go b/actions/delete_handler.go index 8394a34d..d7ac1c02 100644 --- a/actions/delete_handler.go +++ b/actions/delete_handler.go @@ -5,13 +5,12 @@ import ( "fmt" "reflect" + "github.com/castai/cluster-controller/actions/types" "github.com/sirupsen/logrus" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic" - - "github.com/castai/cluster-controller/castai" ) type deleteHandler struct { @@ -26,8 +25,8 @@ func newDeleteHandler(log logrus.FieldLogger, client dynamic.Interface) ActionHa } } -func (h *deleteHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionDelete) +func (h *deleteHandler) Handle(ctx context.Context, action *types.ClusterAction) error { + req, ok := action.Data().(*types.ActionDelete) if !ok { return newUnexpectedTypeErr(action.Data(), req) } diff --git a/actions/delete_handler_test.go b/actions/delete_handler_test.go index 0c019970..c7af35eb 100644 --- a/actions/delete_handler_test.go +++ b/actions/delete_handler_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/castai/cluster-controller/actions/types" "github.com/samber/lo" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" @@ -13,8 +14,6 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic/fake" - - "github.com/castai/cluster-controller/castai" ) func Test_newDeleteHandler(t *testing.T) { @@ -25,21 +24,21 @@ func Test_newDeleteHandler(t *testing.T) { tests := map[string]struct { objs []runtime.Object - action *castai.ClusterAction + action *types.ClusterAction want int err error }{ "should return error when action is of a different type": { - action: &castai.ClusterAction{ - ActionDeleteNode: &castai.ActionDeleteNode{}, + action: &types.ClusterAction{ + ActionDeleteNode: &types.ActionDeleteNode{}, }, - err: newUnexpectedTypeErr(&castai.ActionDeleteNode{}, &castai.ActionDelete{}), + err: newUnexpectedTypeErr(&types.ActionDeleteNode{}, &types.ActionDelete{}), }, "should skip if resource not found": { - action: &castai.ClusterAction{ - ActionDelete: &castai.ActionDelete{ - ID: castai.ObjectID{ - GroupVersionResource: castai.GroupVersionResource{ + action: &types.ClusterAction{ + ActionDelete: &types.ActionDelete{ + ID: types.ObjectID{ + GroupVersionResource: types.GroupVersionResource{ Group: appsv1.SchemeGroupVersion.Group, Version: appsv1.SchemeGroupVersion.Version, Resource: "deployments", @@ -55,10 +54,10 @@ func Test_newDeleteHandler(t *testing.T) { want: 1, }, "should delete deployment": { - action: &castai.ClusterAction{ - ActionDelete: &castai.ActionDelete{ - ID: castai.ObjectID{ - GroupVersionResource: castai.GroupVersionResource{ + action: &types.ClusterAction{ + ActionDelete: &types.ActionDelete{ + ID: types.ObjectID{ + GroupVersionResource: types.GroupVersionResource{ Group: appsv1.SchemeGroupVersion.Group, Version: appsv1.SchemeGroupVersion.Version, Resource: "deployments", @@ -76,10 +75,10 @@ func Test_newDeleteHandler(t *testing.T) { want: 2, }, "should delete resource without namespace": { - action: &castai.ClusterAction{ - ActionDelete: &castai.ActionDelete{ - ID: castai.ObjectID{ - GroupVersionResource: castai.GroupVersionResource{ + action: &types.ClusterAction{ + ActionDelete: &types.ActionDelete{ + ID: types.ObjectID{ + GroupVersionResource: types.GroupVersionResource{ Group: corev1.SchemeGroupVersion.Group, Version: corev1.SchemeGroupVersion.Version, Resource: "nodes", diff --git a/actions/delete_node_handler.go b/actions/delete_node_handler.go index 38e25840..8f572ec0 100644 --- a/actions/delete_node_handler.go +++ b/actions/delete_node_handler.go @@ -7,6 +7,7 @@ import ( "reflect" "time" + "github.com/castai/cluster-controller/actions/types" "github.com/cenkalti/backoff/v4" "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" @@ -14,8 +15,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" "k8s.io/client-go/kubernetes" - - "github.com/castai/cluster-controller/castai" ) type deleteNodeConfig struct { @@ -49,8 +48,8 @@ type deleteNodeHandler struct { cfg deleteNodeConfig } -func (h *deleteNodeHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionDeleteNode) +func (h *deleteNodeHandler) Handle(ctx context.Context, action *types.ClusterAction) error { + req, ok := action.Data().(*types.ActionDeleteNode) if !ok { return fmt.Errorf("unexpected type %T for delete node handler", action.Data()) } @@ -58,7 +57,7 @@ func (h *deleteNodeHandler) Handle(ctx context.Context, action *castai.ClusterAc log := h.log.WithFields(logrus.Fields{ "node_name": req.NodeName, "node_id": req.NodeID, - "type": reflect.TypeOf(action.Data().(*castai.ActionDeleteNode)).String(), + "type": reflect.TypeOf(action.Data().(*types.ActionDeleteNode)).String(), actionIDLogField: action.ID, }) log.Info("deleting kubernetes node") @@ -74,7 +73,7 @@ func (h *deleteNodeHandler) Handle(ctx context.Context, action *castai.ClusterAc return fmt.Errorf("error getting node: %w", err) } - if val, ok := current.Labels[castai.LabelNodeID]; ok { + if val, ok := current.Labels[labelNodeID]; ok { if val != "" && val != req.NodeID { log.Infof("node id mismatch, expected %q got %q. Skipping delete.", req.NodeID, val) return errNodeMismatch diff --git a/actions/delete_node_handler_test.go b/actions/delete_node_handler_test.go index 8dfce49a..c5cc4def 100644 --- a/actions/delete_node_handler_test.go +++ b/actions/delete_node_handler_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/castai/cluster-controller/actions/types" "github.com/google/uuid" "k8s.io/apimachinery/pkg/fields" @@ -13,8 +14,6 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" - - "github.com/castai/cluster-controller/castai" ) func TestDeleteNodeHandler(t *testing.T) { @@ -31,9 +30,9 @@ func TestDeleteNodeHandler(t *testing.T) { } clientset := fake.NewSimpleClientset(node) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDeleteNode: &castai.ActionDeleteNode{ + ActionDeleteNode: &types.ActionDeleteNode{ NodeName: "node1", }, } @@ -62,9 +61,9 @@ func TestDeleteNodeHandler(t *testing.T) { } clientset := fake.NewSimpleClientset(node) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDeleteNode: &castai.ActionDeleteNode{ + ActionDeleteNode: &types.ActionDeleteNode{ NodeName: "already-deleted-node", }, } @@ -89,15 +88,15 @@ func TestDeleteNodeHandler(t *testing.T) { ObjectMeta: metav1.ObjectMeta{ Name: nodeName, Labels: map[string]string{ - castai.LabelNodeID: "node-id", + labelNodeID: "node-id", }, }, } clientset := fake.NewSimpleClientset(node) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDeleteNode: &castai.ActionDeleteNode{ + ActionDeleteNode: &types.ActionDeleteNode{ NodeName: "node1", NodeID: "another-node-id", }, @@ -114,7 +113,7 @@ func TestDeleteNodeHandler(t *testing.T) { existing, err := clientset.CoreV1().Nodes().Get(context.Background(), nodeName, metav1.GetOptions{}) r.NoError(err) - existing.Labels[castai.LabelNodeID] = "node-id" + existing.Labels[labelNodeID] = "node-id" }) t.Run("delete node with pods", func(t *testing.T) { @@ -123,9 +122,9 @@ func TestDeleteNodeHandler(t *testing.T) { podName := "pod1" clientset := setupFakeClientWithNodePodEviction(nodeName, podName) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDeleteNode: &castai.ActionDeleteNode{ + ActionDeleteNode: &types.ActionDeleteNode{ NodeName: nodeName, }, } diff --git a/actions/disconnect_cluster_handler.go b/actions/disconnect_cluster_handler.go index 1acbad70..f3b8cf33 100644 --- a/actions/disconnect_cluster_handler.go +++ b/actions/disconnect_cluster_handler.go @@ -3,9 +3,9 @@ package actions import ( "context" "fmt" - "github.com/castai/cluster-controller/castai" "reflect" + "github.com/castai/cluster-controller/actions/types" "github.com/sirupsen/logrus" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -24,7 +24,7 @@ type disconnectClusterHandler struct { client kubernetes.Interface } -func (c *disconnectClusterHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { +func (c *disconnectClusterHandler) Handle(ctx context.Context, action *types.ClusterAction) error { ns := "castai-agent" _, err := c.client.CoreV1().Namespaces().Get(ctx, ns, metav1.GetOptions{}) if err != nil { @@ -40,7 +40,7 @@ func (c *disconnectClusterHandler) Handle(ctx context.Context, action *castai.Cl return err } log := c.log.WithFields(logrus.Fields{ - "type": reflect.TypeOf(action.Data().(*castai.ActionDisconnectCluster)).String(), + "type": reflect.TypeOf(action.Data().(*types.ActionDisconnectCluster)).String(), actionIDLogField: action.ID, }) diff --git a/actions/disconnect_cluster_handler_test.go b/actions/disconnect_cluster_handler_test.go index 5bf06e1d..561493f1 100644 --- a/actions/disconnect_cluster_handler_test.go +++ b/actions/disconnect_cluster_handler_test.go @@ -2,17 +2,17 @@ package actions import ( "context" - "github.com/google/uuid" "testing" + "github.com/castai/cluster-controller/actions/types" + "github.com/google/uuid" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" - - "github.com/castai/cluster-controller/castai" ) func TestDisconnectClusterHandler(t *testing.T) { @@ -27,9 +27,9 @@ func TestDisconnectClusterHandler(t *testing.T) { } clientset := fake.NewSimpleClientset(node) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDisconnectCluster: &castai.ActionDisconnectCluster{}, + ActionDisconnectCluster: &types.ActionDisconnectCluster{}, } handler := newDisconnectClusterHandler(logrus.New(), clientset) diff --git a/actions/drain_node_handler.go b/actions/drain_node_handler.go index d8cb4a34..8e06229c 100644 --- a/actions/drain_node_handler.go +++ b/actions/drain_node_handler.go @@ -8,6 +8,7 @@ import ( "strings" "time" + "github.com/castai/cluster-controller/actions/types" "github.com/cenkalti/backoff/v4" "github.com/samber/lo" "github.com/sirupsen/logrus" @@ -21,8 +22,6 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes" "k8s.io/kubectl/pkg/drain" - - "github.com/castai/cluster-controller/castai" ) const ( @@ -58,7 +57,7 @@ func newDrainNodeHandler(log logrus.FieldLogger, clientset kubernetes.Interface, // getDrainTimeout returns drain timeout adjusted to action creation time. // the result is clamped between 0s and the requested timeout. -func (h *drainNodeHandler) getDrainTimeout(action *castai.ClusterAction) time.Duration { +func (h *drainNodeHandler) getDrainTimeout(action *types.ClusterAction) time.Duration { timeSinceCreated := time.Since(action.CreatedAt) drainTimeout := time.Duration(action.ActionDrainNode.DrainTimeoutSeconds) * time.Second @@ -74,8 +73,8 @@ type drainNodeHandler struct { cfg drainNodeConfig } -func (h *drainNodeHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionDrainNode) +func (h *drainNodeHandler) Handle(ctx context.Context, action *types.ClusterAction) error { + req, ok := action.Data().(*types.ActionDrainNode) if !ok { return fmt.Errorf("unexpected type %T for drain handler", action.Data()) } @@ -83,7 +82,7 @@ func (h *drainNodeHandler) Handle(ctx context.Context, action *castai.ClusterAct log := h.log.WithFields(logrus.Fields{ "node_name": req.NodeName, "node_id": req.NodeID, - "action": reflect.TypeOf(action.Data().(*castai.ActionDrainNode)).String(), + "action": reflect.TypeOf(action.Data().(*types.ActionDrainNode)).String(), actionIDLogField: action.ID, }) diff --git a/actions/drain_node_handler_test.go b/actions/drain_node_handler_test.go index 12fce87d..842cd189 100644 --- a/actions/drain_node_handler_test.go +++ b/actions/drain_node_handler_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/castai/cluster-controller/actions/types" "github.com/google/uuid" "github.com/sirupsen/logrus" "github.com/sirupsen/logrus/hooks/test" @@ -17,8 +18,6 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" ktest "k8s.io/client-go/testing" - - "github.com/castai/cluster-controller/castai" ) func TestDrainNodeHandler(t *testing.T) { @@ -33,9 +32,9 @@ func TestDrainNodeHandler(t *testing.T) { clientset := setupFakeClientWithNodePodEviction(nodeName, podName) prependEvictionReaction(t, clientset, true) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDrainNode: &castai.ActionDrainNode{ + ActionDrainNode: &types.ActionDrainNode{ NodeName: "node1", DrainTimeoutSeconds: 1, Force: true, @@ -74,9 +73,9 @@ func TestDrainNodeHandler(t *testing.T) { clientset := setupFakeClientWithNodePodEviction(nodeName, podName) prependEvictionReaction(t, clientset, true) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDrainNode: &castai.ActionDrainNode{ + ActionDrainNode: &types.ActionDrainNode{ NodeName: "already-deleted-node", DrainTimeoutSeconds: 1, Force: true, @@ -103,9 +102,9 @@ func TestDrainNodeHandler(t *testing.T) { clientset := setupFakeClientWithNodePodEviction(nodeName, podName) prependEvictionReaction(t, clientset, false) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDrainNode: &castai.ActionDrainNode{ + ActionDrainNode: &types.ActionDrainNode{ NodeName: "node1", DrainTimeoutSeconds: 1, Force: true, @@ -136,9 +135,9 @@ func TestDrainNodeHandler(t *testing.T) { podName := "pod1" clientset := setupFakeClientWithNodePodEviction(nodeName, podName) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDrainNode: &castai.ActionDrainNode{ + ActionDrainNode: &types.ActionDrainNode{ NodeName: "node1", DrainTimeoutSeconds: 1, Force: true, @@ -185,9 +184,9 @@ func TestDrainNodeHandler(t *testing.T) { podName := "pod1" clientset := setupFakeClientWithNodePodEviction(nodeName, podName) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDrainNode: &castai.ActionDrainNode{ + ActionDrainNode: &types.ActionDrainNode{ NodeName: "node1", DrainTimeoutSeconds: 1, Force: true, @@ -227,9 +226,9 @@ func TestDrainNodeHandler(t *testing.T) { podName := "pod1" clientset := setupFakeClientWithNodePodEviction(nodeName, podName) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDrainNode: &castai.ActionDrainNode{ + ActionDrainNode: &types.ActionDrainNode{ NodeName: "node1", DrainTimeoutSeconds: 1, Force: true, @@ -273,9 +272,9 @@ func TestGetDrainTimeout(t *testing.T) { t.Run("drain timeout for new action should be the same like in request", func(t *testing.T) { r := require.New(t) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDrainNode: &castai.ActionDrainNode{ + ActionDrainNode: &types.ActionDrainNode{ NodeName: "node1", DrainTimeoutSeconds: 100, Force: true, @@ -293,9 +292,9 @@ func TestGetDrainTimeout(t *testing.T) { t.Run("drain timeout for older action should be decreased by time since action creation", func(t *testing.T) { r := require.New(t) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDrainNode: &castai.ActionDrainNode{ + ActionDrainNode: &types.ActionDrainNode{ NodeName: "node1", DrainTimeoutSeconds: 600, Force: true, @@ -313,9 +312,9 @@ func TestGetDrainTimeout(t *testing.T) { t.Run("drain timeout min wait timeout should be 0s", func(t *testing.T) { r := require.New(t) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionDrainNode: &castai.ActionDrainNode{ + ActionDrainNode: &types.ActionDrainNode{ NodeName: "node1", DrainTimeoutSeconds: 600, Force: true, diff --git a/actions/mock_actions/client_mock.go b/actions/mock_actions/client_mock.go new file mode 100644 index 00000000..2ae92fac --- /dev/null +++ b/actions/mock_actions/client_mock.go @@ -0,0 +1,79 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/castai/cluster-controller/actions (interfaces: Client) + +// Package mock_actions is a generated GoMock package. +package mock_actions + +import ( + context "context" + reflect "reflect" + + types "github.com/castai/cluster-controller/actions/types" + gomock "github.com/golang/mock/gomock" +) + +// MockClient is a mock of Client interface. +type MockClient struct { + ctrl *gomock.Controller + recorder *MockClientMockRecorder +} + +// MockClientMockRecorder is the mock recorder for MockClient. +type MockClientMockRecorder struct { + mock *MockClient +} + +// NewMockClient creates a new mock instance. +func NewMockClient(ctrl *gomock.Controller) *MockClient { + mock := &MockClient{ctrl: ctrl} + mock.recorder = &MockClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockClient) EXPECT() *MockClientMockRecorder { + return m.recorder +} + +// AckAction mocks base method. +func (m *MockClient) AckAction(arg0 context.Context, arg1 string, arg2 *types.AckClusterActionRequest) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AckAction", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// AckAction indicates an expected call of AckAction. +func (mr *MockClientMockRecorder) AckAction(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AckAction", reflect.TypeOf((*MockClient)(nil).AckAction), arg0, arg1, arg2) +} + +// GetActions mocks base method. +func (m *MockClient) GetActions(arg0 context.Context, arg1 string) ([]*types.ClusterAction, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetActions", arg0, arg1) + ret0, _ := ret[0].([]*types.ClusterAction) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetActions indicates an expected call of GetActions. +func (mr *MockClientMockRecorder) GetActions(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetActions", reflect.TypeOf((*MockClient)(nil).GetActions), arg0, arg1) +} + +// SendAKSInitData mocks base method. +func (m *MockClient) SendAKSInitData(arg0 context.Context, arg1 *types.AKSInitDataRequest) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SendAKSInitData", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// SendAKSInitData indicates an expected call of SendAKSInitData. +func (mr *MockClientMockRecorder) SendAKSInitData(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendAKSInitData", reflect.TypeOf((*MockClient)(nil).SendAKSInitData), arg0, arg1) +} diff --git a/actions/mock_actions/helm_client_mock.go b/actions/mock_actions/helm_client_mock.go new file mode 100644 index 00000000..0019e76d --- /dev/null +++ b/actions/mock_actions/helm_client_mock.go @@ -0,0 +1,111 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/castai/cluster-controller/actions (interfaces: HelmClient) + +// Package mock_actions is a generated GoMock package. +package mock_actions + +import ( + context "context" + reflect "reflect" + + helm "github.com/castai/cluster-controller/helm" + gomock "github.com/golang/mock/gomock" + release "helm.sh/helm/v3/pkg/release" +) + +// MockHelmClient is a mock of HelmClient interface. +type MockHelmClient struct { + ctrl *gomock.Controller + recorder *MockHelmClientMockRecorder +} + +// MockHelmClientMockRecorder is the mock recorder for MockHelmClient. +type MockHelmClientMockRecorder struct { + mock *MockHelmClient +} + +// NewMockHelmClient creates a new mock instance. +func NewMockHelmClient(ctrl *gomock.Controller) *MockHelmClient { + mock := &MockHelmClient{ctrl: ctrl} + mock.recorder = &MockHelmClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockHelmClient) EXPECT() *MockHelmClientMockRecorder { + return m.recorder +} + +// GetRelease mocks base method. +func (m *MockHelmClient) GetRelease(arg0 helm.GetReleaseOptions) (*release.Release, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetRelease", arg0) + ret0, _ := ret[0].(*release.Release) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetRelease indicates an expected call of GetRelease. +func (mr *MockHelmClientMockRecorder) GetRelease(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRelease", reflect.TypeOf((*MockHelmClient)(nil).GetRelease), arg0) +} + +// Install mocks base method. +func (m *MockHelmClient) Install(arg0 context.Context, arg1 helm.InstallOptions) (*release.Release, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Install", arg0, arg1) + ret0, _ := ret[0].(*release.Release) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Install indicates an expected call of Install. +func (mr *MockHelmClientMockRecorder) Install(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Install", reflect.TypeOf((*MockHelmClient)(nil).Install), arg0, arg1) +} + +// Rollback mocks base method. +func (m *MockHelmClient) Rollback(arg0 helm.RollbackOptions) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Rollback", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// Rollback indicates an expected call of Rollback. +func (mr *MockHelmClientMockRecorder) Rollback(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Rollback", reflect.TypeOf((*MockHelmClient)(nil).Rollback), arg0) +} + +// Uninstall mocks base method. +func (m *MockHelmClient) Uninstall(arg0 helm.UninstallOptions) (*release.UninstallReleaseResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Uninstall", arg0) + ret0, _ := ret[0].(*release.UninstallReleaseResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Uninstall indicates an expected call of Uninstall. +func (mr *MockHelmClientMockRecorder) Uninstall(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Uninstall", reflect.TypeOf((*MockHelmClient)(nil).Uninstall), arg0) +} + +// Upgrade mocks base method. +func (m *MockHelmClient) Upgrade(arg0 context.Context, arg1 helm.UpgradeOptions) (*release.Release, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Upgrade", arg0, arg1) + ret0, _ := ret[0].(*release.Release) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Upgrade indicates an expected call of Upgrade. +func (mr *MockHelmClientMockRecorder) Upgrade(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Upgrade", reflect.TypeOf((*MockHelmClient)(nil).Upgrade), arg0, arg1) +} diff --git a/actions/mock_test.go b/actions/mock_test.go new file mode 100644 index 00000000..58af33cd --- /dev/null +++ b/actions/mock_test.go @@ -0,0 +1,60 @@ +package actions + +import ( + "context" + "sync" + + "github.com/castai/cluster-controller/actions/types" +) + +func newMockAPIClient(actions []*types.ClusterAction) *mockClient { + return &mockClient{Actions: actions} +} + +type mockAck struct { + ActionID string + Err *string +} + +type mockClient struct { + Actions []*types.ClusterAction + GetActionsErr error + Acks []*mockAck + AKSInitDataReq *types.AKSInitDataRequest + + mu sync.Mutex +} + +func (m *mockClient) SendAKSInitData(ctx context.Context, req *types.AKSInitDataRequest) error { + m.mu.Lock() + m.AKSInitDataReq = req + m.mu.Unlock() + return nil +} + +func (m *mockClient) GetActions(_ context.Context, _ string) ([]*types.ClusterAction, error) { + m.mu.Lock() + actions := m.Actions + m.mu.Unlock() + return actions, m.GetActionsErr +} + +func (m *mockClient) AckAction(_ context.Context, actionID string, req *types.AckClusterActionRequest) error { + m.mu.Lock() + defer m.mu.Unlock() + + m.removeAckedActions(actionID) + + m.Acks = append(m.Acks, &mockAck{ActionID: actionID, Err: req.Error}) + return nil +} + +func (m *mockClient) removeAckedActions(actionID string) { + var remaining []*types.ClusterAction + for _, action := range m.Actions { + if action.ID != actionID { + remaining = append(remaining, action) + } + } + m.Actions = remaining +} diff --git a/actions/patch_handler.go b/actions/patch_handler.go index d3e5e34b..52a51b36 100644 --- a/actions/patch_handler.go +++ b/actions/patch_handler.go @@ -5,6 +5,8 @@ import ( "fmt" "reflect" + "github.com/castai/cluster-controller/actions/types" + actiontypes "github.com/castai/cluster-controller/actions/types" "github.com/samber/lo" "github.com/sirupsen/logrus" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -12,8 +14,6 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" apitypes "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/dynamic" - - "github.com/castai/cluster-controller/castai" ) type patchHandler struct { @@ -28,8 +28,8 @@ func newPatchHandler(log logrus.FieldLogger, client dynamic.Interface) ActionHan } } -func (h *patchHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionPatch) +func (h *patchHandler) Handle(ctx context.Context, action *actiontypes.ClusterAction) error { + req, ok := action.Data().(*types.ActionPatch) if !ok { return newUnexpectedTypeErr(action.Data(), req) } diff --git a/actions/patch_handler_test.go b/actions/patch_handler_test.go index 341e73a8..fb2bd0f7 100644 --- a/actions/patch_handler_test.go +++ b/actions/patch_handler_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/castai/cluster-controller/actions/types" "github.com/samber/lo" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" @@ -14,25 +15,23 @@ import ( apitypes "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/dynamic/fake" client_testing "k8s.io/client-go/testing" - - "github.com/castai/cluster-controller/castai" ) func TestPatchHandler(t *testing.T) { tests := map[string]struct { objs []runtime.Object - action *castai.ClusterAction + action *types.ClusterAction err error }{ "should return an error when the action is nil": { - action: &castai.ClusterAction{}, - err: newUnexpectedTypeErr(nil, &castai.ActionPatch{}), + action: &types.ClusterAction{}, + err: newUnexpectedTypeErr(nil, &types.ActionPatch{}), }, "should return an error when the action is of a different type": { - action: &castai.ClusterAction{ - ActionDeleteNode: &castai.ActionDeleteNode{}, + action: &types.ClusterAction{ + ActionDeleteNode: &types.ActionDeleteNode{}, }, - err: newUnexpectedTypeErr(&castai.ActionDeleteNode{}, &castai.ActionPatch{}), + err: newUnexpectedTypeErr(&types.ActionDeleteNode{}, &types.ActionPatch{}), }, "should forward patch to the api in the request": { objs: []runtime.Object{ @@ -50,10 +49,10 @@ func TestPatchHandler(t *testing.T) { }, }, }, - action: &castai.ClusterAction{ - ActionPatch: &castai.ActionPatch{ - ID: castai.ObjectID{ - GroupVersionResource: castai.GroupVersionResource{ + action: &types.ClusterAction{ + ActionPatch: &types.ActionPatch{ + ID: types.ObjectID{ + GroupVersionResource: types.GroupVersionResource{ Group: "apps", Version: "v1", Resource: "deployments", diff --git a/actions/patch_node_handler.go b/actions/patch_node_handler.go index b8a1dd0e..5fcea621 100644 --- a/actions/patch_node_handler.go +++ b/actions/patch_node_handler.go @@ -8,12 +8,11 @@ import ( "reflect" "strconv" + "github.com/castai/cluster-controller/actions/types" "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/client-go/kubernetes" - - "github.com/castai/cluster-controller/castai" ) func newPatchNodeHandler(log logrus.FieldLogger, clientset kubernetes.Interface) ActionHandler { @@ -28,8 +27,8 @@ type patchNodeHandler struct { clientset kubernetes.Interface } -func (h *patchNodeHandler) Handle(ctx context.Context, action *castai.ClusterAction) error { - req, ok := action.Data().(*castai.ActionPatchNode) +func (h *patchNodeHandler) Handle(ctx context.Context, action *types.ClusterAction) error { + req, ok := action.Data().(*types.ActionPatchNode) if !ok { return fmt.Errorf("unexpected type %T for delete patch handler", action.Data()) } @@ -52,7 +51,7 @@ func (h *patchNodeHandler) Handle(ctx context.Context, action *castai.ClusterAct log := h.log.WithFields(logrus.Fields{ "node_name": req.NodeName, "node_id": req.NodeID, - "action": reflect.TypeOf(action.Data().(*castai.ActionPatchNode)).String(), + "action": reflect.TypeOf(action.Data().(*types.ActionPatchNode)).String(), actionIDLogField: action.ID, }) @@ -121,7 +120,7 @@ func patchNodeMapField(values map[string]string, patch map[string]string) map[st return values } -func patchTaints(taints []v1.Taint, patch []castai.NodeTaint) []v1.Taint { +func patchTaints(taints []v1.Taint, patch []types.NodeTaint) []v1.Taint { for _, v := range patch { taint := &v1.Taint{Key: v.Key, Value: v.Value, Effect: v1.TaintEffect(v.Effect)} if v.Key[0] == '-' { diff --git a/actions/patch_node_handler_test.go b/actions/patch_node_handler_test.go index 59b9cfd8..2d962a19 100644 --- a/actions/patch_node_handler_test.go +++ b/actions/patch_node_handler_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/castai/cluster-controller/actions/types" "github.com/google/uuid" "github.com/samber/lo" "github.com/sirupsen/logrus" @@ -12,8 +13,6 @@ import ( "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" - - "github.com/castai/cluster-controller/castai" ) func TestPatchNodeHandler(t *testing.T) { @@ -56,9 +55,9 @@ func TestPatchNodeHandler(t *testing.T) { clientset: clientset, } - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionPatchNode: &castai.ActionPatchNode{ + ActionPatchNode: &types.ActionPatchNode{ NodeName: "node1", Labels: map[string]string{ "-l1": "", @@ -68,7 +67,7 @@ func TestPatchNodeHandler(t *testing.T) { "-a1": "", "a2": "", }, - Taints: []castai.NodeTaint{ + Taints: []types.NodeTaint{ { Key: "t3", Value: "t3", @@ -120,9 +119,9 @@ func TestPatchNodeHandler(t *testing.T) { } clientset := fake.NewSimpleClientset(node) - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionPatchNode: &castai.ActionPatchNode{ + ActionPatchNode: &types.ActionPatchNode{ NodeName: "already-deleted-node", }, } @@ -155,9 +154,9 @@ func TestPatchNodeHandler(t *testing.T) { clientset: clientset, } - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionPatchNode: &castai.ActionPatchNode{ + ActionPatchNode: &types.ActionPatchNode{ NodeName: "node1", Unschedulable: lo.ToPtr(true), }, diff --git a/actions/send_aks_init_data_handler.go b/actions/send_aks_init_data_handler.go index a0bd778b..d60b79d3 100644 --- a/actions/send_aks_init_data_handler.go +++ b/actions/send_aks_init_data_handler.go @@ -16,12 +16,11 @@ import ( "regexp" "strings" + "github.com/castai/cluster-controller/actions/types" "github.com/sirupsen/logrus" - - "github.com/castai/cluster-controller/castai" ) -func newSendAKSInitDataHandler(log logrus.FieldLogger, client castai.Client) ActionHandler { +func newSendAKSInitDataHandler(log logrus.FieldLogger, client Client) ActionHandler { return &sendAKSInitDataHandler{ log: log, client: client, @@ -33,13 +32,13 @@ func newSendAKSInitDataHandler(log logrus.FieldLogger, client castai.Client) Act type sendAKSInitDataHandler struct { log logrus.FieldLogger - client castai.Client + client Client baseDir string cloudConfigPath string } -func (s *sendAKSInitDataHandler) Handle(ctx context.Context, _ *castai.ClusterAction) error { +func (s *sendAKSInitDataHandler) Handle(ctx context.Context, _ *types.ClusterAction) error { cloudConfig, err := s.readCloudConfigBase64(s.cloudConfigPath) if err != nil { return fmt.Errorf("reading cloud config: %w", err) @@ -56,7 +55,7 @@ func (s *sendAKSInitDataHandler) Handle(ctx context.Context, _ *castai.ClusterAc if err != nil { return fmt.Errorf("protected settings decrypt failed: %w", err) } - return s.client.SendAKSInitData(ctx, &castai.AKSInitDataRequest{ + return s.client.SendAKSInitData(ctx, &types.AKSInitDataRequest{ CloudConfigBase64: string(cloudConfig), ProtectedSettingsBase64: base64.StdEncoding.EncodeToString(protectedSettings), }) diff --git a/actions/send_aks_init_data_handler_test.go b/actions/send_aks_init_data_handler_test.go index d2512474..18bf01de 100644 --- a/actions/send_aks_init_data_handler_test.go +++ b/actions/send_aks_init_data_handler_test.go @@ -2,14 +2,13 @@ package actions import ( "context" - "github.com/google/uuid" "testing" + "github.com/castai/cluster-controller/actions/types" + "github.com/google/uuid" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" - - "github.com/castai/cluster-controller/castai" - "github.com/castai/cluster-controller/castai/mock" ) func TestAKSInitDataHandler(t *testing.T) { @@ -17,7 +16,7 @@ func TestAKSInitDataHandler(t *testing.T) { log := logrus.New() log.SetLevel(logrus.DebugLevel) - client := mock.NewMockAPIClient(nil) + client := newMockAPIClient(nil) h := sendAKSInitDataHandler{ log: log, client: client, @@ -25,9 +24,9 @@ func TestAKSInitDataHandler(t *testing.T) { baseDir: "../testdata/aks", } - action := &castai.ClusterAction{ + action := &types.ClusterAction{ ID: uuid.New().String(), - ActionSendAKSInitData: &castai.ActionSendAKSInitData{}, + ActionSendAKSInitData: &types.ActionSendAKSInitData{}, } ctx := context.Background() err := h.Handle(ctx, action) diff --git a/castai/types.go b/actions/types/types.go similarity index 93% rename from castai/types.go rename to actions/types/types.go index 258e1cd9..c297f62f 100644 --- a/castai/types.go +++ b/actions/types/types.go @@ -1,16 +1,12 @@ -package castai +package types import ( "fmt" "time" - "github.com/castai/cluster-controller/helm" - "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" -) -const ( - LabelNodeID = "provisioner.cast.ai/node-id" + "github.com/castai/cluster-controller/helm" ) type GetClusterActionsResponse struct { @@ -92,13 +88,6 @@ func (c *ClusterAction) Data() interface{} { return nil } -type LogEvent struct { - Level string `json:"level"` - Time time.Time `json:"time"` - Message string `json:"message"` - Fields logrus.Fields `json:"fields"` -} - type GroupVersionResource struct { Group string `json:"group"` Version string `json:"version"` @@ -188,11 +177,6 @@ type ActionCheckNodeDeleted struct { type ActionCheckNodeStatus_Status string -const ( - ActionCheckNodeStatus_READY ActionCheckNodeStatus_Status = "NodeStatus_READY" - ActionCheckNodeStatus_DELETED ActionCheckNodeStatus_Status = "NodeStatus_DELETED" -) - type ActionCheckNodeStatus struct { NodeName string `json:"nodeName"` NodeID string `json:"nodeId"` diff --git a/castai/client.go b/castai/client.go index 332656ac..a9f3e438 100644 --- a/castai/client.go +++ b/castai/client.go @@ -11,6 +11,8 @@ import ( "github.com/sirupsen/logrus" "golang.org/x/net/http2" + "github.com/castai/cluster-controller/actions" + "github.com/castai/cluster-controller/actions/types" "github.com/castai/cluster-controller/config" ) @@ -21,13 +23,10 @@ const ( ) type Client interface { - GetActions(ctx context.Context, k8sVersion string) ([]*ClusterAction, error) - AckAction(ctx context.Context, actionID string, req *AckClusterActionRequest) error SendLogs(ctx context.Context, req *LogEvent) error - SendAKSInitData(ctx context.Context, req *AKSInitDataRequest) error } -func NewClient(log *logrus.Logger, rest *resty.Client, clusterID string) Client { +func NewClient(log *logrus.Logger, rest *resty.Client, clusterID string) *client { return &client{ log: log, rest: rest, @@ -92,7 +91,10 @@ type client struct { clusterID string } -func (c *client) SendAKSInitData(ctx context.Context, req *AKSInitDataRequest) error { +// client must satisfy actions.Client. +var _ actions.Client = new(client) + +func (c *client) SendAKSInitData(ctx context.Context, req *types.AKSInitDataRequest) error { resp, err := c.rest.R(). SetBody(req). SetContext(ctx). @@ -108,6 +110,13 @@ func (c *client) SendAKSInitData(ctx context.Context, req *AKSInitDataRequest) e return nil } +type LogEvent struct { + Level string `json:"level"` + Time time.Time `json:"time"` + Message string `json:"message"` + Fields logrus.Fields `json:"fields"` +} + func (c *client) SendLogs(ctx context.Context, req *LogEvent) error { // Server expects fields values to be strings. If they're not it fails with BAD_REQUEST/400. // Alternatively we could use "google/protobuf/any.proto" on server side but ATM it doesn't work. @@ -134,8 +143,8 @@ func (c *client) SendLogs(ctx context.Context, req *LogEvent) error { return nil } -func (c *client) GetActions(ctx context.Context, k8sVersion string) ([]*ClusterAction, error) { - res := &GetClusterActionsResponse{} +func (c *client) GetActions(ctx context.Context, k8sVersion string) ([]*types.ClusterAction, error) { + res := &types.GetClusterActionsResponse{} resp, err := c.rest.R(). SetContext(ctx). SetResult(res). @@ -150,7 +159,7 @@ func (c *client) GetActions(ctx context.Context, k8sVersion string) ([]*ClusterA return res.Items, nil } -func (c *client) AckAction(ctx context.Context, actionID string, req *AckClusterActionRequest) error { +func (c *client) AckAction(ctx context.Context, actionID string, req *types.AckClusterActionRequest) error { resp, err := c.rest.R(). SetContext(ctx). SetBody(req). diff --git a/helm/client.go b/helm/client.go index 5c435827..ff6498be 100644 --- a/helm/client.go +++ b/helm/client.go @@ -1,5 +1,3 @@ -//go:generate mockgen -source ./client.go -destination ./mock/client.go . Client - package helm import ( @@ -53,7 +51,7 @@ type RollbackOptions struct { ReleaseName string } -func NewClient(log logrus.FieldLogger, restConfig *rest.Config) Client { +func NewClient(log logrus.FieldLogger, restConfig *rest.Config) *client { return &client{ log: log, configurationGetter: &configurationGetter{ @@ -66,14 +64,6 @@ func NewClient(log logrus.FieldLogger, restConfig *rest.Config) Client { } } -type Client interface { - Install(ctx context.Context, opts InstallOptions) (*release.Release, error) - Uninstall(opts UninstallOptions) (*release.UninstallReleaseResponse, error) - Upgrade(ctx context.Context, opts UpgradeOptions) (*release.Release, error) - Rollback(opts RollbackOptions) error - GetRelease(opts GetReleaseOptions) (*release.Release, error) -} - type client struct { log logrus.FieldLogger configurationGetter ConfigurationGetter diff --git a/helm/mock/client.go b/helm/mock/client.go deleted file mode 100644 index 2c18c573..00000000 --- a/helm/mock/client.go +++ /dev/null @@ -1,150 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: ./client.go - -// Package mock_helm is a generated GoMock package. -package mock_helm - -import ( - context "context" - reflect "reflect" - - helm "github.com/castai/cluster-controller/helm" - gomock "github.com/golang/mock/gomock" - action "helm.sh/helm/v3/pkg/action" - release "helm.sh/helm/v3/pkg/release" -) - -// MockClient is a mock of Client interface. -type MockClient struct { - ctrl *gomock.Controller - recorder *MockClientMockRecorder -} - -// MockClientMockRecorder is the mock recorder for MockClient. -type MockClientMockRecorder struct { - mock *MockClient -} - -// NewMockClient creates a new mock instance. -func NewMockClient(ctrl *gomock.Controller) *MockClient { - mock := &MockClient{ctrl: ctrl} - mock.recorder = &MockClientMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockClient) EXPECT() *MockClientMockRecorder { - return m.recorder -} - -// GetRelease mocks base method. -func (m *MockClient) GetRelease(opts helm.GetReleaseOptions) (*release.Release, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetRelease", opts) - ret0, _ := ret[0].(*release.Release) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// GetRelease indicates an expected call of GetRelease. -func (mr *MockClientMockRecorder) GetRelease(opts interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRelease", reflect.TypeOf((*MockClient)(nil).GetRelease), opts) -} - -// Install mocks base method. -func (m *MockClient) Install(ctx context.Context, opts helm.InstallOptions) (*release.Release, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Install", ctx, opts) - ret0, _ := ret[0].(*release.Release) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Install indicates an expected call of Install. -func (mr *MockClientMockRecorder) Install(ctx, opts interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Install", reflect.TypeOf((*MockClient)(nil).Install), ctx, opts) -} - -// Rollback mocks base method. -func (m *MockClient) Rollback(opts helm.RollbackOptions) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Rollback", opts) - ret0, _ := ret[0].(error) - return ret0 -} - -// Rollback indicates an expected call of Rollback. -func (mr *MockClientMockRecorder) Rollback(opts interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Rollback", reflect.TypeOf((*MockClient)(nil).Rollback), opts) -} - -// Uninstall mocks base method. -func (m *MockClient) Uninstall(opts helm.UninstallOptions) (*release.UninstallReleaseResponse, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Uninstall", opts) - ret0, _ := ret[0].(*release.UninstallReleaseResponse) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Uninstall indicates an expected call of Uninstall. -func (mr *MockClientMockRecorder) Uninstall(opts interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Uninstall", reflect.TypeOf((*MockClient)(nil).Uninstall), opts) -} - -// Upgrade mocks base method. -func (m *MockClient) Upgrade(ctx context.Context, opts helm.UpgradeOptions) (*release.Release, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Upgrade", ctx, opts) - ret0, _ := ret[0].(*release.Release) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Upgrade indicates an expected call of Upgrade. -func (mr *MockClientMockRecorder) Upgrade(ctx, opts interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Upgrade", reflect.TypeOf((*MockClient)(nil).Upgrade), ctx, opts) -} - -// MockConfigurationGetter is a mock of ConfigurationGetter interface. -type MockConfigurationGetter struct { - ctrl *gomock.Controller - recorder *MockConfigurationGetterMockRecorder -} - -// MockConfigurationGetterMockRecorder is the mock recorder for MockConfigurationGetter. -type MockConfigurationGetterMockRecorder struct { - mock *MockConfigurationGetter -} - -// NewMockConfigurationGetter creates a new mock instance. -func NewMockConfigurationGetter(ctrl *gomock.Controller) *MockConfigurationGetter { - mock := &MockConfigurationGetter{ctrl: ctrl} - mock.recorder = &MockConfigurationGetterMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockConfigurationGetter) EXPECT() *MockConfigurationGetterMockRecorder { - return m.recorder -} - -// Get mocks base method. -func (m *MockConfigurationGetter) Get(namespace string) (*action.Configuration, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Get", namespace) - ret0, _ := ret[0].(*action.Configuration) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Get indicates an expected call of Get. -func (mr *MockConfigurationGetterMockRecorder) Get(namespace interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockConfigurationGetter)(nil).Get), namespace) -} diff --git a/log/exporter_test.go b/log/exporter_test.go index 093db9bd..45f06d7e 100644 --- a/log/exporter_test.go +++ b/log/exporter_test.go @@ -20,7 +20,7 @@ func TestLogExporter(t *testing.T) { logger, hook := test.NewNullLogger() defer hook.Reset() - client := mock.NewMockAPIClient(nil) + client := mock.NewMockClient(nil) e := NewExporter(nil, client) logger.AddHook(e) log := logger.WithFields(logrus.Fields{ diff --git a/main.go b/main.go index 2c33aa15..ebc52862 100644 --- a/main.go +++ b/main.go @@ -95,7 +95,7 @@ func main() { func run( ctx context.Context, - client castai.Client, + client actions.Client, logger *logrus.Logger, cfg config.Config, binVersion *config.ClusterControllerVersion, @@ -123,8 +123,6 @@ func run( restConfigLeader.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(float32(cfg.KubeClient.QPS), cfg.KubeClient.Burst) restConfigDynamic.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(float32(cfg.KubeClient.QPS), cfg.KubeClient.Burst) - helmClient := helm.NewClient(logger, restconfig) - clientset, err := kubernetes.NewForConfig(restconfig) if err != nil { return err @@ -176,7 +174,7 @@ func run( clientset, dynamicClient, client, - helmClient, + helm.NewClient(logger, restconfig), healthzAction, )