diff --git a/bcs-services/bcs-cluster-manager/go.mod b/bcs-services/bcs-cluster-manager/go.mod index ee5b543ff5..1fe63317ac 100644 --- a/bcs-services/bcs-cluster-manager/go.mod +++ b/bcs-services/bcs-cluster-manager/go.mod @@ -3,7 +3,7 @@ module github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager go 1.20 replace ( - github.com/Tencent/bk-bcs/bcs-common => ../../bcs-common + github.com/Tencent/bk-bcs/bcs-common => github.com/LidolLxf/bk-bcs/bcs-common v0.0.0-20250115101422-10b01a987a53 k8s.io/api => k8s.io/api v0.26.1 k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.26.1 k8s.io/apimachinery => k8s.io/apimachinery v0.26.1 diff --git a/bcs-services/bcs-cluster-manager/internal/actions/cluster/delete_vcluster.go b/bcs-services/bcs-cluster-manager/internal/actions/cluster/delete_vcluster.go index 6ee5ae2469..bc80b116b8 100644 --- a/bcs-services/bcs-cluster-manager/internal/actions/cluster/delete_vcluster.go +++ b/bcs-services/bcs-cluster-manager/internal/actions/cluster/delete_vcluster.go @@ -99,7 +99,7 @@ func (da *DeleteVirtualAction) getClusterInfo() error { func (da *DeleteVirtualAction) cleanLocalInformation() error { // async delete cluster dependency info - go asyncDeleteImportedClusterInfo(context.Background(), da.model, da.cluster) + go asyncDeleteImportedClusterInfo(da.ctx, da.model, da.cluster) // finally clean cluster da.cluster.Status = common.StatusDeleted diff --git a/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/create.go b/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/create.go index de8a980bbc..9ec6d633d7 100644 --- a/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/create.go +++ b/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/create.go @@ -177,7 +177,7 @@ func (ca *CreateAction) validate() error { return err } - proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(ca.req.ProjectID, true) + proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(ca.ctx, ca.req.ProjectID, true) if errLocal == nil { ca.project = proInfo } diff --git a/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/delete.go b/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/delete.go index 641af7d4aa..09e75c0d0a 100644 --- a/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/delete.go +++ b/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/delete.go @@ -63,7 +63,7 @@ func (da *DeleteAction) validate() error { da.template = template - proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(da.req.ProjectID, true) + proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(da.ctx, da.req.ProjectID, true) if errLocal == nil { da.project = proInfo } diff --git a/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/update.go b/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/update.go index 96e6889388..240b4c3b62 100644 --- a/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/update.go +++ b/bcs-services/bcs-cluster-manager/internal/actions/nodetemplate/update.go @@ -157,7 +157,7 @@ func (ua *UpdateAction) validate() error { return err } - proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(ua.req.ProjectID, true) + proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(ua.ctx, ua.req.ProjectID, true) if errLocal == nil { ua.project = proInfo } diff --git a/bcs-services/bcs-cluster-manager/internal/actions/notifytemplate/create.go b/bcs-services/bcs-cluster-manager/internal/actions/notifytemplate/create.go index 17173264b6..867847414a 100644 --- a/bcs-services/bcs-cluster-manager/internal/actions/notifytemplate/create.go +++ b/bcs-services/bcs-cluster-manager/internal/actions/notifytemplate/create.go @@ -134,7 +134,7 @@ func (ca *CreateAction) validate() error { return err } - proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(ca.req.ProjectID, true) + proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(ca.ctx, ca.req.ProjectID, true) if errLocal == nil { ca.project = proInfo } diff --git a/bcs-services/bcs-cluster-manager/internal/actions/notifytemplate/delete.go b/bcs-services/bcs-cluster-manager/internal/actions/notifytemplate/delete.go index 709f2bb655..d338d977f8 100644 --- a/bcs-services/bcs-cluster-manager/internal/actions/notifytemplate/delete.go +++ b/bcs-services/bcs-cluster-manager/internal/actions/notifytemplate/delete.go @@ -64,7 +64,7 @@ func (da *DeleteAction) validate() error { da.template = template - proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(da.req.ProjectID, true) + proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(da.ctx, da.req.ProjectID, true) if errLocal == nil { da.project = proInfo } diff --git a/bcs-services/bcs-cluster-manager/internal/actions/thirdparty/project_quota.go b/bcs-services/bcs-cluster-manager/internal/actions/thirdparty/project_quota.go index cc66e3abed..4fc88e8780 100644 --- a/bcs-services/bcs-cluster-manager/internal/actions/thirdparty/project_quota.go +++ b/bcs-services/bcs-cluster-manager/internal/actions/thirdparty/project_quota.go @@ -59,7 +59,7 @@ func (ga *GetProjectResourceQuotaUsageAction) validate() error { } // check projectId or Code - proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(ga.req.GetProjectID(), false) + proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(ga.ctx, ga.req.GetProjectID(), false) if errLocal != nil { return errLocal } diff --git a/bcs-services/bcs-cluster-manager/internal/actions/thirdparty/resource.go b/bcs-services/bcs-cluster-manager/internal/actions/thirdparty/resource.go index 4d0aead558..6ade529dd4 100644 --- a/bcs-services/bcs-cluster-manager/internal/actions/thirdparty/resource.go +++ b/bcs-services/bcs-cluster-manager/internal/actions/thirdparty/resource.go @@ -157,7 +157,7 @@ func (ga *GetProviderResourceUsageAction) getBizInfoByPools() error { continue } - proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(cls.ProjectID, true) + proInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(ga.ctx, cls.ProjectID, true) if errLocal != nil { blog.Errorf("getProviderDevicePools GetProjectInfo[%s] failed: %v", group.ClusterID, errLocal) diff --git a/bcs-services/bcs-cluster-manager/internal/app/app.go b/bcs-services/bcs-cluster-manager/internal/app/app.go index 95e4569b4b..46bcb33d71 100644 --- a/bcs-services/bcs-cluster-manager/internal/app/app.go +++ b/bcs-services/bcs-cluster-manager/internal/app/app.go @@ -40,10 +40,13 @@ import ( commonutil "github.com/Tencent/bk-bcs/bcs-common/common/util" "github.com/Tencent/bk-bcs/bcs-common/common/version" "github.com/Tencent/bk-bcs/bcs-common/pkg/auth/iam" + "github.com/Tencent/bk-bcs/bcs-common/pkg/bcsapi/bcsproject" + "github.com/Tencent/bk-bcs/bcs-common/pkg/bcsapi/clustermanager" + "github.com/Tencent/bk-bcs/bcs-common/pkg/discovery" + "github.com/Tencent/bk-bcs/bcs-common/pkg/header" "github.com/Tencent/bk-bcs/bcs-common/pkg/i18n" "github.com/Tencent/bk-bcs/bcs-common/pkg/odm/drivers" "github.com/Tencent/bk-bcs/bcs-common/pkg/odm/drivers/mongo" - "github.com/Tencent/bk-bcs/bcs-common/pkg/otel/trace/constants" "github.com/Tencent/bk-bcs/bcs-common/pkg/otel/trace/micro" "github.com/Tencent/bk-bcs/bcs-services/pkg/bcs-auth/middleware" restful "github.com/emicklei/go-restful" @@ -66,7 +69,6 @@ import ( cmcommon "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/common" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/commonhandler" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/daemon" - "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/discovery" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/handler" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/lock" etcdlock "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/lock/etcd" @@ -79,7 +81,6 @@ import ( "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/cmdb" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/encrypt" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/gse" - "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/install/addons" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/install/helm" installTypes "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/install/types" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/job" @@ -412,18 +413,6 @@ func (cm *ClusterManager) initRemoteClient() error { // nolint if err != nil { return err } - // init addons client - err = addons.SetAddonsClient(&installTypes.Options{ - Enable: cm.opt.Helm.Enable, - GateWay: cm.opt.Helm.GateWay, - Token: cm.opt.Helm.Token, - Module: cm.opt.Helm.Module, - EtcdRegistry: cm.microRegistry, - ClientTLSConfig: cm.clientTLSConfig, - }) - if err != nil { - return err - } // init encrypt client err = encrypt.SetEncryptClient(cm.opt.Encrypt) @@ -708,7 +697,12 @@ func (cm *ClusterManager) initRegistry() error { // initDiscovery discovery client func (cm *ClusterManager) initDiscovery() { - cm.disc = discovery.NewModuleDiscovery(cmcommon.ClusterManagerServiceDomain, cm.microRegistry) + if !discovery.UseServiceDiscovery() { + cm.disc = discovery.NewModuleDiscovery(cmcommon.ClusterManagerServiceDomain, cm.microRegistry) + clustermanager.SetClientConfig(cm.tlsConfig, cm.disc) + } else { + clustermanager.SetClientConfig(cm.tlsConfig, nil) + } blog.Infof("init discovery for cluster manager successfully") // enable discovery resource module @@ -725,13 +719,17 @@ func (cm *ClusterManager) initDiscovery() { // enable discovery project module if cm.opt.ProjectManager.Enable { - cm.projectDisc = discovery.NewModuleDiscovery(cm.opt.ProjectManager.Module, cm.microRegistry) - blog.Infof("init discovery for project manager successfully") - project.SetProjectClient(&project.Options{ Module: cm.opt.ProjectManager.Module, TLSConfig: cm.clientTLSConfig, - }, cm.projectDisc) + }) + if !discovery.UseServiceDiscovery() { + cm.projectDisc = discovery.NewModuleDiscovery(cm.opt.ProjectManager.Module, cm.microRegistry) + blog.Infof("init discovery for project manager successfully") + bcsproject.SetClientConfig(cm.clientTLSConfig, cm.projectDisc) + } else { + bcsproject.SetClientConfig(cm.clientTLSConfig, nil) + } } } @@ -802,26 +800,10 @@ func (cm *ClusterManager) initTunnelServer(router *mux.Router) error { return nil } -// CustomMatcher for http header -func CustomMatcher(key string) (string, bool) { - switch key { - case "X-Request-Id": - return "X-Request-Id", true - case middleware.CustomUsernameHeaderKey: - return middleware.CustomUsernameHeaderKey, true - case middleware.InnerClientHeaderKey: - return middleware.InnerClientHeaderKey, true - case constants.Traceparent: - return constants.GrpcTraceparent, true - default: - return runtime.DefaultHeaderMatcher(key) - } -} - // init http grpc gateway func (cm *ClusterManager) initHTTPGateway(router *mux.Router) error { gwmux := runtime.NewServeMux( - runtime.WithIncomingHeaderMatcher(CustomMatcher), + runtime.WithIncomingHeaderMatcher(header.CustomHeaderMatcher), runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{ OrigName: true, EmitDefaults: true, @@ -1104,7 +1086,6 @@ func (cm *ClusterManager) close() { closeCtx, closeCancel := context.WithTimeout(context.Background(), 10*time.Second) defer closeCancel() helm.GetHelmManagerClient().Stop() - addons.GetAddonsClient().Stop() cm.extraServer.Shutdown(closeCtx) // nolint cm.httpServer.Shutdown(closeCtx) // nolint cm.daemon.Stop() diff --git a/bcs-services/bcs-cluster-manager/internal/cloudprovider/common/component.go b/bcs-services/bcs-cluster-manager/internal/cloudprovider/common/component.go index e178131187..a9aa358f30 100644 --- a/bcs-services/bcs-cluster-manager/internal/cloudprovider/common/component.go +++ b/bcs-services/bcs-cluster-manager/internal/cloudprovider/common/component.go @@ -146,7 +146,7 @@ func InstallWatchComponentByHelm(ctx context.Context, projectID, blog.Errorf("InstallWatchComponentByHelm[%s] GetWatchInstaller failed: %v", taskID, err) return err } - err = installer.Install(clusterID, values) + err = installer.Install(ctx, clusterID, values) if err != nil { blog.Errorf("InstallWatchComponentByHelm[%s] Install failed: %v", taskID, err) return err @@ -166,7 +166,7 @@ func DeleteWatchComponentByHelm(ctx context.Context, projectID, blog.Errorf("DeleteWatchComponentByHelm[%s] GetWatchInstaller failed: %v", traceID, err) return err } - err = install.Uninstall(clusterID) + err = install.Uninstall(ctx, clusterID) if err != nil { blog.Errorf("DeleteWatchComponentByHelm[%s] Uninstall failed: %v", traceID, err) return err @@ -178,7 +178,7 @@ func DeleteWatchComponentByHelm(ctx context.Context, projectID, err = loop.LoopDoFunc(timeContext, func() error { var exist bool - exist, err = install.IsInstalled(clusterID) + exist, err = install.IsInstalled(ctx, clusterID) if err != nil { blog.Errorf("DeleteWatchComponentByHelm[%s] failed[%s:%s]: %v", traceID, projectID, clusterID, err) return nil @@ -260,7 +260,7 @@ func InstallImagePullSecretByAddon(ctx context.Context, projectID, clusterID str blog.Errorf("InstallImagePullSecretByAddon[%s] GetAddonInstaller failed: %v", taskID, err) return err } - err = installer.Install(clusterID, "") + err = installer.Install(ctx, clusterID, "") if err != nil { blog.Errorf("InstallImagePullSecretByAddon[%s] Install failed: %v", taskID, err) return err @@ -280,7 +280,7 @@ func DeleteImagePullSecretByAddon(ctx context.Context, projectID, blog.Errorf("DeleteImagePullSecretByAddon[%s] GetAddonInstaller failed: %v", traceID, err) return err } - err = install.Uninstall(clusterID) + err = install.Uninstall(ctx, clusterID) if err != nil { blog.Errorf("DeleteImagePullSecretByAddon[%s] Uninstall failed: %v", traceID, err) return err @@ -474,7 +474,7 @@ func ensureVclusterWithInstaller(ctx context.Context, info *VclusterInfo) error blog.Errorf("ensureVclusterWithInstaller[%s] GetVclusterInstaller failed: %v", taskID, err) return err } - installed, err := installer.IsInstalled(info.SrcClusterID) + installed, err := installer.IsInstalled(ctx, info.SrcClusterID) if err != nil { blog.Errorf("ensureVclusterWithInstaller[%s] IsInstalled failed: %v", taskID, err) return err @@ -498,17 +498,17 @@ func ensureVclusterWithInstaller(ctx context.Context, info *VclusterInfo) error // install or upgrade if installed { - if errUpgrade := installer.Upgrade(info.SrcClusterID, values); errUpgrade != nil { + if errUpgrade := installer.Upgrade(ctx, info.SrcClusterID, values); errUpgrade != nil { return fmt.Errorf("upgrade app failed, err %s", errUpgrade) } } else { - if errInstall := installer.Install(info.SrcClusterID, values); errInstall != nil { + if errInstall := installer.Install(ctx, info.SrcClusterID, values); errInstall != nil { return fmt.Errorf("install app failed, err %s", errInstall) } } // check status - ok, err := installer.CheckAppStatus(info.SrcClusterID, time.Minute*10, false) + ok, err := installer.CheckAppStatus(ctx, info.SrcClusterID, time.Minute*10, false) if err != nil { return fmt.Errorf("check app status failed, err %s", err) } @@ -596,7 +596,7 @@ func DeleteVclusterComponentByHelm(ctx context.Context, info *VclusterInfo) erro blog.Errorf("DeleteVclusterComponentByHelm[%s] GetVclusterInstaller failed: %v", taskID, err) return err } - err = install.Uninstall(info.SrcClusterID) + err = install.Uninstall(ctx, info.SrcClusterID) if err != nil { blog.Errorf("DeleteVclusterComponentByHelm[%s] Uninstall failed: %v", taskID, err) return err @@ -607,7 +607,7 @@ func DeleteVclusterComponentByHelm(ctx context.Context, info *VclusterInfo) erro defer cancel() err = loop.LoopDoFunc(timeContext, func() error { - exist, errInstall := install.IsInstalled(info.SrcClusterID) + exist, errInstall := install.IsInstalled(ctx, info.SrcClusterID) if errInstall != nil { blog.Errorf("DeleteVclusterComponentByHelm[%s] failed[%s:%s]: %v", taskID, info.ProjectID, info.SrcClusterID, errInstall) @@ -749,7 +749,7 @@ func ensureAutoScalerWithInstaller(ctx context.Context, nodeGroups []proto.NodeG blog.Errorf("ensureAutoScalerWithInstaller[%s] CreateClusterNamespace failed: %v", taskID, err) } - installed, err := installer.IsInstalled(as.ClusterID) + installed, err := installer.IsInstalled(ctx, as.ClusterID) if err != nil { blog.Errorf("ensureAutoScalerWithInstaller IsInstalled failed: %v", err) return err @@ -776,14 +776,14 @@ func ensureAutoScalerWithInstaller(ctx context.Context, nodeGroups []proto.NodeG } // install or upgrade if installed { - if err = installer.Upgrade(as.ClusterID, values); err != nil { + if err = installer.Upgrade(ctx, as.ClusterID, values); err != nil { return fmt.Errorf("upgrade app failed, err %s", err) } cloudprovider.GetStorageModel().CreateTaskStepLogInfo(context.Background(), taskID, stepName, "upgrade app successful") } else { - if err = installer.Install(as.ClusterID, values); err != nil { + if err = installer.Install(ctx, as.ClusterID, values); err != nil { return fmt.Errorf("install app failed, err %s", err) } @@ -793,7 +793,7 @@ func ensureAutoScalerWithInstaller(ctx context.Context, nodeGroups []proto.NodeG // check status var ok bool - ok, err = installer.CheckAppStatus(as.ClusterID, time.Minute*10, false) + ok, err = installer.CheckAppStatus(ctx, as.ClusterID, time.Minute*10, false) if err != nil { return fmt.Errorf("check app status failed, err %s", err) } @@ -814,11 +814,11 @@ func ensureAutoScalerWithInstaller(ctx context.Context, nodeGroups []proto.NodeG return fmt.Errorf("transAutoScalingOptionToValues failed, err: %s", err) } - if err = installer.Upgrade(as.ClusterID, values); err != nil { + if err = installer.Upgrade(ctx, as.ClusterID, values); err != nil { return fmt.Errorf("upgrade app failed, err %s", err) } // check status - ok, errCheck := installer.CheckAppStatus(as.ClusterID, time.Minute*10, false) + ok, errCheck := installer.CheckAppStatus(ctx, as.ClusterID, time.Minute*10, false) if errCheck != nil { return fmt.Errorf("check app status failed, err %s", err) } diff --git a/bcs-services/bcs-cluster-manager/internal/daemon/cluster.go b/bcs-services/bcs-cluster-manager/internal/daemon/cluster.go index 5bbafcae62..1123fb55c9 100644 --- a/bcs-services/bcs-cluster-manager/internal/daemon/cluster.go +++ b/bcs-services/bcs-cluster-manager/internal/daemon/cluster.go @@ -52,7 +52,7 @@ func (d *Daemon) reportClusterGroupNodeNum(error chan<- error) { } bizId := "" - pInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(group.ProjectID, true) + pInfo, errLocal := project.GetProjectManagerClient().GetProjectInfo(d.ctx, group.ProjectID, true) if errLocal == nil { bizId = pInfo.GetBusinessID() } diff --git a/bcs-services/bcs-cluster-manager/internal/discovery/discovery.go b/bcs-services/bcs-cluster-manager/internal/discovery/discovery.go deleted file mode 100644 index 5c12205e74..0000000000 --- a/bcs-services/bcs-cluster-manager/internal/discovery/discovery.go +++ /dev/null @@ -1,203 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Blueking Container Service available. - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * Licensed under the MIT License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * http://opensource.org/licenses/MIT - * Unless required by applicable law or agreed to in writing, software distributed under - * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Package discovery xxx -package discovery - -import ( - "errors" - "fmt" - "math/rand" - "sync" - "time" - - "github.com/Tencent/bk-bcs/bcs-common/common/blog" - "go-micro.dev/v4/registry" -) - -const ( - rewatchSecond = 1 -) - -// EventHandler discovery event handler interface -type EventHandler func(svcs []*registry.Service) - -// ModuleDiscovery discovery service endpoints -type ModuleDiscovery struct { - sync.RWMutex - module string - curServices []*registry.Service - microRegistry registry.Registry - handler EventHandler - stop chan bool -} - -// NewModuleDiscovery create discovery -func NewModuleDiscovery(module string, r registry.Registry) *ModuleDiscovery { - return &ModuleDiscovery{ - module: module, - microRegistry: r, - stop: make(chan bool), - } -} - -// Start start discovery -func (md *ModuleDiscovery) Start() error { - watcher, err := md.microRegistry.Watch(registry.WatchService(md.module)) - if err != nil { - return fmt.Errorf("failed to create registry watcher for module %s, err %s", md.module, err.Error()) - } - go func() { - var err error - for { - select { - case <-md.stop: - if watcher != nil { - watcher.Stop() - } - return - default: - if watcher == nil { - watcher, err = md.microRegistry.Watch(registry.WatchService(md.module)) - if err != nil { - blog.Warnf("see empty watcher and failed to create registry watcher for module %s, err %s", - md.module, err.Error()) - time.Sleep(rewatchSecond * time.Second) - continue - } - } - - if err := md.watchRegistry(watcher); err != nil { - blog.Warnf("failed when watching registry for module %s, err %s", md.module, err.Error()) - time.Sleep(rewatchSecond * time.Second) - } - - if watcher != nil { - watcher.Stop() - watcher = nil - } - } - } - }() - return nil -} - -func (md *ModuleDiscovery) watchRegistry(w registry.Watcher) error { - stop := make(chan bool) - defer func() { - close(stop) - }() - - go func() { - defer w.Stop() - select { - case <-stop: - return - case <-md.stop: - return - } - }() - - // receive event, to update service - svcs, err := md.microRegistry.GetService(md.module) - if err != nil { - blog.Warnf("failed to get service for module %s, err %s", md.module, err.Error()) - return err - } - md.Lock() - md.curServices = svcs - md.Unlock() - if md.handler != nil { - md.handler(svcs) - } - - for { - result, err := w.Next() - if err != nil { - if err != registry.ErrWatcherStopped { - return err - } - break - } - if result != nil && result.Service != nil { - blog.V(5).Infof("service watch result, action %s, service %s", result.Action, result.Service.Name) - } - // receive event, to update service - svcs, err := md.microRegistry.GetService(md.module) - if err != nil { - blog.Warnf("failed to get service for module %s, err %s", md.module, err.Error()) - continue - } - blog.V(5).Infof("get services %v", svcs) - - md.Lock() - md.curServices = svcs - md.Unlock() - if md.handler != nil { - md.handler(svcs) - } - } - return nil -} - -// GetService get service from remote -func (md *ModuleDiscovery) GetService() []*registry.Service { - md.RLock() - defer md.RUnlock() - return md.curServices -} - -// GetRandomServiceNode get random instance by curServices -func (md *ModuleDiscovery) GetRandomServiceNode() (*registry.Node, error) { - allServiceNodes := make([]*registry.Node, 0) - - if len(md.curServices) == 0 { - blog.Infof("discovery has no local service cache[%s]", md.module) - return nil, errors.New("curServices is empty") - } - - md.Lock() - defer md.Unlock() - for i := range md.curServices { - allServiceNodes = append(allServiceNodes, md.curServices[i].Nodes...) - } - - nodeLength := len(allServiceNodes) - if nodeLength == 0 { - blog.V(3).Infof("discovery found no node information of %s", md.module) - return nil, errors.New("allServiceNodes is empty") - } - - // NOCC:gas/crypto(设计如此) - selected := rand.Int() % nodeLength // nolint - return allServiceNodes[selected], nil -} - -// GetModuleName get module name -func (md *ModuleDiscovery) GetModuleName() string { - return md.module -} - -// RegisterEventHandler register event callback function -func (md *ModuleDiscovery) RegisterEventHandler(eHandler EventHandler) { - md.handler = eHandler -} - -// Stop stop discovery -func (md *ModuleDiscovery) Stop() { - select { - case <-md.stop: - return - default: - close(md.stop) - } -} diff --git a/bcs-services/bcs-cluster-manager/internal/remote/install/addons/addons.go b/bcs-services/bcs-cluster-manager/internal/remote/install/addons/addons.go index d46d7e45ce..457064585e 100644 --- a/bcs-services/bcs-cluster-manager/internal/remote/install/addons/addons.go +++ b/bcs-services/bcs-cluster-manager/internal/remote/install/addons/addons.go @@ -72,37 +72,38 @@ func NewAddonsInstaller(opts AddonOptions, client *AddonsClient, var _ install.Installer = &AddonsInstaller{} // IsInstalled returns whether the app is installed -func (a *AddonsInstaller) IsInstalled(clusterID string) (bool, error) { +func (a *AddonsInstaller) IsInstalled(ctx context.Context, clusterID string) (bool, error) { if a.debug { return true, nil } - resp, err := a.getAddonDetail(clusterID) + resp, err := a.getAddonDetail(ctx, clusterID) if err != nil { blog.Errorf("[AddonsInstaller] GetAddonsDetail failed, err: %s", err.Error()) return false, err } // not found addon - if resp.Code != 0 { + if *resp.Code != 0 { blog.Errorf("[AddonsInstaller] GetAddonsDetail failed, code: %d, message: %s", resp.Code, resp.Message) return false, nil } blog.Infof("[AddonsInstaller] [%s:%s] GetAddonsDetail success[%s:%s] status: %s", a.projectID, a.clusterID, resp.Data.Namespace, resp.Data.Name, resp.Data.Status) - if resp.Data.Status == "" { + if *resp.Data.Status == "" { return false, nil } return true, nil } -func (a *AddonsInstaller) getAddonDetail(clusterId string) (*helmmanager.GetAddonsDetailResp, error) { +func (a *AddonsInstaller) getAddonDetail( + ctx context.Context, clusterId string) (*helmmanager.GetAddonsDetailResp, error) { start := time.Now() - resp, err := a.client.GetAddonsDetail(context.Background(), &helmmanager.GetAddonsDetailReq{ - ProjectCode: a.projectID, - ClusterID: clusterId, - Name: a.addonName, + resp, err := a.client.GetAddonsDetail(ctx, &helmmanager.GetAddonsDetailReq{ + ProjectCode: &a.projectID, + ClusterID: &clusterId, + Name: &a.addonName, }) if err != nil { metrics.ReportLibRequestMetric("addons", "GetAddonsDetail", "grpc", metrics.LibCallStatusErr, start) @@ -120,21 +121,21 @@ func (a *AddonsInstaller) getAddonDetail(clusterId string) (*helmmanager.GetAddo } // Install installs the app -func (a *AddonsInstaller) Install(clusterID, values string) error { +func (a *AddonsInstaller) Install(ctx context.Context, clusterID, values string) error { if a.debug { return nil } - addonResp, err := a.getAddonDetail(clusterID) - if err != nil || addonResp.Code != 0 { + addonResp, err := a.getAddonDetail(ctx, clusterID) + if err != nil || *addonResp.Code != 0 { return fmt.Errorf("[AddonsInstaller] InstallAddons failed: %v", err) } start := time.Now() - resp, err := a.client.UpgradeAddons(context.Background(), &helmmanager.UpgradeAddonsReq{ - ProjectCode: a.projectID, - ClusterID: clusterID, - Name: a.addonName, + resp, err := a.client.UpgradeAddons(ctx, &helmmanager.UpgradeAddonsReq{ + ProjectCode: &a.projectID, + ClusterID: &clusterID, + Name: &a.addonName, Version: addonResp.Data.Version, }) if err != nil { @@ -149,9 +150,9 @@ func (a *AddonsInstaller) Install(clusterID, values string) error { return fmt.Errorf("InstallAddons failed, resp is empty") } - if resp.Code != 0 || !resp.Result { + if *resp.Code != 0 || !*resp.Result { blog.Errorf("[AddonsInstaller] InstallAddons failed, code: %d, message: %s", resp.Code, resp.Message) - return fmt.Errorf("InstallAddons failed, code: %d, message: %s", resp.Code, resp.Message) + return fmt.Errorf("InstallAddons failed, code: %d, message: %s", *resp.Code, *resp.Message) } blog.Errorf("[AddonsInstaller] InstallAddons[%s:%s] success[%s]", a.projectID, clusterID, a.addonName) @@ -160,22 +161,22 @@ func (a *AddonsInstaller) Install(clusterID, values string) error { } // Upgrade upgrades the app -func (a *AddonsInstaller) Upgrade(clusterID, values string) error { +func (a *AddonsInstaller) Upgrade(ctx context.Context, clusterID, values string) error { return nil } // Uninstall uninstalls the app -func (a *AddonsInstaller) Uninstall(clusterID string) error { +func (a *AddonsInstaller) Uninstall(ctx context.Context, clusterID string) error { if a.debug { return nil } start := time.Now() // delete addon - resp, err := a.client.UninstallAddons(context.Background(), &helmmanager.UninstallAddonsReq{ - ProjectCode: a.projectID, - ClusterID: clusterID, - Name: a.addonName, + resp, err := a.client.UninstallAddons(ctx, &helmmanager.UninstallAddonsReq{ + ProjectCode: &a.projectID, + ClusterID: &clusterID, + Name: &a.addonName, }) if err != nil { metrics.ReportLibRequestMetric("addons", "UninstallAddons", "grpc", metrics.LibCallStatusErr, start) @@ -184,10 +185,10 @@ func (a *AddonsInstaller) Uninstall(clusterID string) error { } metrics.ReportLibRequestMetric("addons", "UninstallAddons", "grpc", metrics.LibCallStatusOK, start) - if resp.Code != 0 { + if *resp.Code != 0 { blog.Errorf("[AddonsInstaller] UninstallAddons failed, code: %d, message: %s", resp.Code, resp.Message) - return fmt.Errorf("UninstallAddons failed, code: %d, message: %s, requestID: %s", resp.Code, - resp.Message, resp.RequestID) + return fmt.Errorf("UninstallAddons failed, code: %d, message: %s, requestID: %s", *resp.Code, + *resp.Message, *resp.RequestID) } blog.Infof("[AddonsInstaller] delete addon successful[%s:%s]", clusterID, a.addonName) @@ -195,7 +196,8 @@ func (a *AddonsInstaller) Uninstall(clusterID string) error { } // CheckAppStatus check app install status -func (a *AddonsInstaller) CheckAppStatus(clusterID string, timeout time.Duration, pre bool) (bool, error) { +func (a *AddonsInstaller) CheckAppStatus( + ctx context.Context, clusterID string, timeout time.Duration, pre bool) (bool, error) { if a.debug { return true, nil } diff --git a/bcs-services/bcs-cluster-manager/internal/remote/install/addons/client.go b/bcs-services/bcs-cluster-manager/internal/remote/install/addons/client.go index 52a123045f..bbc1b2cee4 100644 --- a/bcs-services/bcs-cluster-manager/internal/remote/install/addons/client.go +++ b/bcs-services/bcs-cluster-manager/internal/remote/install/addons/client.go @@ -14,32 +14,15 @@ package addons import ( - "context" - - "github.com/Tencent/bk-bcs/bcs-common/common/blog" - "github.com/Tencent/bk-bcs/bcs-common/pkg/bcsapi" "github.com/Tencent/bk-bcs/bcs-common/pkg/bcsapi/helmmanager" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/common" - "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/discovery" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/install/types" - "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/utils" ) // addonsClient addons client var addonsClient *AddonsClient -// SetAddonsClient set global addons client -func SetAddonsClient(opts *types.Options) error { - var err error - addonsClient, err = NewAddonsClient(opts) - if err != nil { - return err - } - - return nil -} - // GetAddonsClient get addon client func GetAddonsClient() *AddonsClient { return addonsClient @@ -47,34 +30,6 @@ func GetAddonsClient() *AddonsClient { // AddonsClient client for addons type AddonsClient struct { // nolint - opts *types.Options - discovery *discovery.ModuleDiscovery - ctx context.Context - cancel context.CancelFunc -} - -// NewAddonsClient init addon client and start discovery module -func NewAddonsClient(opts *types.Options) (*AddonsClient, error) { - ok := opts.Validate() - if !ok { - return nil, nil - } - - addonsLocalClient := &AddonsClient{ - opts: opts, - } - addonsLocalClient.ctx, addonsLocalClient.cancel = context.WithCancel(context.Background()) - - if len(opts.GateWay) == 0 { - addonsLocalClient.discovery = discovery.NewModuleDiscovery(opts.Module, opts.EtcdRegistry) - err := addonsLocalClient.discovery.Start() - if err != nil { - blog.Errorf("start discovery[%s] client failed: %v", types.ModuleHelmManager, err) - return nil, err - } - } - - return addonsLocalClient, nil } // GetAddonsClient get addons client @@ -83,36 +38,9 @@ func (ac *AddonsClient) GetAddonsClient() (helmmanager.ClusterAddonsClient, func return nil, nil, types.ErrNotInited } - conf := &bcsapi.Config{ - TLSConfig: ac.opts.ClientTLSConfig, - InnerClientName: common.ClusterManager, - } - - if len(ac.opts.GateWay) != 0 { - conf.Hosts = []string{ac.opts.GateWay} - conf.AuthToken = ac.opts.Token - } else { - nodeServer, err := ac.discovery.GetRandomServiceNode() - if err != nil { - return nil, nil, err - } - endpoints := utils.GetServerEndpointsFromRegistryNode(nodeServer) - conf.Hosts = endpoints - } - - blog.Infof("GetAddonsClient config[%+v]", *conf) - - cli, closeCon := helmmanager.NewHelmAddonsClient(conf) - return cli, closeCon, nil -} - -// Stop stop addonsClient -func (ac *AddonsClient) Stop() { - if ac == nil { - return - } - if ac.discovery != nil { - ac.discovery.Stop() + cli, conn, err := helmmanager.GetClient(common.ClusterManager) + if err != nil { + return nil, nil, err } - ac.cancel() + return cli.ClusterAddonsClient, conn, nil } diff --git a/bcs-services/bcs-cluster-manager/internal/remote/install/helm/client.go b/bcs-services/bcs-cluster-manager/internal/remote/install/helm/client.go index 2a158310c9..ce5af332e2 100644 --- a/bcs-services/bcs-cluster-manager/internal/remote/install/helm/client.go +++ b/bcs-services/bcs-cluster-manager/internal/remote/install/helm/client.go @@ -17,13 +17,11 @@ import ( "context" "github.com/Tencent/bk-bcs/bcs-common/common/blog" - "github.com/Tencent/bk-bcs/bcs-common/pkg/bcsapi" "github.com/Tencent/bk-bcs/bcs-common/pkg/bcsapi/helmmanager" + "github.com/Tencent/bk-bcs/bcs-common/pkg/discovery" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/common" - "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/discovery" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/install/types" - "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/utils" ) // helmClient helm-manager client @@ -65,13 +63,16 @@ func NewHelmClient(opts *types.Options) (*HelmClient, error) { } helmClient.ctx, helmClient.cancel = context.WithCancel(context.Background()) - if len(opts.GateWay) == 0 { + if !discovery.UseServiceDiscovery() { helmClient.discovery = discovery.NewModuleDiscovery(opts.Module, opts.EtcdRegistry) err := helmClient.discovery.Start() if err != nil { blog.Errorf("start discovery[%s] client failed: %v", types.ModuleHelmManager, err) return nil, err } + helmmanager.SetClientConfig(opts.ClientTLSConfig, helmClient.discovery) + } else { + helmmanager.SetClientConfig(opts.ClientTLSConfig, nil) } return helmClient, nil @@ -82,28 +83,11 @@ func (hm *HelmClient) GetHelmManagerClient() (helmmanager.HelmManagerClient, fun if hm == nil { return nil, nil, types.ErrNotInited } - - conf := &bcsapi.Config{ - TLSConfig: hm.opts.ClientTLSConfig, - InnerClientName: common.ClusterManager, - } - - if len(hm.opts.GateWay) != 0 { - conf.Hosts = []string{hm.opts.GateWay} - conf.AuthToken = hm.opts.Token - } else { - nodeServer, err := hm.discovery.GetRandomServiceNode() - if err != nil { - return nil, nil, err - } - endpoints := utils.GetServerEndpointsFromRegistryNode(nodeServer) - conf.Hosts = endpoints + cli, conn, err := helmmanager.GetClient(common.ClusterManager) + if err != nil { + return nil, nil, err } - - blog.Infof("GetHelmManagerClient config[%+v]", *conf) - - cli, closeCon := helmmanager.NewHelmClient(conf) - return cli, closeCon, nil + return cli.HelmManagerClient, conn, nil } // Stop stop HelmManagerClient diff --git a/bcs-services/bcs-cluster-manager/internal/remote/install/helm/helm.go b/bcs-services/bcs-cluster-manager/internal/remote/install/helm/helm.go index 6ff5c1dae3..f391b82223 100644 --- a/bcs-services/bcs-cluster-manager/internal/remote/install/helm/helm.go +++ b/bcs-services/bcs-cluster-manager/internal/remote/install/helm/helm.go @@ -87,17 +87,17 @@ func NewHelmInstaller(opts HelmOptions, client *HelmClient, var _ install.Installer = &HelmInstaller{} // IsInstalled returns whether the app is installed -func (h *HelmInstaller) IsInstalled(clusterID string) (bool, error) { +func (h *HelmInstaller) IsInstalled(ctx context.Context, clusterID string) (bool, error) { if h.debug { return true, nil } start := time.Now() - resp, err := h.client.GetReleaseDetailV1(context.Background(), &helmmanager.GetReleaseDetailV1Req{ - ProjectCode: h.projectID, - ClusterID: clusterID, - Namespace: h.releaseNamespace, - Name: h.releaseName, + resp, err := h.client.GetReleaseDetailV1(ctx, &helmmanager.GetReleaseDetailV1Req{ + ProjectCode: &h.projectID, + ClusterID: &clusterID, + Namespace: &h.releaseNamespace, + Name: &h.releaseName, }) if err != nil { metrics.ReportLibRequestMetric("helm", "GetReleaseDetailV1", "grpc", metrics.LibCallStatusErr, start) @@ -110,7 +110,7 @@ func (h *HelmInstaller) IsInstalled(clusterID string) (bool, error) { return false, fmt.Errorf("GetReleaseDetail failed, resp is empty") } // not found release - if resp.Code != 0 { + if *resp.Code != 0 { blog.Errorf("[HelmInstaller] GetReleaseDetail failed, code: %d, message: %s", resp.Code, resp.Message) return false, nil } @@ -121,12 +121,12 @@ func (h *HelmInstaller) IsInstalled(clusterID string) (bool, error) { return true, nil } -func (h *HelmInstaller) getChartLatestVersion(project string, repo, chart string) (string, error) { +func (h *HelmInstaller) getChartLatestVersion(ctx context.Context, project string, repo, chart string) (string, error) { start := time.Now() - resp, err := h.client.GetChartDetailV1(context.Background(), &helmmanager.GetChartDetailV1Req{ - ProjectCode: project, - RepoName: repo, - Name: chart, + resp, err := h.client.GetChartDetailV1(ctx, &helmmanager.GetChartDetailV1Req{ + ProjectCode: &project, + RepoName: &repo, + Name: &chart, }) if err != nil { metrics.ReportLibRequestMetric("helm", "GetChartDetailV1", "grpc", metrics.LibCallStatusErr, start) @@ -135,12 +135,12 @@ func (h *HelmInstaller) getChartLatestVersion(project string, repo, chart string } metrics.ReportLibRequestMetric("helm", "GetChartDetailV1", "grpc", metrics.LibCallStatusOK, start) - if resp.Code != 0 || !resp.Result { + if *resp.Code != 0 || !*resp.Result { blog.Errorf("[HelmInstaller] getChartLatestVersion[%s] failed: %v", resp.RequestID, resp.Message) return "", err } - return resp.Data.LatestVersion, nil + return *resp.Data.LatestVersion, nil } func (h *HelmInstaller) setRepo() { @@ -151,14 +151,14 @@ func (h *HelmInstaller) setRepo() { } // Install installs the app -func (h *HelmInstaller) Install(clusterID, values string) error { +func (h *HelmInstaller) Install(ctx context.Context, clusterID, values string) error { if h.debug { return nil } h.setRepo() // get chart latest version - version, err := h.getChartLatestVersion(h.projectID, h.repo, h.chartName) + version, err := h.getChartLatestVersion(ctx, h.projectID, h.repo, h.chartName) if err != nil { blog.Errorf("[HelmInstaller] getChartLatestVersion failed: %v", err) return err @@ -166,13 +166,13 @@ func (h *HelmInstaller) Install(clusterID, values string) error { // create app req := &helmmanager.InstallReleaseV1Req{ - ProjectCode: h.projectID, - ClusterID: clusterID, - Namespace: h.releaseNamespace, - Name: h.releaseName, - Repository: h.repo, - Chart: h.chartName, - Version: version, + ProjectCode: &h.projectID, + ClusterID: &clusterID, + Namespace: &h.releaseNamespace, + Name: &h.releaseName, + Repository: &h.repo, + Chart: &h.chartName, + Version: &version, Values: []string{values}, Args: install.DefaultArgsFlag, } @@ -180,7 +180,7 @@ func (h *HelmInstaller) Install(clusterID, values string) error { resp := &helmmanager.InstallReleaseV1Resp{} err = retry.Do(func() error { start := time.Now() - resp, err = h.client.InstallReleaseV1(context.Background(), req) + resp, err = h.client.InstallReleaseV1(ctx, req) if err != nil { metrics.ReportLibRequestMetric("helm", "InstallReleaseV1", "grpc", metrics.LibCallStatusErr, start) blog.Errorf("[HelmInstaller] InstallRelease failed, err: %s", err.Error()) @@ -193,9 +193,9 @@ func (h *HelmInstaller) Install(clusterID, values string) error { return fmt.Errorf("InstallRelease failed, resp is empty") } - if resp.Code != 0 || !resp.Result { + if *resp.Code != 0 || !*resp.Result { blog.Errorf("[HelmInstaller] InstallRelease failed, code: %d, message: %s", resp.Code, resp.Message) - return fmt.Errorf("InstallRelease failed, code: %d, message: %s", resp.Code, resp.Message) + return fmt.Errorf("InstallRelease failed, code: %d, message: %s", *resp.Code, *resp.Message) } return nil @@ -208,13 +208,13 @@ func (h *HelmInstaller) Install(clusterID, values string) error { } // Upgrade upgrades the app -func (h *HelmInstaller) Upgrade(clusterID, values string) error { +func (h *HelmInstaller) Upgrade(ctx context.Context, clusterID, values string) error { if h.debug { return nil } // upgrade need app status deployed - ok, err := h.CheckAppStatus(clusterID, time.Minute*10, true) + ok, err := h.CheckAppStatus(ctx, clusterID, time.Minute*10, true) if err != nil { blog.Errorf("[HelmInstaller] Upgrade CheckAppStatus failed: %v", err) return err @@ -235,19 +235,19 @@ func (h *HelmInstaller) Upgrade(clusterID, values string) error { // update app: default not update chart version req := &helmmanager.UpgradeReleaseV1Req{ - ProjectCode: h.projectID, - ClusterID: clusterID, - Namespace: h.releaseNamespace, - Name: h.releaseName, - Repository: h.repo, - Chart: h.chartName, + ProjectCode: &h.projectID, + ClusterID: &clusterID, + Namespace: &h.releaseNamespace, + Name: &h.releaseName, + Repository: &h.repo, + Chart: &h.chartName, //Version: version, Values: []string{values}, Args: install.DefaultArgsFlag, } start := time.Now() - resp, err := h.client.UpgradeReleaseV1(context.Background(), req) + resp, err := h.client.UpgradeReleaseV1(ctx, req) if err != nil { metrics.ReportLibRequestMetric("helm", "UpgradeReleaseV1", "grpc", metrics.LibCallStatusErr, start) blog.Errorf("[HelmInstaller] UpgradeRelease failed, err: %s", err.Error()) @@ -258,23 +258,23 @@ func (h *HelmInstaller) Upgrade(clusterID, values string) error { blog.Errorf("[HelmInstaller] UpgradeRelease failed, resp is empty") return fmt.Errorf("UpgradeRelease failed, resp is empty") } - if resp.Code != 0 { + if *resp.Code != 0 { blog.Errorf("[HelmInstaller] UpgradeRelease failed, code: %d, message: %s", resp.Code, resp.Message) - return fmt.Errorf("UpgradeRelease failed, code: %d, message: %s, requestID: %s", resp.Code, resp.Message, - resp.RequestID) + return fmt.Errorf("UpgradeRelease failed, code: %d, message: %s, requestID: %s", *resp.Code, *resp.Message, + *resp.RequestID) } return nil } // Uninstall uninstalls the app -func (h *HelmInstaller) Uninstall(clusterID string) error { +func (h *HelmInstaller) Uninstall(ctx context.Context, clusterID string) error { if h.debug { return nil } // get project cluster release - ok, err := h.IsInstalled(clusterID) + ok, err := h.IsInstalled(ctx, clusterID) if err != nil { blog.Errorf("[HelmInstaller] check app installed failed, err: %s", err.Error()) return err @@ -286,11 +286,11 @@ func (h *HelmInstaller) Uninstall(clusterID string) error { start := time.Now() // delete app - resp, err := h.client.UninstallReleaseV1(context.Background(), &helmmanager.UninstallReleaseV1Req{ - ProjectCode: h.projectID, - Name: h.releaseName, - Namespace: h.releaseNamespace, - ClusterID: clusterID, + resp, err := h.client.UninstallReleaseV1(ctx, &helmmanager.UninstallReleaseV1Req{ + ProjectCode: &h.projectID, + Name: &h.releaseName, + Namespace: &h.releaseNamespace, + ClusterID: &clusterID, }) if err != nil { metrics.ReportLibRequestMetric("helm", "UninstallReleaseV1", "grpc", metrics.LibCallStatusErr, start) @@ -298,10 +298,10 @@ func (h *HelmInstaller) Uninstall(clusterID string) error { return err } metrics.ReportLibRequestMetric("helm", "UninstallReleaseV1", "grpc", metrics.LibCallStatusOK, start) - if resp.Code != 0 { + if *resp.Code != 0 { blog.Errorf("[HelmInstaller] UninstallRelease failed, code: %d, message: %s", resp.Code, resp.Message) - return fmt.Errorf("UninstallRelease failed, code: %d, message: %s, requestID: %s", resp.Code, resp.Message, - resp.RequestID) + return fmt.Errorf("UninstallRelease failed, code: %d, message: %s, requestID: %s", *resp.Code, *resp.Message, + *resp.RequestID) } blog.Infof("[HelmInstaller] delete app successful[%s:%s:%v]", clusterID, h.releaseNamespace, h.releaseName) @@ -309,13 +309,14 @@ func (h *HelmInstaller) Uninstall(clusterID string) error { } // CheckAppStatus check app install status -func (h *HelmInstaller) CheckAppStatus(clusterID string, timeout time.Duration, pre bool) (bool, error) { +func (h *HelmInstaller) CheckAppStatus( + ctx context.Context, clusterID string, timeout time.Duration, pre bool) (bool, error) { if h.debug { return true, nil } // get project cluster appID - ok, err := h.IsInstalled(clusterID) + ok, err := h.IsInstalled(ctx, clusterID) if err != nil { blog.Errorf("[HelmInstaller] check app installed failed, err: %s", err.Error()) return false, err @@ -326,16 +327,16 @@ func (h *HelmInstaller) CheckAppStatus(clusterID string, timeout time.Duration, } // 等待应用正常 - ctx, cancel := context.WithTimeout(context.Background(), timeout) + ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() err = loop.LoopDoFunc(ctx, func() error { start := time.Now() // get app resp, err := h.client.GetReleaseDetailV1(ctx, &helmmanager.GetReleaseDetailV1Req{ // nolint - ProjectCode: h.projectID, - ClusterID: clusterID, - Namespace: h.releaseNamespace, - Name: h.releaseName, + ProjectCode: &h.projectID, + ClusterID: &clusterID, + Namespace: &h.releaseNamespace, + Name: &h.releaseName, }) if err != nil { metrics.ReportLibRequestMetric("helm", "GetReleaseDetailV1", "grpc", metrics.LibCallStatusErr, start) @@ -346,16 +347,16 @@ func (h *HelmInstaller) CheckAppStatus(clusterID string, timeout time.Duration, if resp == nil { return fmt.Errorf("[HelmInstaller] GetReleaseDetail failed, resp is empty") } - if resp.Code != 0 { + if *resp.Code != 0 { return fmt.Errorf("[HelmInstaller] GetReleaseDetail failed, code: %d, message: %s, requestID: %s", - resp.Code, resp.Message, resp.RequestID) + *resp.Code, *resp.Message, *resp.RequestID) } blog.Infof("[HelmInstaller] GetReleaseDetail status: %s", resp.Data.Status) // 前置检查 if pre { - switch resp.Data.Status { + switch *resp.Data.Status { case types.DeployedInstall, types.DeployedRollback, types.DeployedUpgrade, types.FailedInstall, types.FailedRollback, types.FailedUpgrade, types.FailedState, types.FailedUninstall: return loop.EndLoop @@ -370,11 +371,11 @@ func (h *HelmInstaller) CheckAppStatus(clusterID string, timeout time.Duration, // 后置检查 // 成功状态 / 失败状态 则终止 - switch resp.Data.Status { + switch *resp.Data.Status { case types.DeployedInstall, types.DeployedRollback, types.DeployedUpgrade: return loop.EndLoop case types.FailedInstall, types.FailedRollback, types.FailedUpgrade, types.FailedState: - return fmt.Errorf("[HelmInstaller] CheckAppStatus[%s] failed: %s", resp.RequestID, resp.Data.Status) + return fmt.Errorf("[HelmInstaller] CheckAppStatus[%s] failed: %s", *resp.RequestID, *resp.Data.Status) default: } diff --git a/bcs-services/bcs-cluster-manager/internal/remote/install/interface.go b/bcs-services/bcs-cluster-manager/internal/remote/install/interface.go index 4467f84954..f0ac740667 100644 --- a/bcs-services/bcs-cluster-manager/internal/remote/install/interface.go +++ b/bcs-services/bcs-cluster-manager/internal/remote/install/interface.go @@ -14,17 +14,18 @@ package install import ( + "context" "time" ) // Installer is the interface for app installer type Installer interface { - IsInstalled(clusterID string) (bool, error) - Install(clusterID, values string) error - Upgrade(clusterID, values string) error - Uninstall(clusterID string) error + IsInstalled(ctx context.Context, clusterID string) (bool, error) + Install(ctx context.Context, clusterID, values string) error + Upgrade(ctx context.Context, clusterID, values string) error + Uninstall(ctx context.Context, clusterID string) error // CheckAppStatus check app status. pre:true 前置检查;pre:false 后置检查 - CheckAppStatus(clusterID string, timeout time.Duration, pre bool) (bool, error) + CheckAppStatus(ctx context.Context, clusterID string, timeout time.Duration, pre bool) (bool, error) Close() } diff --git a/bcs-services/bcs-cluster-manager/internal/remote/project/project.go b/bcs-services/bcs-cluster-manager/internal/remote/project/project.go index 40a01c6a9a..91327f62f8 100644 --- a/bcs-services/bcs-cluster-manager/internal/remote/project/project.go +++ b/bcs-services/bcs-cluster-manager/internal/remote/project/project.go @@ -21,14 +21,12 @@ import ( "time" "github.com/Tencent/bk-bcs/bcs-common/common/blog" - "github.com/Tencent/bk-bcs/bcs-common/pkg/bcsapi" "github.com/Tencent/bk-bcs/bcs-common/pkg/bcsapi/bcsproject" "github.com/patrickmn/go-cache" - "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/discovery" + "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/common" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/metrics" rutils "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/utils" - "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/utils" ) const ( @@ -50,14 +48,12 @@ type Options struct { var ProjectClient *ProManClient // SetProjectClient set global project client -func SetProjectClient(opts *Options, disc *discovery.ModuleDiscovery) { +func SetProjectClient(opts *Options) { if opts.Module == "" { opts.Module = projectManagerServiceName } ProjectClient = &ProManClient{ - opts: opts, - disc: disc, cache: cache.New(5*time.Minute, 60*time.Minute), } } @@ -69,8 +65,6 @@ func GetProjectManagerClient() *ProManClient { // ProManClient project client type ProManClient struct { - opts *Options - disc *discovery.ModuleDiscovery cache *cache.Cache } @@ -79,31 +73,12 @@ func (pm *ProManClient) getProjectManagerClient() (*bcsproject.ProjectClient, fu if pm == nil { return nil, nil, rutils.ErrServerNotInit } - - if pm.disc == nil { - return nil, nil, fmt.Errorf("resourceManager module not enable dsicovery") - } - - // random server - nodeServer, err := pm.disc.GetRandomServiceNode() - if err != nil { - return nil, nil, err - } - endpoints := utils.GetServerEndpointsFromRegistryNode(nodeServer) - - blog.Infof("ProManClient get node[%s] from disc", nodeServer.Address) - conf := &bcsapi.Config{ - Hosts: endpoints, - TLSConfig: pm.opts.TLSConfig, - InnerClientName: "bcs-cluster-manager", - } - cli, closeCon := bcsproject.NewProjectManagerClient(conf) - - return cli, closeCon, nil + return bcsproject.GetClient(common.ClusterManager) } // GetProjectInfo get project detailed info -func (pm *ProManClient) GetProjectInfo(projectIdOrCode string, isCache bool) (*bcsproject.Project, error) { +func (pm *ProManClient) GetProjectInfo( + ctx context.Context, projectIdOrCode string, isCache bool) (*bcsproject.Project, error) { if pm == nil { return nil, rutils.ErrServerNotInit } @@ -131,8 +106,7 @@ func (pm *ProManClient) GetProjectInfo(projectIdOrCode string, isCache bool) (*b }() start := time.Now() - resp, err := cli.Project.GetProject(context.Background(), - &bcsproject.GetProjectRequest{ProjectIDOrCode: projectIdOrCode}) + resp, err := cli.Project.GetProject(ctx, &bcsproject.GetProjectRequest{ProjectIDOrCode: projectIdOrCode}) if err != nil { metrics.ReportLibRequestMetric("project", "GetProject", "grpc", metrics.LibCallStatusErr, start) blog.Errorf("GetProjectInfo[%s] GetProject failed: %v", projectIdOrCode, err) diff --git a/bcs-services/bcs-cluster-manager/internal/remote/resource/tresource/resource.go b/bcs-services/bcs-cluster-manager/internal/remote/resource/tresource/resource.go index a0f7cb05b1..c82abb2bdb 100644 --- a/bcs-services/bcs-cluster-manager/internal/remote/resource/tresource/resource.go +++ b/bcs-services/bcs-cluster-manager/internal/remote/resource/tresource/resource.go @@ -26,10 +26,10 @@ import ( "time" "github.com/Tencent/bk-bcs/bcs-common/common/blog" + "github.com/Tencent/bk-bcs/bcs-common/pkg/discovery" "github.com/avast/retry-go" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/common" - "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/discovery" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/metrics" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/loop" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/remote/resource" diff --git a/bcs-services/bcs-cluster-manager/internal/remote/user/user.go b/bcs-services/bcs-cluster-manager/internal/remote/user/user.go index 1238f78485..e53b794f9a 100644 --- a/bcs-services/bcs-cluster-manager/internal/remote/user/user.go +++ b/bcs-services/bcs-cluster-manager/internal/remote/user/user.go @@ -22,10 +22,10 @@ import ( "github.com/Tencent/bk-bcs/bcs-common/common/blog" "github.com/Tencent/bk-bcs/bcs-common/common/types" + "github.com/Tencent/bk-bcs/bcs-common/pkg/discovery" "github.com/parnurzeal/gorequest" "go-micro.dev/v4/registry" - "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/discovery" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/metrics" ) @@ -122,7 +122,7 @@ func NewUserManagerClient(opts *Options) *UserManagerClient { } userClient.ctx, userClient.cancel = context.WithCancel(context.Background()) - if len(opts.GateWay) == 0 { + if !discovery.UseServiceDiscovery() { userClient.discovery = discovery.NewModuleDiscovery(opts.Module, opts.EtcdRegistry) err := userClient.discovery.Start() if err != nil { diff --git a/bcs-services/bcs-cluster-manager/internal/tunnel/peermanager.go b/bcs-services/bcs-cluster-manager/internal/tunnel/peermanager.go index a6166ea65e..cc1e3a425e 100644 --- a/bcs-services/bcs-cluster-manager/internal/tunnel/peermanager.go +++ b/bcs-services/bcs-cluster-manager/internal/tunnel/peermanager.go @@ -22,10 +22,10 @@ import ( "github.com/Tencent/bk-bcs/bcs-common/common/blog" "github.com/Tencent/bk-bcs/bcs-common/common/websocketDialer" + "github.com/Tencent/bk-bcs/bcs-common/pkg/discovery" "go-micro.dev/v4/registry" cmcommon "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/common" - "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/discovery" "github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/options" )