Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 共享集群支持查询集群下所有命名空间的release #3657

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion bcs-services/bcs-helm-manager/internal/actions/release/listv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -103,7 +106,11 @@ func (l *ListReleaseV1Action) list() (*helmmanager.ReleaseListData, error) {
}

if cluster.IsShared && len(option.Namespace) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要先获取命名空间列表,然后根据列表并发去获取 release

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
Expand All @@ -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)
Expand Down
47 changes: 46 additions & 1 deletion bcs-services/bcs-helm-manager/internal/actions/release/listv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的 context 可以传进来

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()
Expand Down
Loading