Skip to content

Commit

Permalink
feat: project-manager 支持自定义配置projectcode注解
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyudeqiu committed Dec 3, 2024
1 parent b4d9375 commit e58431b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ taskConfig:
address: ""
exchange: ""
workerCnt: ""

sharedClusterConfig:
annoKeyProjCode: ""
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/component/bcscc"
"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/iam"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/config"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/logging"
nsm "github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/store/namespace"
vdm "github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/store/variabledefinition"
Expand Down Expand Up @@ -66,8 +67,8 @@ func (a *SharedNamespaceAction) CreateNamespaceCallback(ctx context.Context,
namespace := &corev1.Namespace{}
namespace.SetName(ns.Name)
namespace.SetAnnotations(map[string]string{
constant.AnnotationKeyProjectCode: req.GetProjectCode(),
constant.AnnotationKeyCreator: ns.Creator,
config.GlobalConf.SharedClusterConfig.AnnoKeyProjCode: req.GetProjectCode(),
constant.AnnotationKeyCreator: ns.Creator,
})
_, err = client.CoreV1().Namespaces().Create(ctx, namespace, metav1.CreateOptions{})
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/actions/namespace/independent"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/common/constant"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/common/envs"
"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/itsm"
Expand All @@ -50,7 +49,7 @@ func (a *SharedNamespaceAction) CreateNamespace(ctx context.Context,
if !config.GlobalConf.ITSM.Enable {
ia := independent.NewIndependentNamespaceAction(a.model)
req.Annotations = append(req.Annotations, &proto.Annotation{
Key: constant.AnnotationKeyProjectCode,
Key: config.GlobalConf.SharedClusterConfig.AnnoKeyProjCode,
Value: req.GetProjectCode(),
})
return ia.CreateNamespace(ctx, req, resp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/common/constant"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/component/clientset"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/config"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/logging"
"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"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (a *SharedNamespaceAction) ListNativeNamespaces(ctx context.Context,
}
retDatas := []*proto.NativeNamespaceData{}
for _, namespace := range namespaces {
projectCode, ok := namespace.Annotations[constant.AnnotationKeyProjectCode]
projectCode, ok := namespace.Annotations[config.GlobalConf.SharedClusterConfig.AnnoKeyProjCode]
if !ok {
continue
}
Expand Down
4 changes: 4 additions & 0 deletions bcs-services/bcs-project-manager/internal/common/envs/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package envs

import (
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/common/constant"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/util/stringx"
)

Expand All @@ -28,4 +29,7 @@ var (
// BCSGatewayToken bcs gateway token
BCSGatewayToken = stringx.GetEnv("gatewayToken", "")
BCSNamespacePrefix = stringx.GetEnv("BCS_NAMESPACE_PREFIX", "bcs")

// AnnotationKeyProjectCode shared cluster project code annotation key
AnnotationKeyProjectCode = stringx.GetEnv("annotationKeyProjectCode", constant.AnnotationKeyProjectCode)
)
9 changes: 9 additions & 0 deletions bcs-services/bcs-project-manager/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ type TaskConfig struct {
WorkerCnt int `yaml:"workerCnt"`
}

// SharedClusterConfig 共享集群相关配置
type SharedClusterConfig struct {
AnnoKeyProjCode string `yaml:"annoKeyProjCode"`
}

// ProjectConfig 项目的配置信息
type ProjectConfig struct {
Etcd EtcdConfig `yaml:"etcd"`
Expand All @@ -201,6 +206,7 @@ type ProjectConfig struct {
TracingConfig conf.TracingConfig `yaml:"tracingConfig"`
RestrictAuthorizedProjects bool `yaml:"restrictAuthorizedProjects"`
TaskConfig TaskConfig `yaml:"taskConfig"`
SharedClusterConfig SharedClusterConfig `yaml:"sharedClusterConfig"`
}

func (conf *ProjectConfig) initServerAddress() {
Expand All @@ -220,6 +226,9 @@ func (conf *ProjectConfig) initFromEnv() {
if conf.BcsGateway.Token == "" {
conf.BcsGateway.Token = envs.BCSGatewayToken
}
if conf.SharedClusterConfig.AnnoKeyProjCode == "" {
conf.SharedClusterConfig.AnnoKeyProjCode = envs.AnnotationKeyProjectCode
}
}

// GlobalConf 项目配置信息,全局都可以使用
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (
corev1 "k8s.io/api/core/v1"

"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/common/constant"
"github.com/Tencent/bk-bcs/bcs-services/bcs-project-manager/internal/config"
)

// FilterNamespaces filter shared namespace
func FilterNamespaces(namespaceList *corev1.NamespaceList, shared bool, projectCode string) []corev1.Namespace {
nsList := []corev1.Namespace{}
for _, ns := range namespaceList.Items {
if shared && ns.Annotations[constant.AnnotationKeyProjectCode] != projectCode {
if shared && ns.Annotations[config.GlobalConf.SharedClusterConfig.AnnoKeyProjCode] != projectCode {
continue
}
nsList = append(nsList, ns)
Expand Down

0 comments on commit e58431b

Please sign in to comment.