diff --git a/cmd/climc/climc.go b/cmd/climc/climc.go index ffe561bdcf..7168dc194e 100644 --- a/cmd/climc/climc.go +++ b/cmd/climc/climc.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "fmt" "io/ioutil" @@ -9,7 +10,7 @@ import ( "strings" "time" - "github.com/c-bata/go-prompt" + prompt "github.com/c-bata/go-prompt" "yunion.io/x/log" "yunion.io/x/pkg/util/version" @@ -183,7 +184,9 @@ func newClientSession(options *BaseOptions) (*mcclient.ClientSession, error) { if options.ApiVersion != "" { mcclient.DisableApiVersionByModule() } - session := client.NewSession(options.OsRegionName, + session := client.NewSession( + context.Background(), + options.OsRegionName, options.OsZoneName, options.OsEndpointType, cacheToken, diff --git a/pkg/cloudcommon/db/quotas/handler.go b/pkg/cloudcommon/db/quotas/handler.go index 4729168e4f..a8e530a152 100644 --- a/pkg/cloudcommon/db/quotas/handler.go +++ b/pkg/cloudcommon/db/quotas/handler.go @@ -54,7 +54,7 @@ func queryQuota(ctx context.Context, projectId string) (*jsonutils.JSONDict, err return nil, err } usage := _manager.newQuota() - err = usage.FetchUsage(projectId) + err = usage.FetchUsage(ctx, projectId) if err != nil { return nil, err } diff --git a/pkg/cloudcommon/db/quotas/quotas.go b/pkg/cloudcommon/db/quotas/quotas.go index 8aaf560b7d..b7f05c8057 100644 --- a/pkg/cloudcommon/db/quotas/quotas.go +++ b/pkg/cloudcommon/db/quotas/quotas.go @@ -17,7 +17,7 @@ const ( type IQuota interface { FetchSystemQuota() - FetchUsage(projectId string) error + FetchUsage(ctx context.Context, projectId string) error Update(quota IQuota) Add(quota IQuota) Sub(quota IQuota) @@ -116,7 +116,7 @@ func (manager *SQuotaManager) _checkQuota(ctx context.Context, userCred mcclient return nil, err } used := manager.newQuota() - err = used.FetchUsage(projectId) + err = used.FetchUsage(ctx, projectId) if err != nil { log.Errorf("fail to get quota usage %s", err) return nil, err diff --git a/pkg/cloudcommon/db/tenantcache.go b/pkg/cloudcommon/db/tenantcache.go index 6b8f4c3f69..4ef053332a 100644 --- a/pkg/cloudcommon/db/tenantcache.go +++ b/pkg/cloudcommon/db/tenantcache.go @@ -78,7 +78,7 @@ func (manager *STenantCacheManager) FetchTenantByName(ctx context.Context, idStr } func (manager *STenantCacheManager) fetchTenantFromKeystone(ctx context.Context, idStr string) (*STenant, error) { - s := auth.GetAdminSession(consts.GetRegion(), "v1") + s := auth.GetAdminSession(ctx, consts.GetRegion(), "v1") tenant, err := modules.Projects.Get(s, idStr, nil) if err != nil { log.Errorf("fetch project fail %s", err) diff --git a/pkg/cloudcommon/policy/policy.go b/pkg/cloudcommon/policy/policy.go index a856902d89..9c9b2882cf 100644 --- a/pkg/cloudcommon/policy/policy.go +++ b/pkg/cloudcommon/policy/policy.go @@ -1,6 +1,7 @@ package policy import ( + "context" "fmt" "sort" "strings" @@ -183,7 +184,7 @@ func parseJsonPolicy(obj jsonutils.JSONObject) (string, rbacutils.SRbacPolicy, e } func fetchPolicies() (map[string]rbacutils.SRbacPolicy, map[string]rbacutils.SRbacPolicy, error) { - s := auth.GetAdminSession(consts.GetRegion(), "v1") + s := auth.GetAdminSession(context.Background(), consts.GetRegion(), "v1") policies := make(map[string]rbacutils.SRbacPolicy) adminPolicies := make(map[string]rbacutils.SRbacPolicy) diff --git a/pkg/cloudprovider/resources.go b/pkg/cloudprovider/resources.go index 6ac7f3c28d..2098809133 100644 --- a/pkg/cloudprovider/resources.go +++ b/pkg/cloudprovider/resources.go @@ -95,7 +95,7 @@ type ICloudStoragecache interface { DownloadImage(userCred mcclient.TokenCredential, imageId string, extId string, path string) (jsonutils.JSONObject, error) - UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) + UploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) } type ICloudStorage interface { diff --git a/pkg/compute/hostdrivers/managedvirtual.go b/pkg/compute/hostdrivers/managedvirtual.go index 3865e401aa..43ac28e46d 100644 --- a/pkg/compute/hostdrivers/managedvirtual.go +++ b/pkg/compute/hostdrivers/managedvirtual.go @@ -46,7 +46,7 @@ func (self *SManagedVirtualizationHostDriver) CheckAndSetCacheImage(ctx context. return nil, err } - extImgId, err := iStorageCache.UploadImage(userCred, imageId, osArch, osType, osDist, osVersion, scimg.ExternalId, isForce) + extImgId, err := iStorageCache.UploadImage(ctx, userCred, imageId, osArch, osType, osDist, osVersion, scimg.ExternalId, isForce) if err != nil { return nil, err diff --git a/pkg/compute/models/cachedimages.go b/pkg/compute/models/cachedimages.go index 302c8f7814..6b77db2a71 100644 --- a/pkg/compute/models/cachedimages.go +++ b/pkg/compute/models/cachedimages.go @@ -206,7 +206,7 @@ func (manager *SCachedimageManager) GetImageById(ctx context.Context, userCred m } } } - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(ctx, options.Options.Region, "") obj, err := modules.Images.Get(s, imageId, nil) if err != nil { log.Errorf("GetImageById %s error %s", imageId, err) @@ -220,7 +220,7 @@ func (manager *SCachedimageManager) GetImageById(ctx context.Context, userCred m } func (manager *SCachedimageManager) getImageByName(ctx context.Context, userCred mcclient.TokenCredential, imageId string) (*SImage, error) { - s := auth.GetSession(userCred, options.Options.Region, "") + s := auth.GetSession(ctx, userCred, options.Options.Region, "") obj, err := modules.Images.GetByName(s, imageId, nil) if err != nil { return nil, err diff --git a/pkg/compute/models/cloudproviders.go b/pkg/compute/models/cloudproviders.go index c91a4d6201..581599cb00 100644 --- a/pkg/compute/models/cloudproviders.go +++ b/pkg/compute/models/cloudproviders.go @@ -219,7 +219,7 @@ func (self *SCloudprovider) syncProject(ctx context.Context) error { var projectId string if err == sql.ErrNoRows { // create one - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(ctx, options.Options.Region, "") params := jsonutils.NewDict() params.Add(jsonutils.NewString(self.Name), "name") params.Add(jsonutils.NewString(fmt.Sprintf("auto create from cloud provider %s (%s)", self.Name, self.Id)), "description") diff --git a/pkg/compute/models/disks.go b/pkg/compute/models/disks.go index 4847e24615..a04bd305a4 100644 --- a/pkg/compute/models/disks.go +++ b/pkg/compute/models/disks.go @@ -703,7 +703,7 @@ func (self *SDisk) PrepareSaveImage(ctx context.Context, userCred mcclient.Token } data.Add(jsonutils.NewString(self.DiskFormat), "disk_format") name, _ := data.GetString("name") - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(ctx, options.Options.Region, "") if imageList, err := modules.Images.List(s, jsonutils.Marshal(map[string]string{"name": name, "admin": "true"})); err != nil { return "", err } else if imageList.Total > 0 { diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index 5b5617b28c..16070194b8 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -763,12 +763,12 @@ func (self *SHostManager) GetPropertyBmStartRegisterScript(ctx context.Context, } func (maanger *SHostManager) ClearAllSchedDescCache() error { - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(context.Background(), options.Options.Region, "") return modules.SchedManager.CleanCache(s, "") } func (maanger *SHostManager) ClearSchedDescCache(hostId string) error { - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(context.Background(), options.Options.Region, "") return modules.SchedManager.CleanCache(s, hostId) } @@ -2178,7 +2178,7 @@ func (manager *SHostManager) GetHostsByManagerAndRegion(managerId string, region }*/ func (self *SHost) Request(userCred mcclient.TokenCredential, method string, url string, headers http.Header, body jsonutils.JSONObject) (jsonutils.JSONObject, error) { - s := auth.GetSession(userCred, "", "") + s := auth.GetSession(nil, userCred, "", "") _, ret, err := s.JSONRequest(self.ManagerUri, "", method, url, headers, body) return ret, err } diff --git a/pkg/compute/models/quotas.go b/pkg/compute/models/quotas.go index 0a0fb019a5..c27af636d0 100644 --- a/pkg/compute/models/quotas.go +++ b/pkg/compute/models/quotas.go @@ -1,6 +1,7 @@ package models import ( + "context" "errors" "fmt" @@ -72,7 +73,7 @@ func (self *SQuota) FetchSystemQuota() { self.Snapshot = options.Options.DefaultSnapshotQuota } -func (self *SQuota) FetchUsage(projectId string) error { +func (self *SQuota) FetchUsage(ctx context.Context, projectId string) error { diskSize := totalDiskSize(projectId, tristate.None, tristate.None, false) net := totalGuestNicCount(projectId, nil, false) guest := totalGuestResourceCount(projectId, nil, nil, nil, false, false, nil, nil, nil) @@ -91,7 +92,7 @@ func (self *SQuota) FetchUsage(projectId string) error { self.Bw = net.InternalBandwidth self.Ebw = net.ExternalBandwidth self.Keypair = 0 // keypair - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(ctx, options.Options.Region, "") self.Image, _ = modules.Images.GetPrivateImageCount(s, projectId, true) self.Group = 0 self.Secgroup = totalSecurityGroupCount(projectId) diff --git a/pkg/compute/skus/skus.go b/pkg/compute/skus/skus.go index bbbb4ae511..fe5557c8b1 100644 --- a/pkg/compute/skus/skus.go +++ b/pkg/compute/skus/skus.go @@ -101,7 +101,7 @@ func processSkuData(ndata jsonutils.JSONObject) jsonutils.JSONObject { } func (self *SkusZone) Init() error { - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(context.Background(), options.Options.Region, "") p, r, z := self.getExternalZone() limit := 1024 offset := 0 diff --git a/pkg/compute/tasks/disk_save_task.go b/pkg/compute/tasks/disk_save_task.go index 32a29f194d..38eafff5fa 100644 --- a/pkg/compute/tasks/disk_save_task.go +++ b/pkg/compute/tasks/disk_save_task.go @@ -102,7 +102,7 @@ func (self *DiskSaveTask) TaskFailed(ctx context.Context, resion string) { self.SetStageFailed(ctx, resion) if imageId, err := self.GetParams().GetString("image_id"); err != nil && len(imageId) > 0 { log.Errorf("save disk task failed, set image %s killed", imageId) - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(ctx, options.Options.Region, "") mc.Images.Update(s, imageId, jsonutils.Marshal(map[string]string{"status": "killed"})) } } diff --git a/pkg/compute/tasks/schedule.go b/pkg/compute/tasks/schedule.go index 245a64c967..48c5588ae2 100644 --- a/pkg/compute/tasks/schedule.go +++ b/pkg/compute/tasks/schedule.go @@ -104,7 +104,7 @@ func doScheduleObjects( task.SetStage("OnScheduleComplete", schedtags) - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(ctx, options.Options.Region, "") results, err := modules.SchedManager.DoSchedule(s, parmas, len(objs)) if err != nil { onSchedulerRequestFail(ctx, task, objs, fmt.Sprintf("Scheduler fail: %s", err)) diff --git a/pkg/dns/dns.go b/pkg/dns/dns.go index daca29868e..f5946b7d07 100644 --- a/pkg/dns/dns.go +++ b/pkg/dns/dns.go @@ -106,8 +106,8 @@ func (r *SRegionDNS) initK8s() { r.K8sManager.Start() } -func (r *SRegionDNS) getAdminSession() *mcclient.ClientSession { - return auth.GetAdminSession(r.Region, "") +func (r *SRegionDNS) getAdminSession(ctx context.Context) *mcclient.ClientSession { + return auth.GetAdminSession(ctx, r.Region, "") } func (r *SRegionDNS) initAuth() { diff --git a/pkg/lbagent/api.go b/pkg/lbagent/api.go index 0a93eae015..38866f24a2 100644 --- a/pkg/lbagent/api.go +++ b/pkg/lbagent/api.go @@ -65,7 +65,7 @@ func (h *ApiHelper) Run(ctx context.Context) { func (h *ApiHelper) adminClientSession(ctx context.Context) *mcclient.ClientSession { region := h.opts.CommonOpts.Region apiVersion := "v2" - s := auth.GetAdminSession(region, apiVersion) + s := auth.GetAdminSession(ctx, region, apiVersion) return s } diff --git a/pkg/mcclient/auth/auth.go b/pkg/mcclient/auth/auth.go index 2fada83d1d..c66a06de51 100644 --- a/pkg/mcclient/auth/auth.go +++ b/pkg/mcclient/auth/auth.go @@ -1,6 +1,7 @@ package auth import ( + "context" "fmt" "time" @@ -221,12 +222,12 @@ func AdminCredential() mcclient.TokenCredential { return manager.adminCredential } -func AdminSession(region, zone, endpointType, apiVersion string) *mcclient.ClientSession { +func AdminSession(ctx context.Context, region, zone, endpointType, apiVersion string) *mcclient.ClientSession { cli := Client() if cli == nil { return nil } - return cli.NewSession(region, zone, endpointType, AdminCredential(), apiVersion) + return cli.NewSession(ctx, region, zone, endpointType, AdminCredential(), apiVersion) } type AuthCompletedCallback func() @@ -262,12 +263,13 @@ func Init(info *AuthInfo, debug, insecure bool, certFile, keyFile string) { <-done } -func GetAdminSession(region string, apiVersion string) *mcclient.ClientSession { - return GetSession(manager.adminCredential, region, apiVersion) +func GetAdminSession(ctx context.Context, region string, + apiVersion string) *mcclient.ClientSession { + return GetSession(ctx, manager.adminCredential, region, apiVersion) } -func GetSession(token mcclient.TokenCredential, region string, apiVersion string) *mcclient.ClientSession { - return manager.client.NewSession(region, "", "internal", token, apiVersion) +func GetSession(ctx context.Context, token mcclient.TokenCredential, region string, apiVersion string) *mcclient.ClientSession { + return manager.client.NewSession(ctx, region, "", "internal", token, apiVersion) } // use for climc test only diff --git a/pkg/mcclient/mcclient.go b/pkg/mcclient/mcclient.go index bff5176829..be1de68169 100644 --- a/pkg/mcclient/mcclient.go +++ b/pkg/mcclient/mcclient.go @@ -93,12 +93,11 @@ func joinUrl(baseUrl, path string) string { return fmt.Sprintf("%s%s", baseUrl, path) } -func (this *Client) rawRequest(endpoint string, token string, method string, url string, header http.Header, body io.Reader) (*http.Response, error) { - ctx := context.Background() +func (this *Client) rawRequest(ctx context.Context, endpoint string, token string, method string, url string, header http.Header, body io.Reader) (*http.Response, error) { return httputils.Request(this.httpconn, ctx, method, joinUrl(endpoint, url), getDefaultHeader(header, token), body, this.debug) } -func (this *Client) jsonRequest(endpoint string, token string, method string, url string, header http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) { +func (this *Client) jsonRequest(ctx context.Context, endpoint string, token string, method string, url string, header http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) { /*bodystr := "" if body != nil { bodystr = body.String() @@ -110,7 +109,6 @@ func (this *Client) jsonRequest(endpoint string, token string, method string, ur header.Add("Content-Type", "application/json") resp, err := this.rawRequest(endpoint, token, method, url, header, jbody) return this.parseJSONResponse(resp, err)*/ - ctx := context.Background() return httputils.JSONRequest(this.httpconn, ctx, method, joinUrl(endpoint, url), getDefaultHeader(header, token), body, this.debug) } @@ -214,7 +212,7 @@ func (this *Client) _authV3(domainName, uname, passwd, projectId, projectName, t body.Add(jsonutils.NewString("default"), "auth", "scope", "project", "domain", "id") body.Add(jsonutils.NewString(projectName), "auth", "scope", "project", "name") } - hdr, rbody, err := this.jsonRequest(this.authUrl, "", "POST", "/auth/tokens", nil, body) + hdr, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "POST", "/auth/tokens", nil, body) if err != nil { return nil, err } @@ -241,7 +239,7 @@ func (this *Client) _authV2(uname, passwd, tenantId, tenantName, token string) ( if len(token) > 0 { body.Add(jsonutils.NewString(token), "auth", "token", "id") } - _, rbody, err := this.jsonRequest(this.authUrl, "", "POST", "/tokens", nil, body) + _, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "POST", "/tokens", nil, body) if err != nil { return nil, err } @@ -292,7 +290,7 @@ func (this *Client) verifyV3(adminToken, token string) (TokenCredential, error) header := http.Header{} header.Add("X-Auth-Token", adminToken) header.Add("X-Subject-Token", token) - _, rbody, err := this.jsonRequest(this.authUrl, "", "GET", "/auth/tokens", header, nil) + _, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "GET", "/auth/tokens", header, nil) if err != nil { return nil, err } @@ -303,7 +301,7 @@ func (this *Client) verifyV2(adminToken, token string) (TokenCredential, error) header := http.Header{} header.Add("X-Auth-Token", adminToken) verifyUrl := fmt.Sprintf("/tokens/%s", token) - _, rbody, err := this.jsonRequest(this.authUrl, "", "GET", verifyUrl, header, nil) + _, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "GET", verifyUrl, header, nil) if err != nil { return nil, err } @@ -329,7 +327,7 @@ func (this *Client) SetProject(tenantId, tenantName string, token TokenCredentia } } -func (this *Client) NewSession(region, zone, endpointType string, token TokenCredential, apiVersion string) *ClientSession { +func (this *Client) NewSession(ctx context.Context, region, zone, endpointType string, token TokenCredential, apiVersion string) *ClientSession { cata := token.GetServiceCatalog() if this.serviceCatalog == nil { if cata == nil { @@ -337,10 +335,19 @@ func (this *Client) NewSession(region, zone, endpointType string, token TokenCre } this.serviceCatalog = cata } - return &ClientSession{client: this, region: region, zone: zone, - endpointType: endpointType, token: token, + if ctx == nil { + ctx = context.Background() + } + return &ClientSession{ + ctx: ctx, + client: this, + region: region, + zone: zone, + endpointType: endpointType, + token: token, defaultApiVersion: apiVersion, - Header: http.Header{}} + Header: http.Header{}, + } } /* diff --git a/pkg/mcclient/session.go b/pkg/mcclient/session.go index cd7125036c..2fa87f6ae8 100644 --- a/pkg/mcclient/session.go +++ b/pkg/mcclient/session.go @@ -1,6 +1,7 @@ package mcclient import ( + "context" "fmt" "io" "io/ioutil" @@ -9,6 +10,7 @@ import ( "regexp" "strings" "time" + "yunion.io/x/jsonutils" "yunion.io/x/onecloud/pkg/util/httputils" "yunion.io/x/pkg/utils" @@ -38,6 +40,8 @@ func EnableApiVersionByModule() { } type ClientSession struct { + ctx context.Context + client *Client region string zone string @@ -166,7 +170,11 @@ func (this *ClientSession) RawVersionRequest( populateHeader(&tmpHeader, headers) } populateHeader(&tmpHeader, this.Header) - return this.client.rawRequest(baseurl, + ctx := this.ctx + if this.ctx == nil { + ctx = context.Background() + } + return this.client.rawRequest(ctx, baseurl, this.token.GetTokenString(), method, url, tmpHeader, body) } @@ -189,7 +197,11 @@ func (this *ClientSession) JSONVersionRequest( populateHeader(&tmpHeader, headers) } populateHeader(&tmpHeader, this.Header) - return this.client.jsonRequest(baseUrl, + ctx := this.ctx + if this.ctx == nil { + ctx = context.Background() + } + return this.client.jsonRequest(ctx, baseUrl, this.token.GetTokenString(), method, url, tmpHeader, body) } diff --git a/pkg/util/aliyun/storagecache.go b/pkg/util/aliyun/storagecache.go index 49dbe73a3c..06ef8d09d7 100644 --- a/pkg/util/aliyun/storagecache.go +++ b/pkg/util/aliyun/storagecache.go @@ -1,6 +1,7 @@ package aliyun import ( + "context" "fmt" "io/ioutil" "os" @@ -104,7 +105,7 @@ func (self *SStoragecache) GetPath() string { return "" } -func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) { if len(extId) > 0 { log.Debugf("UploadImage: Image external ID exists %s", extId) @@ -120,12 +121,12 @@ func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageI log.Debugf("UploadImage: no external ID") } - return self.uploadImage(userCred, imageId, osArch, osType, osDist, isForce) + return self.uploadImage(ctx, userCred, imageId, osArch, osType, osDist, isForce) } -func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool) (string, error) { // first upload image to oss - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(ctx, options.Options.Region, "") meta, reader, err := modules.Images.Download(s, imageId) if err != nil { @@ -342,7 +343,7 @@ func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imag } else if err := bucket.DownloadFile(imageList.Objects[0].Key, tmpImageFile.Name(), 12*1024*1024, oss.Routines(3), oss.Progress(&OssProgressListener{})); err != nil { return nil, err } else { - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(context.Background(), options.Options.Region, "") params := jsonutils.Marshal(map[string]string{"image_id": imageId, "disk-format": "raw"}) if file, err := os.Open(tmpImageFile.Name()); err != nil { return nil, err diff --git a/pkg/util/aws/storagecache.go b/pkg/util/aws/storagecache.go index 3223a1d9be..4e0840a293 100644 --- a/pkg/util/aws/storagecache.go +++ b/pkg/util/aws/storagecache.go @@ -1,6 +1,7 @@ package aws import ( + "context" "fmt" "strings" "time" @@ -109,7 +110,7 @@ func (self *SStoragecache) DownloadImage(userCred mcclient.TokenCredential, imag return self.downloadImage(userCred, imageId, extId) } -func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) { if len(extId) > 0 { log.Debugf("UploadImage: Image external ID exists %s", extId) @@ -124,7 +125,7 @@ func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageI log.Debugf("UploadImage: no external ID") } - return self.uploadImage(userCred, imageId, osArch, osType, osDist, isForce) + return self.uploadImage(ctx, userCred, imageId, osArch, osType, osDist, isForce) } @@ -148,7 +149,7 @@ func (self *SStoragecache) fetchImages() error { return nil } -func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool) (string, error) { bucketName := GetBucketName(self.region.GetId(), imageId) err := self.region.initVmimport(bucketName) if err != nil { @@ -161,10 +162,10 @@ func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageI return "", err } - defer s3client.DeleteBucket(&s3.DeleteBucketInput{Bucket: &bucketName}) // remove bucket + defer s3client.DeleteBucket(&s3.DeleteBucketInput{Bucket: &bucketName}) // remove bucket var diskFormat string - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(ctx, options.Options.Region, "") _, err = s3client.GetObject(&s3.GetObjectInput{Bucket: &bucketName, Key: &imageId}) if err != nil { // first upload image to oss @@ -196,7 +197,7 @@ func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageI if err != nil { return "", err } - defer s3client.DeleteObject(&s3.DeleteObjectInput{Bucket: &bucketName, Key: &imageId}) // remove object + defer s3client.DeleteObject(&s3.DeleteObjectInput{Bucket: &bucketName, Key: &imageId}) // remove object } else { meta, _, err := modules.Images.Download(s, imageId) if err != nil { @@ -300,7 +301,7 @@ func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imag return nil, err } - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(context.Background(), options.Options.Region, "") params := jsonutils.Marshal(map[string]string{"image_id": imageId, "disk-format": "raw"}) if result, err := modules.Images.Upload(s, params, ret.Body, IntVal(ret.ContentLength)); err != nil { return nil, err diff --git a/pkg/util/azure/storagecache.go b/pkg/util/azure/storagecache.go index ecd854e364..c625e56fba 100644 --- a/pkg/util/azure/storagecache.go +++ b/pkg/util/azure/storagecache.go @@ -1,6 +1,7 @@ package azure import ( + "context" "fmt" "io" "io/ioutil" @@ -98,7 +99,7 @@ func (self *SStoragecache) GetPath() string { return "" } -func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) { if len(extId) > 0 { log.Debugf("UploadImage: Image external ID exists %s", extId) status, err := self.region.GetImageStatus(extId) @@ -111,7 +112,7 @@ func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageI } else { log.Debugf("UploadImage: no external ID") } - return self.uploadImage(userCred, imageId, osArch, osType, osDist, isForce, options.Options.TempPath) + return self.uploadImage(ctx, userCred, imageId, osArch, osType, osDist, isForce, options.Options.TempPath) } func (self *SStoragecache) checkStorageAccount() (*SStorageAccount, error) { @@ -146,8 +147,8 @@ func (self *SStoragecache) checkStorageAccount() (*SStorageAccount, error) { return storageaccount, nil } -func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool, tmpPath string) (string, error) { - s := auth.GetAdminSession(options.Options.Region, "") +func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist string, isForce bool, tmpPath string) (string, error) { + s := auth.GetAdminSession(ctx, options.Options.Region, "") meta, reader, err := modules.Images.Download(s, imageId) if err != nil { return "", err @@ -278,7 +279,7 @@ func (self *SStoragecache) downloadImage(userCred mcclient.TokenCredential, imag log.Debugf("download complate") } - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(context.Background(), options.Options.Region, "") params := jsonutils.Marshal(map[string]string{"image_id": imageId, "disk-format": "raw"}) if file, err := os.Open(tmpImageFile.Name()); err != nil { return nil, err diff --git a/pkg/util/esxi/storagecache.go b/pkg/util/esxi/storagecache.go index 6a9a304892..89d85334ad 100644 --- a/pkg/util/esxi/storagecache.go +++ b/pkg/util/esxi/storagecache.go @@ -127,6 +127,8 @@ func (self *SDatastoreImageCache) DownloadImage(userCred mcclient.TokenCredentia return nil, cloudprovider.ErrNotImplemented } -func (self *SDatastoreImageCache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) { +func (self *SDatastoreImageCache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, + imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool, +) (string, error) { return "", cloudprovider.ErrNotImplemented } diff --git a/pkg/util/k8s/cluster.go b/pkg/util/k8s/cluster.go index b6d6828680..3227efbcbc 100644 --- a/pkg/util/k8s/cluster.go +++ b/pkg/util/k8s/cluster.go @@ -1,6 +1,7 @@ package k8s import ( + "context" "sync" "time" @@ -92,7 +93,7 @@ func (man *SKubeClusterManager) refreshKubeConfig() { } func (man *SKubeClusterManager) getKubeClusterConfig() (string, error) { - session := auth.GetAdminSession(man.region, "v1") + session := auth.GetAdminSession(context.Background(), man.region, "v1") params := jsonutils.NewDict() params.Add(jsonutils.JSONTrue, "directly") ret, err := kubeserver.Clusters.PerformAction(session, "default", "generate-kubeconfig", params) diff --git a/pkg/util/logclient/logclient.go b/pkg/util/logclient/logclient.go index fd675fcfd1..52b9e19d99 100644 --- a/pkg/util/logclient/logclient.go +++ b/pkg/util/logclient/logclient.go @@ -1,6 +1,8 @@ package logclient import ( + "context" + "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/util/stringutils" @@ -135,7 +137,7 @@ func addLog(model IObject, action string, iNotes interface{}, userCred mcclient. logentry.Add(jsonutils.NewString(notes), "notes") logclientWorkerMan.Run(func() { - s := auth.GetSession(userCred, "", "") + s := auth.GetSession(context.Background(), userCred, "", "") _, err := api.Create(s, logentry) if err != nil { log.Errorf("create action log failed %s", err) diff --git a/pkg/util/qcloud/storagecache.go b/pkg/util/qcloud/storagecache.go index b70d6b5a57..efc522e6ca 100644 --- a/pkg/util/qcloud/storagecache.go +++ b/pkg/util/qcloud/storagecache.go @@ -126,7 +126,7 @@ func (self *SStoragecache) GetPath() string { return "" } -func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) { +func (self *SStoragecache) UploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, extId string, isForce bool) (string, error) { if len(extId) > 0 { log.Debugf("UploadImage: Image external ID exists %s", extId) @@ -140,7 +140,7 @@ func (self *SStoragecache) UploadImage(userCred mcclient.TokenCredential, imageI } else { log.Debugf("UploadImage: no external ID") } - return self.uploadImage(userCred, imageId, osArch, osType, osDist, osVersion, isForce) + return self.uploadImage(ctx, userCred, imageId, osArch, osType, osDist, osVersion, isForce) } func (self *SRegion) getCosUrl(bucket, object string) string { @@ -148,9 +148,9 @@ func (self *SRegion) getCosUrl(bucket, object string) string { return fmt.Sprintf("http://%s-%s.cos.%s.myqcloud.com/%s", bucket, self.client.AppID, self.Region, object) } -func (self *SStoragecache) uploadImage(userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, isForce bool) (string, error) { +func (self *SStoragecache) uploadImage(ctx context.Context, userCred mcclient.TokenCredential, imageId string, osArch, osType, osDist, osVersion string, isForce bool) (string, error) { // first upload image to oss - s := auth.GetAdminSession(options.Options.Region, "") + s := auth.GetAdminSession(ctx, options.Options.Region, "") meta, reader, err := modules.Images.Download(s, imageId) if err != nil { diff --git a/pkg/webconsole/command/ssh_command.go b/pkg/webconsole/command/ssh_command.go index 3bc166b313..dfe590e3f0 100644 --- a/pkg/webconsole/command/ssh_command.go +++ b/pkg/webconsole/command/ssh_command.go @@ -30,7 +30,7 @@ type SSHtoolSol struct { func getCommand(ctx context.Context, userCred mcclient.TokenCredential, ip string) (string, *BaseCommand, error) { cmd := NewBaseCommand(o.Options.SshToolPath) - s := auth.GetAdminSession(o.Options.Region, "v2") + s := auth.GetAdminSession(ctx, o.Options.Region, "v2") key, err := modules.Sshkeypairs.GetById(s, userCred.GetProjectId(), jsonutils.Marshal(map[string]bool{"admin": true})) if err != nil { return "", nil, err diff --git a/pkg/webconsole/handlers.go b/pkg/webconsole/handlers.go index 6d78769dd9..9a23f652d5 100644 --- a/pkg/webconsole/handlers.go +++ b/pkg/webconsole/handlers.go @@ -54,7 +54,7 @@ func fetchK8sEnv(ctx context.Context, w http.ResponseWriter, r *http.Request) (* } podName := params[""] container, _ := body.GetString("container") - adminSession := auth.GetAdminSession(o.Options.Region, "") + adminSession := auth.GetAdminSession(ctx, o.Options.Region, "") query := jsonutils.NewDict() query.Add(jsonutils.NewString(namespace), "namespace") @@ -107,7 +107,7 @@ func fetchCloudEnv(ctx context.Context, w http.ResponseWriter, r *http.Request) if userCred == nil { return nil, httperrors.NewUnauthorizedError("No token founded") } - s := auth.Client().NewSession(o.Options.Region, "", "internal", userCred, "v2") + s := auth.Client().NewSession(ctx, o.Options.Region, "", "internal", userCred, "v2") return &CloudEnv{ ClientSessin: s, Params: params,