-
Notifications
You must be signed in to change notification settings - Fork 4
chore: separate actions from the controller service #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package actions | ||
|
|
||
| import ( | ||
| "reflect" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
| "k8s.io/client-go/dynamic" | ||
| "k8s.io/client-go/kubernetes" | ||
|
|
||
| "github.com/castai/cluster-controller/internal/castai" | ||
| "github.com/castai/cluster-controller/internal/helm" | ||
| ) | ||
|
|
||
| type ActionHandlers map[reflect.Type]ActionHandler | ||
|
|
||
| func NewDefaultActionHandlers( | ||
| k8sVersion string, | ||
| castNamespace string, | ||
| log logrus.FieldLogger, | ||
| clientset *kubernetes.Clientset, | ||
| dynamicClient dynamic.Interface, | ||
| helmClient helm.Client, | ||
| ) ActionHandlers { | ||
| return ActionHandlers{ | ||
| reflect.TypeOf(&castai.ActionDeleteNode{}): NewDeleteNodeHandler(log, clientset), | ||
| reflect.TypeOf(&castai.ActionDrainNode{}): NewDrainNodeHandler(log, clientset, castNamespace), | ||
| reflect.TypeOf(&castai.ActionPatchNode{}): NewPatchNodeHandler(log, clientset), | ||
| reflect.TypeOf(&castai.ActionCreateEvent{}): NewCreateEventHandler(log, clientset), | ||
| reflect.TypeOf(&castai.ActionChartUpsert{}): NewChartUpsertHandler(log, helmClient), | ||
| reflect.TypeOf(&castai.ActionChartUninstall{}): NewChartUninstallHandler(log, helmClient), | ||
| reflect.TypeOf(&castai.ActionChartRollback{}): NewChartRollbackHandler(log, helmClient, k8sVersion), | ||
| reflect.TypeOf(&castai.ActionDisconnectCluster{}): NewDisconnectClusterHandler(log, clientset), | ||
| reflect.TypeOf(&castai.ActionCheckNodeDeleted{}): NewCheckNodeDeletedHandler(log, clientset), | ||
| reflect.TypeOf(&castai.ActionCheckNodeStatus{}): NewCheckNodeStatusHandler(log, clientset), | ||
| reflect.TypeOf(&castai.ActionEvictPod{}): NewEvictPodHandler(log, clientset), | ||
| reflect.TypeOf(&castai.ActionPatch{}): NewPatchHandler(log, dynamicClient), | ||
| reflect.TypeOf(&castai.ActionCreate{}): NewCreateHandler(log, dynamicClient), | ||
| reflect.TypeOf(&castai.ActionDelete{}): NewDeleteHandler(log, dynamicClient), | ||
| } | ||
| } | ||
|
|
||
| func (h ActionHandlers) Close() error { | ||
| return h[reflect.TypeOf(&castai.ActionCreateEvent{})].(*CreateEventHandler).Close() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package controller | |
| import ( | ||
| "context" | ||
| "fmt" | ||
| "reflect" | ||
| "testing" | ||
| "time" | ||
|
|
||
|
|
@@ -11,12 +12,12 @@ import ( | |
| "github.com/sirupsen/logrus" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/goleak" | ||
| "k8s.io/client-go/kubernetes" | ||
|
|
||
| "github.com/castai/cluster-controller/health" | ||
| "github.com/castai/cluster-controller/internal/actions" | ||
| mock_actions "github.com/castai/cluster-controller/internal/actions/mock" | ||
| "github.com/castai/cluster-controller/internal/castai" | ||
| "github.com/castai/cluster-controller/internal/castai/mock" | ||
| mock_castai "github.com/castai/cluster-controller/internal/castai/mock" | ||
| ) | ||
|
|
||
| // nolint: govet | ||
|
|
@@ -99,9 +100,9 @@ func TestController_Run(t *testing.T) { | |
| }, | ||
| }, | ||
| }, nil).Times(1).MinTimes(1) | ||
| m.EXPECT().AckAction(gomock.Any(), "a1", gomock.Any()).Return(nil).MinTimes(1) | ||
| m.EXPECT().AckAction(gomock.Any(), "a2", gomock.Any()).Return(nil).MinTimes(1) | ||
| m.EXPECT().AckAction(gomock.Any(), "a3", gomock.Any()).Return(nil).MinTimes(1) | ||
| m.EXPECT().AckAction(gomock.Any(), "a1", &castai.AckClusterActionRequest{}).Return(nil).MinTimes(1) | ||
| m.EXPECT().AckAction(gomock.Any(), "a2", &castai.AckClusterActionRequest{}).Return(nil).MinTimes(1) | ||
| m.EXPECT().AckAction(gomock.Any(), "a3", &castai.AckClusterActionRequest{}).Return(nil).MinTimes(1) | ||
| }, | ||
| }, | ||
| }, | ||
|
|
@@ -240,22 +241,25 @@ func TestController_Run(t *testing.T) { | |
| if tt.fields.tuneMockCastAIClient != nil { | ||
| tt.fields.tuneMockCastAIClient(client) | ||
| } | ||
| s := NewService( | ||
| logrus.New(), | ||
| tt.fields.cfg, | ||
| tt.fields.k8sVersion, | ||
| kubernetes.New(nil), | ||
| nil, | ||
| client, | ||
| nil, | ||
| health.NewHealthzProvider(health.HealthzCfg{HealthyPollIntervalLimit: pollTimeout}, logrus.New())) | ||
|
|
||
| handler := mock_actions.NewMockActionHandler(m) | ||
| if tt.fields.tuneMockHandler != nil { | ||
| tt.fields.tuneMockHandler(handler) | ||
| } | ||
| for k := range s.actionHandlers { | ||
| s.actionHandlers[k] = handler | ||
| testActionHandlers := map[reflect.Type]actions.ActionHandler{ | ||
| reflect.TypeOf(&castai.ActionDeleteNode{}): handler, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We used to initialize them all with mock handler, correct? Does this mean we only test these 3?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correct, only 3 action types are actually tested here. I'd argue if only fake actions should be used instead as it's mostly to test if the controller implementation works correctly |
||
| reflect.TypeOf(&castai.ActionDrainNode{}): handler, | ||
| reflect.TypeOf(&castai.ActionPatchNode{}): handler, | ||
| } | ||
|
|
||
| s := NewService( | ||
| logrus.New(), | ||
| tt.fields.cfg, | ||
| tt.fields.k8sVersion, | ||
| client, | ||
| health.NewHealthzProvider(health.HealthzCfg{HealthyPollIntervalLimit: pollTimeout}, logrus.New()), | ||
| testActionHandlers) | ||
|
|
||
| s.Run(tt.args.ctx()) | ||
| }) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can panic, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really when used with the constructor, but this is mostly copy & paste from the existing place
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, maybe we should make it not panic even in this case, minor change