From cbfd1b895dc5f22990cd4e9262ed447b32d9e89a Mon Sep 17 00:00:00 2001 From: LidolLxf <13660354959@163.com> Date: Mon, 20 Jan 2025 16:40:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20cluster-resources=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=B3=B3=E9=81=93=E7=89=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bcs-services/cluster-resources/cmd/init.go | 3 ++- bcs-services/cluster-resources/go.mod | 1 + .../cluster-resources/pkg/cluster/client.go | 2 ++ .../pkg/component/project/client.go | 2 ++ .../cluster-resources/pkg/util/http/http.go | 20 ------------------- .../pkg/util/http/http_test.go | 10 ---------- .../pkg/util/httpx/middleware.go | 8 +++++--- 7 files changed, 12 insertions(+), 34 deletions(-) diff --git a/bcs-services/cluster-resources/cmd/init.go b/bcs-services/cluster-resources/cmd/init.go index 7febedf6f1..1584a629bf 100644 --- a/bcs-services/cluster-resources/cmd/init.go +++ b/bcs-services/cluster-resources/cmd/init.go @@ -26,6 +26,7 @@ import ( "github.com/Tencent/bk-bcs/bcs-common/common/ssl" "github.com/Tencent/bk-bcs/bcs-common/common/tcp/listener" "github.com/Tencent/bk-bcs/bcs-common/common/types" + "github.com/Tencent/bk-bcs/bcs-common/pkg/header" "github.com/Tencent/bk-bcs/bcs-common/pkg/odm/drivers/mongo" microEtcd "github.com/go-micro/plugins/v4/registry/etcd" microGrpc "github.com/go-micro/plugins/v4/server/grpc" @@ -306,7 +307,7 @@ func (crSvc *clusterResourcesService) initTLSConfig() error { // initHTTPService 初始化 HTTP 服务 func (crSvc *clusterResourcesService) initHTTPService() error { rmMux := runtime.NewServeMux( - runtime.WithIncomingHeaderMatcher(httpUtil.CustomHeaderMatcher), + runtime.WithIncomingHeaderMatcher(header.CustomHeaderMatcher), runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true}), ) diff --git a/bcs-services/cluster-resources/go.mod b/bcs-services/cluster-resources/go.mod index e92080c06f..6dd90a967f 100644 --- a/bcs-services/cluster-resources/go.mod +++ b/bcs-services/cluster-resources/go.mod @@ -3,6 +3,7 @@ module github.com/Tencent/bk-bcs/bcs-services/cluster-resources go 1.23 replace ( + github.com/Tencent/bk-bcs/bcs-common => github.com/LidolLxf/bk-bcs/bcs-common v0.0.0-20250120075906-fd5988c50f3f github.com/chai2010/gettext-go => github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5 k8s.io/api => k8s.io/api v0.23.1 k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.23.1 diff --git a/bcs-services/cluster-resources/pkg/cluster/client.go b/bcs-services/cluster-resources/pkg/cluster/client.go index bf2ecfca6e..6b070ec8ec 100644 --- a/bcs-services/cluster-resources/pkg/cluster/client.go +++ b/bcs-services/cluster-resources/pkg/cluster/client.go @@ -18,6 +18,7 @@ import ( "sync" "time" + "github.com/Tencent/bk-bcs/bcs-common/pkg/header" "github.com/patrickmn/go-cache" "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/common/runmode" @@ -96,6 +97,7 @@ func (c *CMClient) fetchClusterInfo(ctx context.Context, clusterID string) (*Clu resp, err := httpclient.GetClient().R(). SetContext(ctx). + SetHeaders(header.GetLaneIDByCtx(ctx)). SetAuthToken(config.G.BCSAPIGW.AuthToken). Get(url) diff --git a/bcs-services/cluster-resources/pkg/component/project/client.go b/bcs-services/cluster-resources/pkg/component/project/client.go index 304eee3ff4..81d81eb03a 100644 --- a/bcs-services/cluster-resources/pkg/component/project/client.go +++ b/bcs-services/cluster-resources/pkg/component/project/client.go @@ -18,6 +18,7 @@ import ( "sync" "time" + "github.com/Tencent/bk-bcs/bcs-common/pkg/header" "github.com/patrickmn/go-cache" "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/common/runmode" @@ -101,6 +102,7 @@ func (c *ProjClient) fetchProjInfo(ctx context.Context, projectID string) (*Proj resp, err := httpclient.GetClient().R(). SetContext(ctx). SetHeader("X-Project-Username", ""). // bcs_project 要求有这个header + SetHeaders(header.GetLaneIDByCtx(ctx)). SetAuthToken(config.G.BCSAPIGW.AuthToken). Get(url) diff --git a/bcs-services/cluster-resources/pkg/util/http/http.go b/bcs-services/cluster-resources/pkg/util/http/http.go index 0f980da8ba..c4319fb3d0 100644 --- a/bcs-services/cluster-resources/pkg/util/http/http.go +++ b/bcs-services/cluster-resources/pkg/util/http/http.go @@ -16,29 +16,9 @@ package http import ( "strings" - "github.com/grpc-ecosystem/grpc-gateway/runtime" - - "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/common/ctxkey" "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/slice" ) -// CustomHeaderMatcher 自定义 HTTP Header Matcher -func CustomHeaderMatcher(key string) (string, bool) { - switch key { - case "X-Request-Id": - return "X-Request-Id", true - case "Traceparent": - // http -> grpc Traceparent - return "Traceparent", true - case ctxkey.CustomUsernameHeaderKey: - return ctxkey.CustomUsernameHeaderKey, true - case ctxkey.InnerClientHeaderKey: - return ctxkey.CustomUsernameHeaderKey, true - default: - return runtime.DefaultHeaderMatcher(key) - } -} - // 会在 websocket 连接中被转发的 Header Key(可按需添加) var wsHeadersToForward = []string{"origin", "referer", "authorization", "cookie"} diff --git a/bcs-services/cluster-resources/pkg/util/http/http_test.go b/bcs-services/cluster-resources/pkg/util/http/http_test.go index e007455025..167ad28504 100644 --- a/bcs-services/cluster-resources/pkg/util/http/http_test.go +++ b/bcs-services/cluster-resources/pkg/util/http/http_test.go @@ -20,16 +20,6 @@ import ( "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/http" ) -func TestCustomHeaderMatcher(t *testing.T) { - // 自定义头字段 - ret, _ := http.CustomHeaderMatcher("X-Request-Id") - assert.Equal(t, "X-Request-Id", ret) - - // 标准头字段 - ret, _ = http.CustomHeaderMatcher("Content-Type") - assert.Equal(t, "grpcgateway-Content-Type", ret) -} - func TestWSHeaderForwarder(t *testing.T) { // 允许转发的 Header Key assert.True(t, http.WSHeaderForwarder("Origin")) diff --git a/bcs-services/cluster-resources/pkg/util/httpx/middleware.go b/bcs-services/cluster-resources/pkg/util/httpx/middleware.go index 40ca97a1fb..e51185f02a 100644 --- a/bcs-services/cluster-resources/pkg/util/httpx/middleware.go +++ b/bcs-services/cluster-resources/pkg/util/httpx/middleware.go @@ -24,6 +24,7 @@ import ( "github.com/Tencent/bk-bcs/bcs-common/common/blog" "github.com/Tencent/bk-bcs/bcs-common/pkg/auth/jwt" bcsJwt "github.com/Tencent/bk-bcs/bcs-common/pkg/auth/jwt" + "github.com/Tencent/bk-bcs/bcs-common/pkg/header" middleauth "github.com/Tencent/bk-bcs/bcs-services/pkg/bcs-auth/middleware" jwtGo "github.com/golang-jwt/jwt/v4" "github.com/google/uuid" @@ -104,15 +105,16 @@ func ParseProjectIDMiddleware(next http.Handler) http.Handler { next.ServeHTTP(w, r) return } - - pj, err := projectClient.GetProjectInfo(r.Context(), projectCode) + // X-Lane往下透传 + ctx := header.WithLaneIdCtx(r.Context(), r.Header) + pj, err := projectClient.GetProjectInfo(ctx, projectCode) if err != nil { msg := fmt.Errorf("ParseProjectID get projectID error, projectCode: %s, err: %s", projectCode, err.Error()) ResponseSystemError(w, r, msg) return } - ctx := context.WithValue(r.Context(), contextx.ProjectCodeContextKey, pj.Code) + ctx = context.WithValue(ctx, contextx.ProjectCodeContextKey, pj.Code) ctx = context.WithValue(ctx, contextx.ProjectIDContextKey, pj.ID) r = r.WithContext(ctx) next.ServeHTTP(w, r)