Skip to content

Commit

Permalink
feature: cluster manager support ctyun deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Maclon9573 committed Oct 13, 2023
1 parent 5c1c816 commit 751a215
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,53 @@ func (c *CTClient) CreateCluster(req *CreateClusterRequest) (*CreateClusterReObj
return resp.ReturnObj, nil
}

// DeleteCluster deletes ECK cluster
func (c *CTClient) DeleteCluster(req *DeleteClusterReq) (*DeleteClusterReObj, error) {
reqPath := "/v2/cluster/delete"
resp := &DeleteClusterResponse{}

requestID, _ := uuid.NewUUID()
signDate := time.Now()
eopDate := signDate.Format("20060102T150405Z")
eopDt := signDate.Format("20060102")

reqBody, err := json.Marshal(req)
if err != nil {
return nil, err
}

result, respBody, errs := gorequest.New().
Timeout(defaultTimeOut).
Post(endingPoint+reqPath).
SetDebug(true).
Set("Content-Type", "application/json").
Set("User-Agent", "bcs-cluster-manager/v1.0").
Set("Eop-date", eopDate).
Set("ctyun-eop-request-id", requestID.String()).
Set("Eop-Authorization",
c.buildSignHeader(requestID.String(), eopDate, eopDt, string(reqBody), "")).
Send(string(reqBody)).
EndStruct(resp)

if len(errs) > 0 {
blog.Errorf("call DeleteCluster API failed: %v", errs[0])
return nil, errs[0]
}

if result.StatusCode != 200 {
errMsg := fmt.Errorf("call DeleteCluster API error: code[%d], %s",
result.StatusCode, string(respBody))
return nil, errMsg
}

if resp.StatusCode != "ok" {
blog.Errorf("DeleteCluster failed, %s", string(respBody))
return nil, fmt.Errorf("DeleteCluster failed, %s", string(respBody))
}

return resp.ReturnObj, nil
}

