Skip to content

Commit

Permalink
feat: cluster-resources新增泳道特性
Browse files Browse the repository at this point in the history
  • Loading branch information
LidolLxf committed Jan 14, 2025
1 parent 9552e83 commit 29f7792
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 38 deletions.
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-20250114064919-9d414c14aedf
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
11 changes: 9 additions & 2 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 @@ -94,8 +95,14 @@ func (c *CMClient) fetchClusterInfoWithCache(ctx context.Context, clusterID stri
func (c *CMClient) fetchClusterInfo(ctx context.Context, clusterID string) (*Cluster, error) {
url := fmt.Sprintf("%s/bcsapi/v4/clustermanager/v1/cluster/%s", config.G.BCSAPIGW.Host, clusterID)

resp, err := httpclient.GetClient().R().
SetContext(ctx).
httpRClient := httpclient.GetClient().R().
SetContext(ctx)

laneIDKey, laneIDValue := header.GetLaneIDByCtx(ctx)
if laneIDKey != "" {
httpRClient.SetHeader(laneIDKey, laneIDValue) // 泳道特性
}
resp, err := httpRClient.
SetAuthToken(config.G.BCSAPIGW.AuthToken).
Get(url)

Expand Down
11 changes: 9 additions & 2 deletions bcs-services/cluster-resources/pkg/component/project/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 @@ -98,9 +99,15 @@ func (c *ProjClient) fetchProjInfoWithCache(ctx context.Context, projectID strin
func (c *ProjClient) fetchProjInfo(ctx context.Context, projectID string) (*Project, error) {
url := fmt.Sprintf("%s/bcsapi/v4/bcsproject/v1/projects/%s", config.G.BCSAPIGW.Host, projectID)

resp, err := httpclient.GetClient().R().
httpRClient := httpclient.GetClient().R().
SetContext(ctx).
SetHeader("X-Project-Username", ""). // bcs_project 要求有这个header
SetHeader("X-Project-Username", "") // bcs_project 要求有这个header

laneIDKey, laneIDValue := header.GetLaneIDByCtx(ctx)
if laneIDKey != "" {
httpRClient.SetHeader(laneIDKey, laneIDValue) // 泳道特性
}
resp, err := httpRClient.
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(r.Context(), contextx.ProjectCodeContextKey, pj.Code)
ctx = context.WithValue(ctx, contextx.ProjectIDContextKey, pj.ID)
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
Expand Down

0 comments on commit 29f7792

Please sign in to comment.