From e58431b8672f39a1552edad74bc5781364a700f0 Mon Sep 17 00:00:00 2001 From: carlchen Date: Tue, 19 Nov 2024 17:30:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20project-manager=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E9=85=8D=E7=BD=AEprojectcode?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bcs-project-manager/etc/bcs-project-manager.yaml | 3 ++- .../internal/actions/namespace/shared/create-callback.go | 5 +++-- .../internal/actions/namespace/shared/create.go | 3 +-- .../internal/actions/namespace/shared/native-list.go | 3 ++- .../bcs-project-manager/internal/common/envs/envs.go | 4 ++++ .../bcs-project-manager/internal/config/config.go | 9 +++++++++ .../internal/util/namespace/namespace.go | 3 ++- 7 files changed, 23 insertions(+), 7 deletions(-) diff --git a/bcs-services/bcs-project-manager/etc/bcs-project-manager.yaml b/bcs-services/bcs-project-manager/etc/bcs-project-manager.yaml index 794187f0ec..06eaf9a7b1 100644 --- a/bcs-services/bcs-project-manager/etc/bcs-project-manager.yaml +++ b/bcs-services/bcs-project-manager/etc/bcs-project-manager.yaml @@ -92,4 +92,5 @@ taskConfig: address: "" exchange: "" workerCnt: "" - +sharedClusterConfig: + annoKeyProjCode: "" diff --git a/bcs-services/bcs-project-manager/internal/actions/namespace/shared/create-callback.go b/bcs-services/bcs-project-manager/internal/actions/namespace/shared/create-callback.go index bec4e2ec54..40f7ffe828 100644 --- a/bcs-services/bcs-project-manager/internal/actions/namespace/shared/create-callback.go +++ b/bcs-services/bcs-project-manager/internal/actions/namespace/shared/create-callback.go @@ -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" @@ -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 { diff --git a/bcs-services/bcs-project-manager/internal/actions/namespace/shared/create.go b/bcs-services/bcs-project-manager/internal/actions/namespace/shared/create.go index dcbb132cd3..6feef6673c 100644 --- a/bcs-services/bcs-project-manager/internal/actions/namespace/shared/create.go +++ b/bcs-services/bcs-project-manager/internal/actions/namespace/shared/create.go @@ -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" @@ -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) diff --git a/bcs-services/bcs-project-manager/internal/actions/namespace/shared/native-list.go b/bcs-services/bcs-project-manager/internal/actions/namespace/shared/native-list.go index 9897512c42..e4c897abaa 100644 --- a/bcs-services/bcs-project-manager/internal/actions/namespace/shared/native-list.go +++ b/bcs-services/bcs-project-manager/internal/actions/namespace/shared/native-list.go @@ -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" @@ -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 } diff --git a/bcs-services/bcs-project-manager/internal/common/envs/envs.go b/bcs-services/bcs-project-manager/internal/common/envs/envs.go index f597ed0449..ef21b90d4c 100644 --- a/bcs-services/bcs-project-manager/internal/common/envs/envs.go +++ b/bcs-services/bcs-project-manager/internal/common/envs/envs.go @@ -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" ) @@ -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) ) diff --git a/bcs-services/bcs-project-manager/internal/config/config.go b/bcs-services/bcs-project-manager/internal/config/config.go index 2a7ac4b6bd..eec80151ab 100644 --- a/bcs-services/bcs-project-manager/internal/config/config.go +++ b/bcs-services/bcs-project-manager/internal/config/config.go @@ -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"` @@ -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() { @@ -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 项目配置信息,全局都可以使用 diff --git a/bcs-services/bcs-project-manager/internal/util/namespace/namespace.go b/bcs-services/bcs-project-manager/internal/util/namespace/namespace.go index 07bf0bccb9..ee17e88b37 100644 --- a/bcs-services/bcs-project-manager/internal/util/namespace/namespace.go +++ b/bcs-services/bcs-project-manager/internal/util/namespace/namespace.go @@ -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)