Skip to content

Commit

Permalink
feat: update bcs cc sync nodes (Tencent#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
abner authored Sep 9, 2020
1 parent 60412e9 commit c69e62f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
9 changes: 6 additions & 3 deletions bcs-cc/components/auth/authtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,19 @@ func GetAccessToken() (string, error) {
"id_provider": "client",
"grant_type": "client_credentials",
"env_name": "prod",
"app_code": confBase.CCAppCode,
"app_secret": confBase.CCAppSecret,
}
headers := map[string]string{
"Content-Type": "application/json",
"X-BK-APP-CODE": confBase.CCAppCode,
"X-BK-APP-SECRET": confBase.CCAppSecret,
}
// compose the request
req := utils.GoReq{
Method: "POST",
URL: reqURL,
Params: "",
Data: reqData,
Header: map[string]string{"Content-Type": "application/json"},
Header: headers,
Timeout: time.Duration(10),
}
body, err := utils.RequestClient(req, authConf.Proxy)
Expand Down
29 changes: 19 additions & 10 deletions bcs-cc/components/bcs/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"bcs_cc/utils"
)

var defaultClusterState = "bcs_new"

type agentClusterIDResp struct {
ID string `json:"id"`
Message string `json:"message"`
Expand Down Expand Up @@ -57,7 +59,9 @@ type nodeListResp struct {

func queryK8sAgentClusterID(projectID string, clusterID string, env string, accessToken string) (string, error) {
confBase := config.GlobalConfigurations
reqURL := fmt.Sprintf("%s/%s%s", confBase.APIGWConf.Host, env, confBase.BCSConf.K8sQueryClusterIDPath)
// 直接连接bcs api
host, _ := confBase.BCSConf.Hosts[env]
reqURL := fmt.Sprintf("%s%s", host, confBase.BCSConf.K8sQueryClusterIDPath)
params := fmt.Sprintf("access_token=%s&project_id=%s&cluster_id=%s", accessToken, projectID, clusterID)
req := utils.GoReq{
Method: "GET",
Expand Down Expand Up @@ -87,7 +91,8 @@ func queryK8sAgentClusterID(projectID string, clusterID string, env string, acce
func queryCredentials(agentClusterID string, env string, accessToken string) (string, error) {
confBase := config.GlobalConfigurations
reqPath := fmt.Sprintf(confBase.BCSConf.K8sQueryCredentialsPath, agentClusterID)
reqURL := fmt.Sprintf("%s/%s%s", confBase.APIGWConf.Host, env, reqPath)
host, _ := confBase.BCSConf.Hosts[env]
reqURL := fmt.Sprintf("%s%s", host, reqPath)
params := fmt.Sprintf("access_token=%s", accessToken)
req := utils.GoReq{
Method: "GET",
Expand Down Expand Up @@ -116,11 +121,15 @@ func queryCredentials(agentClusterID string, env string, accessToken string) (st
}

// compose the node list, include ip and status
func composeNodeList(resp nodeListResp) (nodeList []map[string]string) {
func composeNodeList(resp nodeListResp, clusterState string) (nodeList []map[string]string) {
isBCSNEWCluster := true
if clusterState != defaultClusterState {
isBCSNEWCluster = false
}
for _, info := range resp.Items {
// only node/slave info
role := info.Metadata.Labels["node-role.kubernetes.io/node"]
if role != "true" {
// 针对平台创建集群,获取角色为node的节点,进行保存
nodeRole := info.Metadata.Labels["node-role.kubernetes.io/node"]
if nodeRole != "true" && isBCSNEWCluster {
continue
}
innerIP := ""
Expand Down Expand Up @@ -149,7 +158,7 @@ func composeNodeList(resp nodeListResp) (nodeList []map[string]string) {
return nodeList
}

func queryNodes(agentClusterID string, serverPath string, env string, accessToken string) ([]map[string]string, error) {
func queryNodes(agentClusterID string, serverPath string, env string, accessToken string, clusterState string) ([]map[string]string, error) {
confBase := config.GlobalConfigurations
host := confBase.BCSConf.Hosts[env]
serverPathTrim := strings.Trim(serverPath, "/")
Expand Down Expand Up @@ -182,11 +191,11 @@ func queryNodes(agentClusterID string, serverPath string, env string, accessToke
logging.Error("parse bcs response error, cluster_id: %s, detial: %v", agentClusterID, err)
return nil, err
}
return composeNodeList(resp), nil
return composeNodeList(resp, clusterState), nil
}

// K8sIPResource :
func K8sIPResource(projectID string, clusterID string, env string, accessToken string) ([]map[string]string, error) {
func K8sIPResource(projectID string, clusterID string, env string, accessToken string, clusterState string) ([]map[string]string, error) {
// query k8s agent registered cluster id
agentClusterID, err := queryK8sAgentClusterID(projectID, clusterID, env, accessToken)
if err != nil {
Expand All @@ -197,5 +206,5 @@ func K8sIPResource(projectID string, clusterID string, env string, accessToken s
if err != nil {
return nil, err
}
return queryNodes(agentClusterID, serverPath, env, accessToken)
return queryNodes(agentClusterID, serverPath, env, accessToken, clusterState)
}
16 changes: 13 additions & 3 deletions bcs-cc/confcenter/tasks/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ func getClusterInfo(projectID string, clusterID string) ([]map[string]string, st
clusterType = info.Type
creator = info.Creator
clusterList = append(clusterList, map[string]string{
"cluster_id": info.ClusterID, "env": info.Environment, "type": info.Type})
"cluster_id": info.ClusterID,
"env": info.Environment,
"type": info.Type,
"state": info.State,
})
}
}
} else {
Expand All @@ -72,7 +76,11 @@ func getClusterInfo(projectID string, clusterID string) ([]map[string]string, st
clusterType = clusterRecord.Type
creator = clusterRecord.Creator
clusterList = append(clusterList, map[string]string{
"cluster_id": clusterRecord.ClusterID, "env": clusterRecord.Environment, "type": clusterRecord.Type})
"cluster_id": clusterRecord.ClusterID,
"env": clusterRecord.Environment,
"type": clusterRecord.Type,
"state": clusterRecord.State,
})
}
}

Expand Down Expand Up @@ -191,7 +199,9 @@ func handleK8sNodes(projectID string, clusterList []map[string]string, accessTok
if err != nil {
return err
}
ipResource, err := bcs.K8sIPResource(projectID, info["cluster_id"], bcsEnv[info["env"]], accessToken)
ipResource, err := bcs.K8sIPResource(
projectID, info["cluster_id"], bcsEnv[info["env"]], accessToken, info["clusterState"],
)
if err != nil {
return err
}
Expand Down

0 comments on commit c69e62f

Please sign in to comment.