1. 示例简介
华为云提供了云容器引擎 CCE SDK,您可以直接集成SDK来调用相关API,从而实现对云容器引擎服务的快速操作。
一个CCE集群可以管理一组节点资源。该示例展示了如何在云容器引擎服务为指定集群创建节点,并等待节点创建成功。
注意:示例中创建的节点为按需计费,如不再使用请及时删除节点或集群。
3. 安装SDK
云容器引擎 CCE SDK支持go 1.14及以上版本。执行go version检查当前Go的版本信息。
使用go get安装华为云Go SDK,执行如下命令安装华为云Go SDK库以及相关依赖库,具体的SDK版本号请参见SDK开发中心。
Copied!
# 安装华为云Go库
go get github.com/huaweicloud/huaweicloud-sdk-go-v3
4. 代码示例
以下代码展示如何为CCE集群创建节点:
Copied!
package main
import (
"encoding/json"
"fmt"
"time"
"os"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
cce "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/model"
region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/region"
)
func main() {
ak := os.Getenv("HUAWEICLOUD_SDK_AK")
sk := os.Getenv("HUAWEICLOUD_SDK_SK")
projectID := "<YOUR PROJECT ID>"
userRegion := "<YOUR REGION>"
clusterID := "<YOUR CLUSTER ID>"
az := "<YOUR NODE AZ>"
flavor := "<YOUR NODE FLAVOR>"
sshKey := "<YOUR SSH KEY>"
fmt.Printf("This is a demo to create node in cluster.\n")
data := "{\"kind\":\"Node\",\"apiVersion\":\"v3\",\"spec\":{\"rootVolume\":{\"size\":40,\"volumetype\":\"SATA\"},\"dataVolumes\":[{\"size\":100,\"volumetype\":\"SATA\"}],\"billingMode\":0,\"count\":1}}"
auth := basic.NewCredentialsBuilder().
WithAk(ak).
WithSk(sk).
WithProjectId(projectID).
Build()
fmt.Printf("Build auth successfully.\n")
client := cce.NewCceClient(
cce.CceClientBuilder().
WithRegion(region.ValueOf(userRegion)).
WithCredential(auth).
Build())
fmt.Printf("Build client successfully.\n")
request := &model.CreateNodeRequest{}
request.ClusterId = clusterID
request.Body = &model.NodeCreateRequest{}
json.Unmarshal([]byte(data), request.Body)
request.Body.Spec.Az = az
request.Body.Spec.Flavor = flavor
request.Body.Spec.Login = &model.Login{
SshKey: &sshKey,
}
response, err := client.CreateNode(request)
if err == nil {
fmt.Printf("Start to create node...\n")
} else {
fmt.Printf("Create node failed...\n")
fmt.Printf("Error: %+v\n", err)
return
}
if response != nil && response.Status != nil && response.Status.JobID != nil {
waitNodeReady(client, *response.Status.JobID)
} else {
fmt.Printf("Get node state failed...\n")
}
}
func waitNodeReady(c *cce.CceClient, jobID string) {
req := &model.ShowJobRequest{
JobId: jobID,
}
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
timeout := time.After(30 * time.Minute)
for {
select {
case <-timeout:
fmt.Printf("Wait node state timed out.")
return
case <-ticker.C:
resp, err := c.ShowJob(req)
if err != nil {
fmt.Printf("Show create node job failed...\n")
fmt.Printf("Error: %+v\n", err)
return
}
if resp != nil && resp.Status != nil && resp.Status.Phase != nil {
fmt.Printf("Wait node ready, job is %s\n", *resp.Status.Phase)
if *resp.Status.Phase == "Success" {
fmt.Printf("Create node successfully!")
return
} else if *resp.Status.Phase == "Failed" {
fmt.Printf("Create node failed!")
return
}
}
}
}
}
0. 版本说明
本示例基于华为云SDK V3.0版本开发。
1. 示例简介
华为云提供了云容器引擎 CCE SDK,您可以直接集成SDK来调用相关API,从而实现对云容器引擎服务的快速操作。
一个CCE集群可以管理一组节点资源。该示例展示了如何在云容器引擎服务为指定集群创建节点,并等待节点创建成功。
注意:示例中创建的节点为按需计费,如不再使用请及时删除节点或集群。
2. 开发前准备
3. 安装SDK
云容器引擎 CCE SDK支持go 1.14及以上版本。执行go version检查当前Go的版本信息。
使用go get安装华为云Go SDK,执行如下命令安装华为云Go SDK库以及相关依赖库,具体的SDK版本号请参见SDK开发中心。
4. 代码示例
以下代码展示如何为CCE集群创建节点:
package main import ( "encoding/json" "fmt" "time" "os" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" cce "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/region" ) func main() { /* 以下部分请替换成用户实际参数 */ // 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 ak := os.Getenv("HUAWEICLOUD_SDK_AK") // 华为云账号Access Key sk := os.Getenv("HUAWEICLOUD_SDK_SK") // 华为云账号Secret Access Key projectID := "<YOUR PROJECT ID>" // 华为云账号项目ID userRegion := "<YOUR REGION>" // 服务所在区域,eg: cn-north-4 clusterID := "<YOUR CLUSTER ID>" // 新建节点所在集群的ID az := "<YOUR NODE AZ>" // 节点所在可用区, eg: cn-north-4a flavor := "<YOUR NODE FLAVOR>" // 节点规格, eg: s2.large.2 sshKey := "<YOUR SSH KEY>" // 用户密钥对名称 /* 主流程 */ fmt.Printf("This is a demo to create node in cluster.\n") data := "{\"kind\":\"Node\",\"apiVersion\":\"v3\",\"spec\":{\"rootVolume\":{\"size\":40,\"volumetype\":\"SATA\"},\"dataVolumes\":[{\"size\":100,\"volumetype\":\"SATA\"}],\"billingMode\":0,\"count\":1}}" auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectID). Build() fmt.Printf("Build auth successfully.\n") client := cce.NewCceClient( cce.CceClientBuilder(). WithRegion(region.ValueOf(userRegion)). WithCredential(auth). Build()) fmt.Printf("Build client successfully.\n") request := &model.CreateNodeRequest{} request.ClusterId = clusterID request.Body = &model.NodeCreateRequest{} json.Unmarshal([]byte(data), request.Body) request.Body.Spec.Az = az request.Body.Spec.Flavor = flavor request.Body.Spec.Login = &model.Login{ SshKey: &sshKey, } /* 创建节点 */ response, err := client.CreateNode(request) if err == nil { fmt.Printf("Start to create node...\n") } else { fmt.Printf("Create node failed...\n") fmt.Printf("Error: %+v\n", err) return } if response != nil && response.Status != nil && response.Status.JobID != nil { waitNodeReady(client, *response.Status.JobID) } else { fmt.Printf("Get node state failed...\n") } } /* 查询任务执行状态,等待节点创建成功 */ func waitNodeReady(c *cce.CceClient, jobID string) { req := &model.ShowJobRequest{ JobId: jobID, } ticker := time.NewTicker(5 * time.Second) defer ticker.Stop() timeout := time.After(30 * time.Minute) for { select { case <-timeout: fmt.Printf("Wait node state timed out.") return case <-ticker.C: resp, err := c.ShowJob(req) if err != nil { fmt.Printf("Show create node job failed...\n") fmt.Printf("Error: %+v\n", err) return } if resp != nil && resp.Status != nil && resp.Status.Phase != nil { fmt.Printf("Wait node ready, job is %s\n", *resp.Status.Phase) if *resp.Status.Phase == "Success" { fmt.Printf("Create node successfully!") return } else if *resp.Status.Phase == "Failed" { fmt.Printf("Create node failed!") return } } } } }
5. 参考
更多信息请参考:云容器引擎 CCE 文档
6. 修订记录