-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bk-bscp): access blueking notification center (#2913)
* feat(bk-bscp): access blueking notification center * feat(bk-bscp): access blueking notification center * feat(bk-bscp): access blueking notification center
- Loading branch information
1 parent
3a123a8
commit fc3f9b7
Showing
9 changed files
with
293 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making Blueking Container Service available. | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* http://opensource.org/licenses/MIT | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Package cmd provides operations for init and upgrading the bk-apigateway resources and | ||
// register the system 'bk-bscp' into bk-notice | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/cc" | ||
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/components/bknotice" | ||
) | ||
|
||
var migrateCmd = &cobra.Command{ | ||
Use: "migrate", | ||
Short: "api-server migrations tool", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
}, | ||
} | ||
|
||
var migrateInitNoticeCmd = &cobra.Command{ | ||
Use: "init-notice", | ||
Short: "Register system 'bk-bscp' into bk-notice", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
if err := cc.LoadSettings(SysOpt.Sys); err != nil { | ||
fmt.Println("load settings from config files failed, err:", err) | ||
return | ||
} | ||
|
||
if !cc.ApiServer().BKNotice.Enable { | ||
fmt.Println("bknotice is disabled, skip init") | ||
return | ||
} | ||
|
||
if err := bknotice.RegisterSystem(context.Background()); err != nil { | ||
fmt.Println("register system to bknotice failed, err:", err) | ||
return | ||
} | ||
}, | ||
} | ||
|
||
var migrateInitApigatewayCmd = &cobra.Command{ | ||
Use: "init-apigateway", | ||
Short: "Create 'bk-bscp' apigateway instance and upgrade api resources", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
if err := cc.LoadSettings(SysOpt.Sys); err != nil { | ||
fmt.Println("load settings from config files failed, err:", err) | ||
return | ||
} | ||
|
||
fmt.Println("Need to be implemented") | ||
}, | ||
} | ||
|
||
func init() { | ||
|
||
// Add "--debug" flag to all migrate sub commands | ||
migrateCmd.PersistentFlags().BoolP("debug", "d", false, | ||
"whether to debug output the execution process,, default is false") | ||
|
||
migrateCmd.AddCommand(migrateInitNoticeCmd) | ||
migrateCmd.AddCommand(migrateInitApigatewayCmd) | ||
|
||
// Add "migrate" command to the root command | ||
rootCmd.AddCommand(migrateCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making Blueking Container Service available. | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* http://opensource.org/licenses/MIT | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/cmd/api-server/app" | ||
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/cmd/api-server/options" | ||
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/cc" | ||
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/logs" | ||
) | ||
|
||
// SysOpt is the system option | ||
var SysOpt *options.Option | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "bk-bscp-apiserver", | ||
Short: "BSCP ApiServer", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
if err := app.Run(SysOpt); err != nil { | ||
fmt.Fprintf(os.Stderr, "start api server failed, err: %v", err) | ||
logs.CloseLogs() | ||
os.Exit(1) | ||
} | ||
|
||
}, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
cobra.CheckErr(rootCmd.Execute()) | ||
} | ||
|
||
func init() { | ||
|
||
rootCmd.CompletionOptions.DisableDefaultCmd = true | ||
|
||
SysOpt = options.InitOptions() | ||
|
||
cc.InitService(cc.APIServerName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making Blueking Container Service available. | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* http://opensource.org/licenses/MIT | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package service | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/cc" | ||
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/config" | ||
) | ||
|
||
// bkNoticeService is http handler for bknotice service. | ||
type bkNoticeService struct { | ||
} | ||
|
||
// GetCurrentAnnouncements get current announcements | ||
func (s *bkNoticeService) GetCurrentAnnouncements(w http.ResponseWriter, r *http.Request) { | ||
// Prepare the new request | ||
|
||
proxyURL := fmt.Sprintf("%s/v1/announcement/get_current_announcements/?platform=%s", | ||
config.G.Base.AppCode, cc.ApiServer().BKNotice.Host) | ||
|
||
proxyReq, err := http.NewRequest("GET", proxyURL, nil) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
authHeader := fmt.Sprintf("{\"bk_app_code\": \"%s\", \"bk_app_secret\": \"%s\"}", | ||
config.G.Base.AppCode, config.G.Base.AppSecret) | ||
|
||
proxyReq.Header.Set("X-Bkapi-Authorization", authHeader) | ||
|
||
// Send the request to the target API | ||
client := &http.Client{} | ||
resp, err := client.Do(proxyReq) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadGateway) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
// Copy the response headers and status code | ||
for key, values := range resp.Header { | ||
for _, value := range values { | ||
w.Header().Add(key, value) | ||
} | ||
} | ||
w.WriteHeader(resp.StatusCode) | ||
|
||
_, err = io.Copy(w, resp.Body) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
func newBKNoticeService() (*bkNoticeService, error) { | ||
|
||
service := &bkNoticeService{} | ||
|
||
return service, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making Blueking Container Service available. | ||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* http://opensource.org/licenses/MIT | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
* either express or implied. See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Package bknotice provides bknotice client. | ||
package bknotice | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/cc" | ||
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/components" | ||
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/config" | ||
) | ||
|
||
type registerSystemResp struct { | ||
Result bool `json:"result"` | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
} | ||
|
||
// RegisterSystem 注册系统到通知中心 | ||
func RegisterSystem(ctx context.Context) error { | ||
url := fmt.Sprintf("%s/v1/register/", cc.ApiServer().BKNotice.Host) | ||
|
||
authHeader := fmt.Sprintf("{\"bk_app_code\": \"%s\", \"bk_app_secret\": \"%s\"}", | ||
config.G.Base.AppCode, config.G.Base.AppSecret) | ||
|
||
resp, err := components.GetClient().R(). | ||
SetContext(ctx). | ||
SetHeader("X-Bkapi-Authorization", authHeader). | ||
Post(url) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
resigerResp := ®isterSystemResp{} | ||
if err := json.Unmarshal(resp.Body(), resigerResp); err != nil { | ||
return err | ||
} | ||
|
||
if resigerResp.Code != 0 { | ||
return fmt.Errorf("register system to bknotice failed, code: %d, message: %s", | ||
resigerResp.Code, resigerResp.Message) | ||
} | ||
return nil | ||
} |