// GetKubeConfig gets kubeconfig
func (c *CTClient) GetKubeConfig(clusterId string) (*GetKubeConfigReObj, error) {
reqPath := "/v2/cluster/getKubeConfig"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,16 @@ type ContainerRuntime struct {
Version string `json:"version"`
}

type CommonResponse struct {
StatusCode string `json:"statusCode,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
}

// GetClusterResponse xxx
type GetClusterResponse struct {
StatusCode string `json:"statusCode,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
ReturnObj *GetClusterReObj `json:"returnObj,omitempty"`
CommonResponse
ReturnObj *GetClusterReObj `json:"returnObj,omitempty"`
}

// GetClusterReObj xxx
Expand Down Expand Up @@ -225,10 +229,8 @@ type KubeConfig struct {

// CreateClusterResponse xxx
type CreateClusterResponse struct {
StatusCode string `json:"statusCode,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
ReturnObj *CreateClusterReObj `json:"returnObj,omitempty"`
CommonResponse
ReturnObj *CreateClusterReObj `json:"returnObj,omitempty"`
}

// CreateClusterReObj xxx
Expand All @@ -238,11 +240,26 @@ type CreateClusterReObj struct {
TaskId string `json:"taskId"`
}

type DeleteClusterReq struct {
ClusterId string `json:"clusterId"`
ReservedLbIds []string `json:"reservedLbIds"`
ReservedNatIds []string `json:"reservedNatIds"`
ReservedSgId string `json:"reservedSgId"`
}

type DeleteClusterResponse struct {
CommonResponse
ReturnObj *DeleteClusterReObj `json:"returnObj,omitempty"`
}

type DeleteClusterReObj struct {
RequestId string `json:"requestId"`
TaskId string `json:"taskId"`
}

type GetKubeConfigResponse struct {
StatusCode string `json:"statusCode,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
ReturnObj *GetKubeConfigReObj `json:"returnObj,omitempty"`
CommonResponse
ReturnObj *GetKubeConfigReObj `json:"returnObj,omitempty"`
}

type GetKubeConfigReObj struct {
Expand All @@ -252,18 +269,16 @@ type GetKubeConfigReObj struct {
}

type ListNodeReq struct {
ClusterID string
NodePoolId string
Page uint32
PerPage uint32
NodeNames string
ClusterID string `json:"clusterID"`
NodeNames string `json:"nodeNames,omitempty"`
NodePoolId string `json:"nodePoolId,omitempty"`
Page uint32 `json:"page,omitempty"`
PerPage uint32 `json:"perPage,omitempty"`
}

type ListNodeResponse struct {
StatusCode string `json:"statusCode,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
ReturnObj *ListNodeReObj `json:"returnObj,omitempty"`
CommonResponse
ReturnObj *ListNodeReObj `json:"returnObj,omitempty"`
}

type ListNodeReObj struct {
Expand All @@ -287,10 +302,8 @@ type Node struct {
}

type GetNodePoolResponse struct {
StatusCode string `json:"statusCode,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
ReturnObj *GetNodePoolReObj `json:"returnObj"`
CommonResponse
ReturnObj *GetNodePoolReObj `json:"returnObj"`
}

type GetNodePoolReObj struct {
Expand Down Expand Up @@ -329,19 +342,17 @@ type Paging struct {
}

type ListNodePoolReq struct {
ClusterID string
NodePoolName string
Page uint32
PerPage uint32
EnableAutoScaling string
RetainSystemNodePool bool
ClusterID string `json:"clusterID"`
EnableAutoScaling string `json:"enableAutoScaling,omitempty"`
NodePoolName string `json:"nodePoolName,omitempty"`
Page uint32 `json:"page,omitempty"`
PerPage uint32 `json:"perPage,omitempty"`
RetainSystemNodePool bool `json:"retainSystemNodePool,omitempty"`
}

type ListNodePoolResponse struct {
StatusCode string `json:"statusCode,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
ReturnObj *ListNodePoolReObj `json:"returnObj"`
CommonResponse
ReturnObj *ListNodePoolReObj `json:"returnObj"`
}

type ListNodePoolReObj struct {
Expand Down Expand Up @@ -383,10 +394,8 @@ type NodeNum struct {
}

type ListVpcResponse struct {
StatusCode string `json:"statusCode,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
ReturnObj *ListVpcReObj `json:"returnObj,omitempty"`
CommonResponse
ReturnObj *ListVpcReObj `json:"returnObj,omitempty"`
}

type ListVpcReObj struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,33 @@ func (c Cluster) ImportCluster(cls *cmproto.Cluster, opt *cloudprovider.ImportCl

// DeleteCluster delete kubenretes cluster according cloudprovider
func (c Cluster) DeleteCluster(cls *cmproto.Cluster, opt *cloudprovider.DeleteClusterOption) (*cmproto.Task, error) {
return nil, cloudprovider.ErrCloudNotImplemented
if cls == nil {
return nil, fmt.Errorf("eopCloud DeleteCluster cluster is empty")
}

if opt == nil || opt.Account == nil || len(opt.Account.SecretID) == 0 ||
len(opt.Account.SecretKey) == 0 {
return nil, fmt.Errorf("eopCloud DeleteCluster cluster lost oprion")
}

mgr, err := cloudprovider.GetTaskManager(opt.Cloud.CloudProvider)
if err != nil {
blog.Errorf("get cloud %s TaskManager when DeleteCluster %d failed, %s",
opt.Cloud.CloudID, cls.ClusterName, err.Error(),
)
return nil, err
}

// build delete cluster task
task, err := mgr.BuildDeleteClusterTask(cls, opt)
if err != nil {
blog.Errorf("build DeleteCluster task for cluster %s with cloudprovider %s failed, %s",
cls.ClusterName, cls.Provider, err.Error(),
)
return nil, err
}

return task, nil
}

// DeleteVirtualCluster delete virtual cluster
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func newtask() *Task {
task.works[registerManageClusterKubeConfigStep.StepMethod] = tasks.RegisterClusterKubeConfigTask

// delete cluster task
//task.works[deleteECKClusterStep.StepMethod] = tasks.DeleteECKClusterTask
//task.works[cleanClusterDBInfoStep.StepMethod] = tasks.CleanClusterDBInfoTask
task.works[deleteECKClusterStep.StepMethod] = tasks.DeleteECKClusterTask
task.works[cleanClusterDBInfoStep.StepMethod] = tasks.CleanClusterDBInfoTask

return task
}
Expand Down Expand Up @@ -148,7 +148,54 @@ func (t Task) BuildImportClusterTask(

func (t Task) BuildDeleteClusterTask(
cls *proto.Cluster, opt *cloudprovider.DeleteClusterOption) (*proto.Task, error) {
return nil, cloudprovider.ErrCloudNotImplemented
// validate request params
if cls == nil {
return nil, fmt.Errorf("BuildDeleteClusterTask cluster info empty")
}
if opt == nil || opt.Operator == "" || opt.Cloud == nil || opt.Cluster == nil {
return nil, fmt.Errorf("BuildDeleteClusterTask TaskOptions is lost")
}

// init task information
nowStr := time.Now().Format(time.RFC3339)
task := &proto.Task{
TaskID: uuid.New().String(),
TaskType: cloudprovider.GetTaskType(cloudName, cloudprovider.DeleteCluster),
TaskName: cloudprovider.DeleteClusterTask.String(),
Status: cloudprovider.TaskStatusInit,
Message: "task initializing",
Start: nowStr,
Steps: make(map[string]*proto.Step),
StepSequence: make([]string, 0),
ClusterID: cls.ClusterID,
ProjectID: cls.ProjectID,
Creator: opt.Operator,
Updater: opt.Operator,
LastUpdate: nowStr,
CommonParams: make(map[string]string),
ForceTerminate: false,
}
taskName := fmt.Sprintf(deleteClusterTaskTemplate, cls.ClusterID)
task.CommonParams[cloudprovider.TaskNameKey.String()] = taskName
task.CommonParams[cloudprovider.OperatorKey.String()] = opt.Operator

// setting all steps details
deleteCluster := &DeleteClusterTaskOption{
Cluster: cls,
}
// step1: deleteECKClusterTask delete eck cluster
deleteCluster.BuildDeleteECKClusterStep(task)
// step2: update cluster DB info and associated data
deleteCluster.BuildCleanClusterDBInfoStep(task)

// set current step
if len(task.StepSequence) == 0 {
return nil, fmt.Errorf("BuildDeleteClusterTask task StepSequence empty")
}
task.CurrentStep = task.StepSequence[0]
task.CommonParams[cloudprovider.JobTypeKey.String()] = cloudprovider.DeleteClusterJob.String()

return task, nil
}

func (t Task) BuildAddNodesToClusterTask(
Expand Down
Loading

0 comments on commit 751a215

Please sign in to comment.