Skip to content

Commit

Permalink
feat: project-manager提供项目活跃度接口
Browse files Browse the repository at this point in the history
  • Loading branch information
LidolLxf committed Dec 5, 2024
1 parent dd0b169 commit 027856a
Show file tree
Hide file tree
Showing 8 changed files with 1,729 additions and 861 deletions.
73 changes: 73 additions & 0 deletions bcs-services/bcs-project-manager/internal/actions/project/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ package project
import (
"context"

"github.com/Tencent/bk-bcs/bcs-common/pkg/odm/drivers"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/component/clientset"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/component/clustermanager"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/logging"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/store"
pm "github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/store/project"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/util/errorx"
nsutils "github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/util/namespace"
proto "github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/proto/bcsproject"
)

Expand Down Expand Up @@ -47,3 +54,69 @@ func (ga *GetAction) Do(ctx context.Context, req *proto.GetProjectRequest) (*pm.

return p, nil
}

// Active get project active
func (ga *GetAction) Active(ctx context.Context, req *proto.GetProjectActiveRequest) (bool, error) {
ga.ctx = ctx

p, err := ga.model.GetProject(ctx, req.ProjectIDOrCode)
// 未开启容器服务,直接返回不活跃
if err == drivers.ErrTableRecordNotFound {
return false, nil
}
if err != nil {
return false, errorx.NewDBErr(err.Error())
}

// 未开启容器服务,直接返回不活跃
if p.Kind != "k8s" {
return false, nil
}

clusters, err := clustermanager.ListClusters(p.ProjectID)
if err != nil {
return false, err
}

// 没有集群则返回不活跃
if len(clusters) == 0 {
return false, nil
}

// 没有共享集群命名空间则返回不活跃
for _, v := range clusters {
if v.IsShared {
isActive, err := listSharedNamespace(ctx, v.ClusterID, p.ProjectCode)
if err != nil {
return false, err
}
// 存在共享集群命名空间则返回活跃
if isActive {
return true, nil
}
}
}

return false, nil
}

// listSharedNamespace list shares namespace
func listSharedNamespace(ctx context.Context, clusterID, projectCode string) (bool, error) {
client, err := clientset.GetClientGroup().Client(clusterID)
if err != nil {
logging.Error("get clientset for cluster %s failed, err: %s", clusterID, err.Error())
return false, err
}
nsList, err := client.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return false, errorx.NewClusterErr(err.Error())
}

namespaces := nsutils.FilterNamespaces(nsList, true, projectCode)

if len(namespaces) != 0 {
return true, nil
}

return false, nil
}
14 changes: 14 additions & 0 deletions bcs-services/bcs-project-manager/internal/handler/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,17 @@ func getProjectIDs(p *map[string]interface{}) []string {
}
return ids
}

// GetProjectActive get projects active
func (p *ProjectHandler) GetProjectActive(ctx context.Context,
req *proto.GetProjectActiveRequest, resp *proto.GetProjectActiveResponse) error {
lap := project.NewGetAction(p.model)
isActive, e := lap.Active(ctx, req)
if e != nil {
return e
}
resp.Data = &proto.ProjectActiveData{
IsActive: isActive,
}
return nil
}
Loading

0 comments on commit 027856a

Please sign in to comment.