Skip to content

Commit

Permalink
feature: cluster manager support ctyun creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Maclon9573 committed Oct 11, 2023
1 parent 93acd2a commit c2e095c
Show file tree
Hide file tree
Showing 25 changed files with 7,092 additions and 3,968 deletions.
7,905 changes: 3,968 additions & 3,937 deletions bcs-services/bcs-cluster-manager/api/clustermanager/clustermanager.pb.go

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3301,19 +3301,27 @@ message InstanceTemplateConfig {
title: "imageInfo",
description: "新实例启动时的配置镜像信息"
}];
string initLoginPassword = 14[(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {
string nodeRole = 14[(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {
title: "nodeRole",
description: "新实例节点角色,取值:MASTER_ETCD, WORKER"
}];
string initLoginUsername = 15[(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {
title: "initLoginUsername",
description: "新实例登录用户名"
}];
string initLoginPassword = 16[(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {
title: "initLoginPassword",
description: "新实例初始设置password,如果不设置系统自动生成"
}];
repeated string securityGroupIDs = 15[(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {
repeated string securityGroupIDs = 17[(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {
title: "securityGroupIDs",
description: "新实例安全组关联的ID列表,如果不填写,使用系统默认的安全组信息"
}];
bool isSecurityService = 16[(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {
bool isSecurityService = 18[(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {
title: "isSecurityService",
description: "新实例启动时的是否开启云安全, 选填 qcloud默认开启"
}];
bool isMonitorService = 17[(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {
bool isMonitorService = 19[(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {
title: "isMonitorService",
description: "新实例启动时的是否开启云监控, 选填 默认开启"
}];
Expand Down Expand Up @@ -4685,6 +4693,10 @@ message CreateClusterReq {
title: "nodeTemplateID",
description: "创建集群上架节点时的节点模板"
}];
repeated NodeGroup nodeGroups = 41[(grpc.gateway.protoc_gen_swagger.options.openapiv2_field) = {
title: "nodeGroups",
description: "创建集群时的节点池信息"
}];
}

message CreateClusterResp {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8527,6 +8527,14 @@
"type": "string",
"description": "创建集群上架节点时的节点模板",
"title": "nodeTemplateID"
},
"nodeGroups": {
"type": "array",
"items": {
"$ref": "#/definitions/clustermanagerNodeGroup"
},
"description": "创建集群时的节点池信息",
"title": "nodeGroups"
}
},
"description": "创建集群请求,如果集群已经存在,直接报错。更新集群信息请使用Update接口",
Expand Down Expand Up @@ -11408,6 +11416,16 @@
"description": "新实例启动时的配置镜像信息",
"title": "imageInfo"
},
"nodeRole": {
"type": "string",
"description": "新实例节点角色,取值:MASTER_ETCD, WORKER",
"title": "nodeRole"
},
"initLoginUsername": {
"type": "string",
"description": "新实例登录用户名",
"title": "initLoginUsername"
},
"initLoginPassword": {
"type": "string",
"description": "新实例初始设置password,如果不设置系统自动生成",
Expand Down
89 changes: 62 additions & 27 deletions bcs-services/bcs-cluster-manager/internal/actions/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,36 +254,26 @@ func (ca *CreateAction) validate() error {
if err := ca.req.Validate(); err != nil {
return err
}
// kubernetes version
if len(ca.req.ClusterBasicSettings.Version) == 0 {
return fmt.Errorf("lost kubernetes version in request")
}

// check masterIP
if ca.req.ManageType == common.ClusterManageTypeIndependent && len(ca.req.Master) == 0 {
return fmt.Errorf("lost kubernetes cluster masterIP")
// cloud validate
cloudValidate, err := cloudprovider.GetCloudValidateMgr(ca.cloud.CloudProvider)
if err != nil {
return err
}

// default not handle systemReinstall
ca.req.SystemReinstall = true

// auto generate master nodes
if ca.req.AutoGenerateMasterNodes && len(ca.req.Instances) == 0 {
return fmt.Errorf("invalid instanceTemplate config when AutoGenerateMasterNodes=true")
// first, get cloud credentialInfo from project; second, get from cloud provider when failed to obtain
cOption, err := cloudprovider.GetCredential(&cloudprovider.CredentialData{
Cloud: ca.cloud,
AccountID: ca.req.CloudAccountID,
})
if err != nil {
blog.Errorf("Get Credential failed from Cloud %s: %s",
ca.cloud.CloudID, err.Error())
return err
}
cOption.Region = ca.req.Region

// use existed instances
if !ca.req.AutoGenerateMasterNodes {
switch ca.req.ManageType {
case common.ClusterManageTypeManaged:
if len(ca.req.Nodes) == 0 {
return fmt.Errorf("invalid node config when AutoGenerateMasterNodes false in MANAGED_CLUSTER")
}
default:
if len(ca.req.Master) == 0 {
return fmt.Errorf("invalid master config when AutoGenerateMasterNodes false in INDEPENDENT_CLUSTER")
}
}
err = cloudValidate.CreateClusterValidate(ca.req, cOption)
if err != nil {
return err
}

// masterIP check
Expand Down Expand Up @@ -378,6 +368,37 @@ func (ca *CreateAction) generateClusterID(cls *cmproto.Cluster) error {
return nil
}

func (ca *CreateAction) createNodegroup(cls *cmproto.Cluster) error {
timeStr := time.Now().Format(time.RFC3339)
if ca.req.NodeGroups != nil {
for _, ng := range ca.req.NodeGroups {
ng.NodeGroupID = fmt.Sprintf("BCS-ng-%s", utils.RandomString(8))
ng.Region = cls.Region
ng.ClusterID = cls.ClusterID
ng.ProjectID = cls.ProjectID
ng.Provider = cls.Provider
ng.Status = common.StatusCreateNodeGroupCreating
ng.CreateTime = timeStr
ng.UpdateTime = timeStr
err := ca.model.CreateNodeGroup(context.Background(), ng)
if err != nil {
blog.Errorf("save NodeGroup %s information to store failed, %s", ng.NodeGroupID, err.Error())
if errors.Is(err, drivers.ErrTableRecordDuplicateKey) {
ca.resp.Data = cls
ca.setResp(common.BcsErrClusterManagerDatabaseRecordDuplicateKey, err.Error())
return err
}
//other db operation error
ca.resp.Data = cls
ca.setResp(common.BcsErrClusterManagerDBOperation, err.Error())
return err
}
}
}

return nil
}

// Handle create cluster request
func (ca *CreateAction) Handle(ctx context.Context, req *cmproto.CreateClusterReq, resp *cmproto.CreateClusterResp) {
if req == nil || resp == nil {
Expand Down Expand Up @@ -430,6 +451,12 @@ func (ca *CreateAction) Handle(ctx context.Context, req *cmproto.CreateClusterRe
return
}

err = ca.createNodegroup(cls)
if err != nil {
blog.Errorf("createNodegroup failed: %v", err)
return
}

// import cluster nodes
_ = ca.checkClusterWorkerNodes(cls)

Expand Down Expand Up @@ -513,6 +540,13 @@ func (ca *CreateAction) createClusterTask(ctx context.Context, cls *cmproto.Clus
}
coption.Region = ca.req.Region

var nodeGroupIDs []string
if ca.req.NodeGroups != nil {
for _, ng := range ca.req.NodeGroups {
nodeGroupIDs = append(nodeGroupIDs, ng.NodeGroupID)
}
}

// create cluster task by task manager
task, err := provider.CreateCluster(cls, &cloudprovider.CreateClusterOption{
CommonOption: *coption,
Expand All @@ -521,6 +555,7 @@ func (ca *CreateAction) createClusterTask(ctx context.Context, cls *cmproto.Clus
Operator: ca.req.Creator,
Cloud: ca.cloud,
Nodes: ca.req.Nodes,
NodeGroupIDs: nodeGroupIDs,
NodeTemplate: func() *cmproto.NodeTemplate {
if ca.req.NodeTemplateID == "" {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func init() {
type CloudValidate struct {
}

// CreateClusterValidate create cluster validate
func (c *CloudValidate) CreateClusterValidate(req *proto.CreateClusterReq, opt *cloudprovider.CommonOption) error {
return cloudprovider.ErrCloudNotImplemented
}

// CreateCloudAccountValidate create cloud account validate
func (c *CloudValidate) CreateCloudAccountValidate(account *proto.Account) error {
return cloudprovider.ErrCloudNotImplemented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package blueking
import (
"encoding/base64"
"fmt"
"github.com/Tencent/bk-bcs/bcs-services/bcs-cluster-manager/internal/common"
"sync"

"github.com/Tencent/bk-bcs/bcs-common/common/blog"
Expand All @@ -38,6 +39,46 @@ func init() {
type CloudValidate struct {
}

// CreateClusterValidate create cluster validate
func (c *CloudValidate) CreateClusterValidate(req *proto.CreateClusterReq, opt *cloudprovider.CommonOption) error {
// kubernetes version
if len(req.ClusterBasicSettings.Version) == 0 {
return fmt.Errorf("%s CreateClusterValidate lost kubernetes version in request", cloudName)
}

// check masterIP
if req.ManageType == common.ClusterManageTypeIndependent && len(req.Master) == 0 {
return fmt.Errorf("%s CreateClusterValidate lost kubernetes cluster masterIP", cloudName)
}

// default not handle systemReinstall
req.SystemReinstall = true

// auto generate master nodes
if req.AutoGenerateMasterNodes && len(req.Instances) == 0 {
return fmt.Errorf("%s CreateClusterValidate invalid instanceTemplate config "+
"when AutoGenerateMasterNodes=true", cloudName)
}

// use existed instances
if !req.AutoGenerateMasterNodes {
switch req.ManageType {
case common.ClusterManageTypeManaged:
if len(req.Nodes) == 0 {
return fmt.Errorf("%s CreateClusterValidate invalid node config "+
"when AutoGenerateMasterNodes false in MANAGED_CLUSTER", cloudName)
}
default:
if len(req.Master) == 0 {
return fmt.Errorf("%s CreateClusterValidate invalid master config "+
"when AutoGenerateMasterNodes false in INDEPENDENT_CLUSTER", cloudName)
}
}
}

return nil
}

// CreateCloudAccountValidate create cloud account validate
func (c *CloudValidate) CreateCloudAccountValidate(account *proto.Account) error {
return cloudprovider.ErrCloudNotImplemented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ var (
// FailedClusterNodeIDsKey xxx
FailedClusterNodeIDsKey ParamKey = "failedClusterNodeIDs"

// SuccessNodeGroupIDsKey xxx
SuccessNodeGroupIDsKey ParamKey = "successNodeGroupIDs"
// FailedNodeGroupIDsKey xxx
FailedNodeGroupIDsKey ParamKey = "failedNodeGroupIDs"

// JobTypeKey xxx
JobTypeKey ParamKey = "jobType"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 api

const (
// ClusterStatusRunning running
ClusterStatusRunning = "K8S_CLUSTER_STATE_RUNNING"
// ClusterStatusInitial initial
ClusterStatusInitial = "K8S_CLUSTER_STATE_INITIAL"
// ClusterStatusCreateFailed create failed
ClusterStatusCreateFailed = "K8S_CLUSTER_STATE_CREATE_FAILED"

// NodePoolStatusInitial nodepool initial state
NodePoolStatusInitial = "NODE_POOL_STATE_INITIAL"
// NodePoolStatusCreateFailed nodepool create failed state
NodePoolStatusCreateFailed = "NODE_POOL_STATE_CREATE_FAILED"
// NodePoolStatusActive nodepool active state
NodePoolStatusActive = "NODE_POOL_STATE_ACTIVATED"

// NodeStatusRunning node running state
NodeStatusRunning = "NODE_STATE_RUNNING"
// NodeStatusUnknown node unknown state
NodeStatusUnknown = "NODE_STATE_UNKNOWN"
)
Loading

0 comments on commit c2e095c

Please sign in to comment.