From 2598c96063b07bc70e0d11ba954905fbe77949d8 Mon Sep 17 00:00:00 2001 From: Future-Outlier Date: Fri, 22 Sep 2023 12:44:44 +0800 Subject: [PATCH] Add supportTaskTypes for agentservice without write it in config twice. (#398) Signed-off-by: Future Outlier Co-authored-by: Future Outlier --- go/tasks/plugins/webapi/agent/config.go | 4 ---- go/tasks/plugins/webapi/agent/integration_test.go | 2 +- go/tasks/plugins/webapi/agent/plugin.go | 12 ++++++++---- go/tasks/plugins/webapi/agent/plugin_test.go | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/go/tasks/plugins/webapi/agent/config.go b/go/tasks/plugins/webapi/agent/config.go index 58355c5ee..fb369b380 100644 --- a/go/tasks/plugins/webapi/agent/config.go +++ b/go/tasks/plugins/webapi/agent/config.go @@ -44,7 +44,6 @@ var ( Insecure: true, DefaultTimeout: config.Duration{Duration: 10 * time.Second}, }, - SupportedTaskTypes: []string{"task_type_1", "task_type_2"}, } configSection = pluginsConfig.MustRegisterSubSection("agent-service", &defaultConfig) @@ -66,9 +65,6 @@ type Config struct { // Maps task types to their agents. {TaskType: AgentId} AgentForTaskTypes map[string]string `json:"agentForTaskTypes" pflag:"-,"` - - // SupportedTaskTypes is a list of task types that are supported by this plugin. - SupportedTaskTypes []string `json:"supportedTaskTypes" pflag:"-,Defines a list of task types that are supported by this plugin."` } type Agent struct { diff --git a/go/tasks/plugins/webapi/agent/integration_test.go b/go/tasks/plugins/webapi/agent/integration_test.go index f66c5f733..8cf64f478 100644 --- a/go/tasks/plugins/webapi/agent/integration_test.go +++ b/go/tasks/plugins/webapi/agent/integration_test.go @@ -161,7 +161,7 @@ func TestEndToEnd(t *testing.T) { tr.OnRead(context.Background()).Return(nil, fmt.Errorf("read fail")) tCtx.OnTaskReader().Return(tr) - agentPlugin := newAgentPlugin() + agentPlugin := newAgentPlugin(SupportedTaskTypes{}) pluginEntry := pluginmachinery.CreateRemotePlugin(agentPlugin) plugin, err := pluginEntry.LoadPlugin(context.TODO(), newFakeSetupContext("test3")) assert.NoError(t, err) diff --git a/go/tasks/plugins/webapi/agent/plugin.go b/go/tasks/plugins/webapi/agent/plugin.go index 0153d9dc1..9e663319e 100644 --- a/go/tasks/plugins/webapi/agent/plugin.go +++ b/go/tasks/plugins/webapi/agent/plugin.go @@ -29,6 +29,8 @@ import ( type GetClientFunc func(ctx context.Context, agent *Agent, connectionCache map[*Agent]*grpc.ClientConn) (service.AsyncAgentServiceClient, error) +type TaskType = string +type SupportedTaskTypes []TaskType type Plugin struct { metricScope promutils.Scope cfg *Config @@ -296,8 +298,10 @@ func getFinalContext(ctx context.Context, operation string, agent *Agent) (conte return context.WithTimeout(ctx, timeout) } -func newAgentPlugin() webapi.PluginEntry { - supportedTaskTypes := GetConfig().SupportedTaskTypes +func newAgentPlugin(supportedTaskTypes SupportedTaskTypes) webapi.PluginEntry { + if len(supportedTaskTypes) == 0 { + supportedTaskTypes = SupportedTaskTypes{"default_supported_task_type"} + } return webapi.PluginEntry{ ID: "agent-service", @@ -313,9 +317,9 @@ func newAgentPlugin() webapi.PluginEntry { } } -func RegisterAgentPlugin() { +func RegisterAgentPlugin(supportedTaskTypes SupportedTaskTypes) { gob.Register(ResourceMetaWrapper{}) gob.Register(ResourceWrapper{}) - pluginmachinery.PluginRegistry().RegisterRemotePlugin(newAgentPlugin()) + pluginmachinery.PluginRegistry().RegisterRemotePlugin(newAgentPlugin(supportedTaskTypes)) } diff --git a/go/tasks/plugins/webapi/agent/plugin_test.go b/go/tasks/plugins/webapi/agent/plugin_test.go index bf9e25e20..3c413bf37 100644 --- a/go/tasks/plugins/webapi/agent/plugin_test.go +++ b/go/tasks/plugins/webapi/agent/plugin_test.go @@ -43,7 +43,7 @@ func TestPlugin(t *testing.T) { }) t.Run("test newAgentPlugin", func(t *testing.T) { - p := newAgentPlugin() + p := newAgentPlugin(SupportedTaskTypes{}) assert.NotNil(t, p) assert.Equal(t, "agent-service", p.ID) assert.NotNil(t, p.PluginLoader)