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

fix(bcs-bscp): enable update system in iam #2935

Merged
merged 2 commits into from
Feb 1, 2024
Merged
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
5 changes: 5 additions & 0 deletions bcs-services/bcs-bscp/cmd/api-server/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/cc"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/components/bknotice"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/logs"
)

var migrateCmd = &cobra.Command{
Expand All @@ -42,6 +43,8 @@ var migrateInitNoticeCmd = &cobra.Command{
return
}

logs.InitLogger(cc.ApiServer().Log.Logs())

if !cc.ApiServer().BKNotice.Enable {
fmt.Println("bknotice is disabled, skip init")
return
Expand All @@ -64,6 +67,8 @@ var migrateInitApigatewayCmd = &cobra.Command{
return
}

logs.InitLogger(cc.ApiServer().Log.Logs())

fmt.Println("Need to be implemented")
},
}
Expand Down
2 changes: 2 additions & 0 deletions bcs-services/bcs-bscp/cmd/auth-server/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ var migrateInitCmd = &cobra.Command{
return
}

logs.InitLogger(cc.AuthServer().Log.Logs())

iamSys, err := NewIamSys()
if err != nil {
fmt.Printf("new iam sys failed, err: %v\n", err)
Expand Down
7 changes: 7 additions & 0 deletions bcs-services/bcs-bscp/cmd/data-service/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
_ "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/cmd/data-service/db-migration/migrations"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/cmd/data-service/db-migration/migrator"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/cc"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/logs"
)

// cmd for migration
Expand Down Expand Up @@ -89,6 +90,8 @@ var migrateUpCmd = &cobra.Command{
return
}

logs.InitLogger(cc.DataService().Log.Logs())

