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: cluster-resources新增泳道特性 #3668

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion bcs-services/cluster-resources/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}),
)

Expand Down
1 change: 1 addition & 0 deletions bcs-services/cluster-resources/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions bcs-services/cluster-resources/pkg/cluster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
20 changes: 0 additions & 20 deletions bcs-services/cluster-resources/pkg/util/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
10 changes: 0 additions & 10 deletions bcs-services/cluster-resources/pkg/util/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
8 changes: 5 additions & 3 deletions bcs-services/cluster-resources/pkg/util/httpx/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Loading