Skip to content

Commit

Permalink
feat: refactor event proxy api (merge request !2197)
Browse files Browse the repository at this point in the history
Squash merge branch 'feat/event' into 'master'
feat: refactor event proxy api
  • Loading branch information
adevjoe authored and ifooth committed Jan 16, 2025
1 parent ca6e4fc commit 0edc608
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
52 changes: 11 additions & 41 deletions bcs-services/cluster-resources/cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package cmd

import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"errors"
Expand All @@ -25,7 +24,6 @@ import (
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -40,7 +38,6 @@ import (
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/contextx"
httpUtil "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/http"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/httpx"
"github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/stringx"
)

// SpaceNames model template space name
Expand Down Expand Up @@ -69,8 +66,8 @@ func NewAPIRouter(crs *clusterResourcesService) *mux.Router {
r.Use(httpx.AuthorizationMiddleware)
r.Use(httpx.AuditMiddleware)

// events接口反向代理,单独对cluster进行鉴权
r.Methods("GET").Path("/clusterresources/api/v1/projects/{projectCode}/clusters/{clusterID}/storage/events").
// events 接口代理
r.Methods("GET").Path("/clusterresources/api/v1/projects/{projectCode}/clusters/{clusterID}/events").
Handler(httpx.ParseClusterIDMiddleware(http.HandlerFunc(StorageEvents(crs))))
// import template
r.Methods("POST").Path("/clusterresources/api/v1/projects/{projectCode}/import/template").
Expand All @@ -84,53 +81,26 @@ func NewAPIRouter(crs *clusterResourcesService) *mux.Router {
// StorageEvents reverse proxy events
func StorageEvents(crs *clusterResourcesService) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
targetURL := fmt.Sprintf("%s/bcsapi/v4/storage/events", config.G.BCSAPIGW.Host)
targetURLPath := fmt.Sprintf("%s/bcsstorage/v1/events", config.G.Component.BCSStorageHost)

targetURLParse, err := url.Parse(targetURL)
targetURL, err := url.Parse(targetURLPath)
if err != nil {
httpx.ResponseSystemError(w, r, err)
return
}
clusterID := contextx.GetClusterIDFromCtx(r.Context())
query := r.URL.Query()
query.Set("clusterId", clusterID)
targetURL.RawQuery = query.Encode()

postData := paramToPostData(r.URL)
postData["clusterId"] = clusterID
b, err := json.Marshal(postData)
if err != nil {
httpx.ResponseSystemError(w, r, err)
return
}

proxy := httpUtil.NewHTTPReverseProxy(crs.clientTLSConfig, modifyRequest(targetURLParse, b))
proxy := httpUtil.NewHTTPReverseProxy(crs.clientTLSConfig, func(request *http.Request) {
request.URL = targetURL
request.Method = http.MethodGet
})
proxy.ServeHTTP(w, r)
}
}

// GET参数转post data
func paramToPostData(reqUrl *url.URL) map[string]interface{} {
query := reqUrl.Query()
postData := make(map[string]interface{})
for k := range query {
if k == "offset" || k == "length" {
postData[k] = stringx.GetIntOrDefault(query.Get(k))
continue
}
postData[k] = query.Get(k)
}
return postData
}

// 反向代理请求处理
func modifyRequest(targetURL *url.URL, reqBody []byte) func(r *http.Request) {
return func(r *http.Request) {
r.URL = targetURL
r.Method = http.MethodPost
r.Header.Set("Content-Length", strconv.Itoa(len(reqBody)))
r.Body = http.NoBody
r.Body = io.NopCloser(bytes.NewReader(reqBody))
}
}

// ImportTemplate import template
func ImportTemplate(crs *clusterResourcesService) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
Expand Down
15 changes: 15 additions & 0 deletions bcs-services/cluster-resources/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func LoadConf(filePath string) (*ClusterResourcesConf, error) {
conf.initJWTPubKey,
// 初始化 iam
conf.initIAM,
conf.initCompoment,
} {
if initErr := f(); initErr != nil {
return nil, initErr
Expand Down Expand Up @@ -223,6 +224,14 @@ func (c *ClusterResourcesConf) initIAM() error {
return nil
}

// initCompoment 初始化 compoment
func (c *ClusterResourcesConf) initCompoment() error {
if c.Global.Component.BCSStorageHost == "" {
c.Global.Component.BCSStorageHost = "https://bcs-storage:50024"
}
return nil
}

// EtcdConf Etcd 相关配置
type EtcdConf struct {
EtcdEndpoints string `yaml:"endpoints" usage:"Etcd Endpoints"`
Expand Down Expand Up @@ -307,11 +316,17 @@ type GlobalConf struct {
Auth AuthConf `yaml:"auth"`
Basic BasicConf `yaml:"basic"`
BCSAPIGW BCSAPIGatewayConf `yaml:"bcsApiGW"` // nolint:tagliatelle
Component ComponentConf `yaml:"component"`
IAM IAMConf `yaml:"iam"`
SharedCluster SharedClusterConf `yaml:"sharedCluster"`
MultiCluster MultiClusterConf `yaml:"multiCluster"`
}

// ComponentConf 组件配置
type ComponentConf struct {
BCSStorageHost string `yaml:"bcsStorageHost" usage:"BCS Storage Host"`
}

// AuthConf 认证相关配置
type AuthConf struct {
Disabled bool `yaml:"disabled" usage:"是否禁用身份认证"`
Expand Down

0 comments on commit 0edc608

Please sign in to comment.