db, err = migrator.NewDB(debug)
if err != nil {
fmt.Println("Unable to new db migrator, err:", err)
Expand Down Expand Up @@ -139,6 +142,8 @@ var migrateDownCmd = &cobra.Command{
return
}

logs.InitLogger(cc.DataService().Log.Logs())

db, err = migrator.NewDB(debug)
if err != nil {
fmt.Println("Unable to new db migrator, err:", err)
Expand Down Expand Up @@ -182,6 +187,8 @@ var migrateStatusCmd = &cobra.Command{
return
}

logs.InitLogger(cc.DataService().Log.Logs())

db, err = migrator.NewDB(debug)
if err != nil {
fmt.Println("Unable to new db migrator, err:", err)
Expand Down
8 changes: 2 additions & 6 deletions bcs-services/bcs-bscp/pkg/iam/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,8 @@ func (c *Client) GetSystemInfo(ctx context.Context, fields []SystemQueryField) (
return resp, nil
}

// UpdateSystemConfig update system config in IAM
// Note: can only update provider_config.host field.
func (c *Client) UpdateSystemConfig(ctx context.Context, config *SysConfig) error {
sys := new(System)
config.Auth = "basic"
sys.ProviderConfig = config
// UpdateSystem update system in IAM
func (c *Client) UpdateSystem(ctx context.Context, sys System) error {
resp := new(BaseResponse)
result := c.client.Put().
SubResourcef("/api/v1/model/systems/%s", c.config.SystemID).
Expand Down
41 changes: 15 additions & 26 deletions bcs-services/bcs-bscp/pkg/iam/sys/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,19 @@ func (s *Sys) registerSystem(ctx context.Context, host string) (*client.Register
return nil, err
}

sys := client.System{
ID: SystemIDBSCP,
Name: SystemNameBSCP,
EnglishName: SystemNameBSCPEn,
Clients: SystemIDBSCP,
ProviderConfig: &client.SysConfig{
Host: host,
Auth: "basic",
},
}

// if iam bscp system has not been registered, register system
if err == client.ErrNotFound {
sys := client.System{
ID: SystemIDBSCP,
Name: SystemNameBSCP,
EnglishName: SystemNameBSCPEn,
Clients: SystemIDBSCP,
ProviderConfig: &client.SysConfig{
Host: host,
Auth: "basic",
},
}

if err = s.client.RegisterSystem(ctx, sys); err != nil {
logs.Errorf("register system failed, system: %v, err: %v", sys, err)
Expand All @@ -357,26 +358,14 @@ func (s *Sys) registerSystem(ctx context.Context, host string) (*client.Register
logs.Infof("register new system succeed, system: %v", sys)
}

} else if resp.Data.BaseInfo.ProviderConfig == nil || resp.Data.BaseInfo.ProviderConfig.Host != host {
// if iam registered bscp system has no ProviderConfig
// or registered host config is different with current host config, update system host config
if err = s.client.UpdateSystemConfig(ctx, &client.SysConfig{Host: host}); err != nil {
} else {
// else update bscp system in iam
if err = s.client.UpdateSystem(ctx, sys); err != nil {
logs.Errorf("update system host config failed, host: %s, err: %v", host, err)
return nil, err
}

if resp.Data.BaseInfo.ProviderConfig == nil {
if logs.V(5) {
logs.Infof("update system host succeed, new: %s", host)
}

} else {
if logs.V(5) {
logs.Infof("update system host succeed, old: %s, new: %s", resp.Data.BaseInfo.
ProviderConfig.Host, host)
}

}
logs.V(5).Infof("update system host succeed, old: %s, new: %s", resp.Data.BaseInfo.ProviderConfig.Host, host)
}

return &resp.Data, nil
Expand Down
48 changes: 43 additions & 5 deletions bcs-services/bcs-bscp/pkg/rest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,42 @@ func (r *Request) tryThrottle(url string) {
}
}

func toCurlCommand(req *http.Request, body []byte) string {
var command []string
command = append(command, "curl")
command = append(command, fmt.Sprintf("'%s'", req.URL))
command = append(command, fmt.Sprintf("-X %s", req.Method))

for header, values := range req.Header {
for _, value := range values {
command = append(command, fmt.Sprintf("-H '%s: %s'", header, value))
}
}

if len(body) > 0 {
command = append(command, fmt.Sprintf("-d '%s'", strings.ReplaceAll(string(body), "'", "'\"'\"'")))
}

return strings.Join(command, " ")
}

func toCurlResponse(resp *http.Response, body []byte) string {
var responseHeaders []string
for header, values := range resp.Header {
for _, value := range values {
responseHeaders = append(responseHeaders, fmt.Sprintf("%s: %s", header, value))
}
}
responseHeaderStr := strings.Join(responseHeaders, "\n")

return fmt.Sprintf("HTTP/1.1 %d %s\n%s\n\n%s",
resp.StatusCode,
http.StatusText(resp.StatusCode),
responseHeaderStr,
string(body),
)
}

// Do http request do.
//
//nolint:funlen
Expand Down Expand Up @@ -363,6 +399,8 @@ func (r *Request) Do() *Result {
r.tryThrottle(urlString)
}

logs.V(4).Info(toCurlCommand(req, r.body))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里已经封装,看能否复用


start := time.Now()
resp, err := client.Do(req)

Expand All @@ -383,6 +421,11 @@ func (r *Request) Do() *Result {
continue
}

respBody, _ := io.ReadAll(resp.Body)
logs.V(4).Info(toCurlResponse(resp, respBody))

resp.Body = io.NopCloser(bytes.NewBuffer(respBody))

// collect request metrics
if r.client.requestDuration != nil {
labels := prometheus.Labels{
Expand Down Expand Up @@ -415,11 +458,6 @@ func (r *Request) Do() *Result {
body = data
}

if logs.V(4) {
logs.Infof("http request cost: %dms, %s %s with body %s, response status: %s, response body: %s, rid: "+
"%s", time.Since(start)/time.Millisecond, string(r.verb), urlString, r.body, resp.Status, body, rid)
}

result.Body = body
result.StatusCode = resp.StatusCode
result.Status = resp.Status
Expand Down
Loading