From b2717994e96648cc68b8d0094b292955fc723234 Mon Sep 17 00:00:00 2001 From: LidolLxf <13660354959@163.com> Date: Thu, 26 Dec 2024 15:32:36 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=85=B1=E4=BA=AB=E9=9B=86?= =?UTF-8?q?=E7=BE=A4=E6=94=AF=E6=8C=81=E6=9F=A5=E8=AF=A2=E9=9B=86=E7=BE=A4?= =?UTF-8?q?=E4=B8=8B=E6=89=80=E6=9C=89=E5=91=BD=E5=90=8D=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=E7=9A=84release?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/actions/release/listv1.go | 47 ++++++++++++++++++- .../internal/actions/release/listv2.go | 47 ++++++++++++++++++- .../internal/component/project/project.go | 25 ++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) diff --git a/bcs-services/bcs-helm-manager/internal/actions/release/listv1.go b/bcs-services/bcs-helm-manager/internal/actions/release/listv1.go index 29a2c142f1..978b6f7f61 100644 --- a/bcs-services/bcs-helm-manager/internal/actions/release/listv1.go +++ b/bcs-services/bcs-helm-manager/internal/actions/release/listv1.go @@ -17,16 +17,19 @@ import ( "fmt" "sort" "strings" + "sync" "github.com/Tencent/bk-bcs/bcs-common/common/blog" "github.com/Tencent/bk-bcs/bcs-common/pkg/odm/operator" authUtils "github.com/Tencent/bk-bcs/bcs-services/pkg/bcs-auth/utils" "go.mongodb.org/mongo-driver/bson/primitive" + "golang.org/x/sync/errgroup" helmrelease "helm.sh/helm/v3/pkg/release" "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/auth" "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/common" "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/component/clustermanager" + "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/component/project" "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/release" "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/store" "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/store/entity" @@ -103,7 +106,11 @@ func (l *ListReleaseV1Action) list() (*helmmanager.ReleaseListData, error) { } if cluster.IsShared && len(option.Namespace) == 0 { - return l.mergeReleases(nil, dbReleases), nil + clusterReleases, errr := l.listReleaseByNamespaces() + if errr != nil { + return nil, errr + } + return l.mergeReleases(clusterReleases, dbReleases), nil } // get release from cluster @@ -120,6 +127,44 @@ func (l *ListReleaseV1Action) list() (*helmmanager.ReleaseListData, error) { return l.mergeReleases(clusterReleases, dbReleases), nil } +// 共享集群支持查询集群下所有命名空间的release +func (l *ListReleaseV1Action) listReleaseByNamespaces() ([]*helmmanager.Release, error) { + namespaces, err := project.ListNamespaces(l.req.GetProjectCode(), l.req.GetClusterID()) + if err != nil { + return nil, err + } + clusterReleases := make([]*helmmanager.Release, 0) + eg := errgroup.Group{} + mux := sync.Mutex{} + eg.SetLimit(10) + for _, data := range namespaces { + nsData := data + eg.Go(func() error { + + // get release from cluster + _, originReleases, errr := l.releaseHandler.Cluster(l.req.GetClusterID()).List(l.ctx, release.ListOption{ + Namespace: nsData.Name, + Name: "", + }) + if errr != nil { + return fmt.Errorf("list release from cluster error, %s", errr.Error()) + } + mux.Lock() + for _, item := range originReleases { + clusterReleases = append(clusterReleases, item.Transfer2Proto(contextx.GetProjectCodeFromCtx(l.ctx), + l.req.GetClusterID())) + } + mux.Unlock() + return nil + }) + } + err = eg.Wait() + if err != nil { + return nil, err + } + return clusterReleases, nil +} + func (l *ListReleaseV1Action) mergeReleases(clusterReleases, dbReleases []*helmmanager.Release) *helmmanager.ReleaseListData { release := make([]*helmmanager.Release, 0) diff --git a/bcs-services/bcs-helm-manager/internal/actions/release/listv2.go b/bcs-services/bcs-helm-manager/internal/actions/release/listv2.go index c3b1267b7a..2e7911aa5a 100644 --- a/bcs-services/bcs-helm-manager/internal/actions/release/listv2.go +++ b/bcs-services/bcs-helm-manager/internal/actions/release/listv2.go @@ -15,11 +15,14 @@ package release import ( "context" "fmt" + "sync" "github.com/Tencent/bk-bcs/bcs-common/common/blog" + "golang.org/x/sync/errgroup" "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/common" "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/component/clustermanager" + "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/component/project" "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/release" "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/store" "github.com/Tencent/bk-bcs/bcs-services/bcs-helm-manager/internal/store/utils" @@ -93,7 +96,11 @@ func (l *ListReleaseV2Action) list() (*helmmanager.ReleaseListData, error) { } if cluster.IsShared && len(option.Namespace) == 0 { - return l.mergeReleases(nil, dbReleases), nil + clusterReleases, errr := l.listReleaseByNamespaces() + if errr != nil { + return nil, errr + } + return l.mergeReleases(clusterReleases, dbReleases), nil } // get release from cluster @@ -109,3 +116,41 @@ func (l *ListReleaseV2Action) list() (*helmmanager.ReleaseListData, error) { // merge release return l.mergeReleases(clusterReleases, dbReleases), nil } + +// 共享集群支持查询集群下所有命名空间的release +func (l *ListReleaseV2Action) listReleaseByNamespaces() ([]*helmmanager.Release, error) { + namespaces, err := project.ListNamespaces(l.req.GetProjectCode(), l.req.GetClusterID()) + if err != nil { + return nil, err + } + clusterReleases := make([]*helmmanager.Release, 0) + eg := errgroup.Group{} + mux := sync.Mutex{} + eg.SetLimit(10) + for _, data := range namespaces { + nsData := data + eg.Go(func() error { + + // get release from cluster + _, originReleases, errr := l.releaseHandler.Cluster(l.req.GetClusterID()).ListV2(l.ctx, release.ListOption{ + Namespace: nsData.Name, + Name: "", + }) + if errr != nil { + return fmt.Errorf("list release from cluster error, %s", errr.Error()) + } + mux.Lock() + for _, item := range originReleases { + clusterReleases = append(clusterReleases, item.Transfer2Proto(contextx.GetProjectCodeFromCtx(l.ctx), + l.req.GetClusterID())) + } + mux.Unlock() + return nil + }) + } + err = eg.Wait() + if err != nil { + return nil, err + } + return clusterReleases, nil +} diff --git a/bcs-services/bcs-helm-manager/internal/component/project/project.go b/bcs-services/bcs-helm-manager/internal/component/project/project.go index b4c1a223a8..ad6433faee 100644 --- a/bcs-services/bcs-helm-manager/internal/component/project/project.go +++ b/bcs-services/bcs-helm-manager/internal/component/project/project.go @@ -112,6 +112,31 @@ func GetProjectByCode(projectCode string) (*bcsproject.Project, error) { return p.Data, nil } +// ListNamespaces list namespaces +func ListNamespaces(projectCode, clusterID string) ([]*bcsproject.NamespaceData, error) { + cli, close, err := client.getProjectClient() + defer func() { + if close != nil { + close() + } + }() + if err != nil { + return nil, err + } + p, err := cli.Namespace.ListNamespaces(context.Background(), &bcsproject.ListNamespacesRequest{ + ProjectCode: projectCode, + ClusterID: clusterID, + }) + if err != nil { + return nil, fmt.Errorf("ListNamespaces error: %s", err) + } + if p.Code != 0 || p.Data == nil { + return nil, fmt.Errorf("ListNamespaces error, code: %d, message: %s, requestID: %s", + p.Code, p.GetMessage(), p.GetRequestID()) + } + return p.Data, nil +} + // GetVariable get project from project code func GetVariable(projectCode, clusterID, namespace string) ([]*bcsproject.VariableValue, error) { client, close, err := client.getProjectClient() From 4d2b75fcf0f832c53ad61ee640e48211a90ee3f2 Mon Sep 17 00:00:00 2001 From: LidolLxf <13660354959@163.com> Date: Tue, 14 Jan 2025 15:24:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20context=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bcs-helm-manager/internal/actions/release/listv1.go | 2 +- .../bcs-helm-manager/internal/actions/release/listv2.go | 2 +- .../bcs-helm-manager/internal/component/project/project.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bcs-services/bcs-helm-manager/internal/actions/release/listv1.go b/bcs-services/bcs-helm-manager/internal/actions/release/listv1.go index 978b6f7f61..7034e337e5 100644 --- a/bcs-services/bcs-helm-manager/internal/actions/release/listv1.go +++ b/bcs-services/bcs-helm-manager/internal/actions/release/listv1.go @@ -129,7 +129,7 @@ func (l *ListReleaseV1Action) list() (*helmmanager.ReleaseListData, error) { // 共享集群支持查询集群下所有命名空间的release func (l *ListReleaseV1Action) listReleaseByNamespaces() ([]*helmmanager.Release, error) { - namespaces, err := project.ListNamespaces(l.req.GetProjectCode(), l.req.GetClusterID()) + namespaces, err := project.ListNamespaces(l.ctx, l.req.GetProjectCode(), l.req.GetClusterID()) if err != nil { return nil, err } diff --git a/bcs-services/bcs-helm-manager/internal/actions/release/listv2.go b/bcs-services/bcs-helm-manager/internal/actions/release/listv2.go index 2e7911aa5a..a221d293f7 100644 --- a/bcs-services/bcs-helm-manager/internal/actions/release/listv2.go +++ b/bcs-services/bcs-helm-manager/internal/actions/release/listv2.go @@ -119,7 +119,7 @@ func (l *ListReleaseV2Action) list() (*helmmanager.ReleaseListData, error) { // 共享集群支持查询集群下所有命名空间的release func (l *ListReleaseV2Action) listReleaseByNamespaces() ([]*helmmanager.Release, error) { - namespaces, err := project.ListNamespaces(l.req.GetProjectCode(), l.req.GetClusterID()) + namespaces, err := project.ListNamespaces(l.ctx, l.req.GetProjectCode(), l.req.GetClusterID()) if err != nil { return nil, err } diff --git a/bcs-services/bcs-helm-manager/internal/component/project/project.go b/bcs-services/bcs-helm-manager/internal/component/project/project.go index ad6433faee..6933f9dc1e 100644 --- a/bcs-services/bcs-helm-manager/internal/component/project/project.go +++ b/bcs-services/bcs-helm-manager/internal/component/project/project.go @@ -113,7 +113,7 @@ func GetProjectByCode(projectCode string) (*bcsproject.Project, error) { } // ListNamespaces list namespaces -func ListNamespaces(projectCode, clusterID string) ([]*bcsproject.NamespaceData, error) { +func ListNamespaces(ctx context.Context, projectCode, clusterID string) ([]*bcsproject.NamespaceData, error) { cli, close, err := client.getProjectClient() defer func() { if close != nil { @@ -123,7 +123,7 @@ func ListNamespaces(projectCode, clusterID string) ([]*bcsproject.NamespaceData, if err != nil { return nil, err } - p, err := cli.Namespace.ListNamespaces(context.Background(), &bcsproject.ListNamespacesRequest{ + p, err := cli.Namespace.ListNamespaces(ctx, &bcsproject.ListNamespacesRequest{ ProjectCode: projectCode, ClusterID: clusterID, })