From e671a218e96a68edb9da1061bff2ce8eca5eaf68 Mon Sep 17 00:00:00 2001 From: rainzm Date: Sat, 9 Jan 2021 17:03:49 +0800 Subject: [PATCH] feat(cloudcommon): set TaskNotifyUrl in GetTaskRequestHeader To support cross-service task callbacks, you must set TaskNotifyUrl, which is automatically added to TaskRequestHeader here --- pkg/cloudcommon/db/taskman/tasks.go | 11 +++++++++++ pkg/compute/service/service.go | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/cloudcommon/db/taskman/tasks.go b/pkg/cloudcommon/db/taskman/tasks.go index b10355f9a4..c9aec3443e 100644 --- a/pkg/cloudcommon/db/taskman/tasks.go +++ b/pkg/cloudcommon/db/taskman/tasks.go @@ -19,6 +19,7 @@ import ( "database/sql" "fmt" "net/http" + "path/filepath" "reflect" "runtime/debug" "strconv" @@ -826,9 +827,19 @@ func (task *STask) GetTaskRequestHeader() http.Header { } header := mcclient.GetTokenHeaders(userCred) header.Set(mcclient.TASK_ID, task.GetTaskId()) + if len(serviceUrl) > 0 { + notifyUrl := filepath.Join(serviceUrl, "tasks", task.GetTaskId()) + header.Set(mcclient.TASK_NOTIFY_URL, notifyUrl) + } return header } +var serviceUrl string + +func SetServiceUrl(url string) { + serviceUrl = url +} + func (task *STask) GetStartTime() time.Time { return task.CreatedAt } diff --git a/pkg/compute/service/service.go b/pkg/compute/service/service.go index cc602ce9c1..21768fa04f 100644 --- a/pkg/compute/service/service.go +++ b/pkg/compute/service/service.go @@ -27,10 +27,12 @@ import ( "yunion.io/x/pkg/errors" api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/apis/identity" "yunion.io/x/onecloud/pkg/cloudcommon" app_common "yunion.io/x/onecloud/pkg/cloudcommon/app" "yunion.io/x/onecloud/pkg/cloudcommon/cronman" "yunion.io/x/onecloud/pkg/cloudcommon/db" + "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" "yunion.io/x/onecloud/pkg/cloudcommon/elect" "yunion.io/x/onecloud/pkg/cloudcommon/etcd" common_options "yunion.io/x/onecloud/pkg/cloudcommon/options" @@ -44,6 +46,7 @@ import ( _ "yunion.io/x/onecloud/pkg/compute/tasks" "yunion.io/x/onecloud/pkg/controller/autoscaling" "yunion.io/x/onecloud/pkg/httperrors" + "yunion.io/x/onecloud/pkg/mcclient/auth" "yunion.io/x/onecloud/pkg/multicloud/esxi" _ "yunion.io/x/onecloud/pkg/multicloud/loader" ) @@ -64,8 +67,13 @@ func StartService() { log.Infof("Auth complete!!") }) common_options.StartOptionManager(opts, opts.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, options.OnOptionsChange) + serviceUrl, err := auth.GetServiceURL(api.SERVICE_TYPE, opts.Region, "", identity.EndpointInterfaceInternal) + if err != nil { + log.Fatalf("unable to get service url: %v", err) + } + taskman.SetServiceUrl(serviceUrl) - err := esxi.InitEsxiConfig(opts.EsxiOptions) + err = esxi.InitEsxiConfig(opts.EsxiOptions) if err != nil { log.Fatalf("unable to init esxi configs: %v", err) }