NodeGroup Management
Updated at:2025-10-27
Create node group
Plain Text
1func CreateInstanceGroup() {
2 // User’s Access Key ID and Secret Access Key
3 AK, SK := "", ""
4 // User-specified endpoint
5 ENDPOINT := ""
6 // Initialize a CCEClient
7 ccev2Client, err := v2.NewClient(AK, SK, ENDPOINT)
8 if err != nil {
9 panic(err)
10 }
11 resp, err := ccev2Client.CreateInstanceGroup(&v2.CreateInstanceGroupArgs{
12 ClusterID: "",
13 Request: &v2.CreateInstanceGroupRequest{
14 InstanceGroupSpec: types.InstanceGroupSpec{
15 InstanceGroupName: "",
16 ClusterID: "",
17 InstanceTemplate: types.InstanceTemplate{
18 InstanceSpec: types.InstanceSpec{
19 MachineType: types.MachineTypeBCC,
20 InstanceType: api.InstanceType34,
21 InstanceName: "",
22 VPCConfig: types.VPCConfig{
23 VPCSubnetID: "sbn-xxx",
24 SecurityGroup: types.SecurityGroup{
25 EnableCCERequiredSecurityGroup: true,
26 EnableCCEOptionalSecurityGroup: false,
27 },
28 },
29 InstanceResource: types.InstanceResource{
30 CPU: 2,
31 MEM: 8,
32 RootDiskType: api.StorageTypeHP1,
33 RootDiskSize: 100,
34 LocalDiskSize: 0,
35 CDSList: []types.CDSConfig{},
36 MachineSpec: "bcc.g5.c2m8",
37 },
38 ImageID: "7183d3d0-3e24-464d-9c02-xxxxxxx",
39 InstanceOS: types.InstanceOS{
40 ImageType: api.ImageTypeSystem,
41 },
42 SSHKeyID: "k-xxxx",
43 DeployCustomConfig: types.DeployCustomConfig{
44 KubeletRootDir: "/var/lib/kubelet",
45 KubeReserved: map[string]string{
46 "cpu": "50m",
47 "memory": "100Mi",
48 },
49 SystemReserved: map[string]string{
50 "cpu": "50m",
51 "memory": "100Mi",
52 },
53 ContainerdConfig: types.ContainerdConfig{
54 DataRoot: "/home/cce/containerd",
55 },
56 },
57 RelationTag: true,
58 InstanceChargingType: api.PaymentTimingPostPaid,
59 Tags: types.TagList{
60 {
61 TagKey: "tagkey",
62 TagValue: "tagvalue",
63 },
64 },
65 },
66 },
67 Replicas: 1,
68 },
69 },
70 })
71 if err != nil {
72 fmt.Println(err.Error())
73 return
74 }
75 s, _ := json.MarshalIndent(resp, "", "\t")
76 fmt.Println("Response:" + string(s))
77}
Create node group and associate tags
Plain Text
1 resp, err := ccev2Client.CreateInstanceGroup(&v2.CreateInstanceGroupArgs{
2 ClusterID: "",
3 Request: &v2.CreateInstanceGroupRequest{
4 InstanceGroupSpec: types.InstanceGroupSpec{
5 InstanceGroupName: "",
6 ClusterID: "",
7 InstanceTemplate: types.InstanceTemplate{
8 InstanceSpec: types.InstanceSpec{
9 // Instance configurations in the node group
10
11 //
12 RelationTag: true,
13 Tags: types.TagList{
14 {
15 TagKey: "tagkey",
16 TagValue: "tagvalue",
17 },
18 },
19 },
20 },
21 Replicas: 1,
22 },
23 },
24 })
25 if err != nil {
26 fmt.Println(err.Error())
27 return
28 }
29 s, _ := json.MarshalIndent(resp, "", "\t")
30 fmt.Println("Response:" + string(s))
Query node group
Plain Text
1func GetInstanceGroup() {
2 // User’s Access Key ID and Secret Access Key
3 AK, SK := "", ""
4 // User-specified endpoint
5 ENDPOINT := ""
6 // Initialize a CCEClient
7 ccev2Client, err := v2.NewClient(AK, SK, ENDPOINT)
8 if err != nil {
9 panic(err)
10 }
11 resp, err := ccev2Client.GetInstanceGroup(&v2.GetInstanceGroupArgs{
12 ClusterID: "",
13 InstanceGroupID: "",
14 })
15 if err != nil {
16 fmt.Println(err.Error())
17 return
18 }
19 s, _ := json.MarshalIndent(resp, "", "\t")
20 fmt.Println("Response:" + string(s))
21 // Node group tags
22 tags, _ := json.MarshalIndent(resp.InstanceGroup.Spec.InstanceTemplate.Tags, "", "\t")
23 fmt.Println("Response:" + string(tags))
24}
Add existing nodes to the node group
Plain Text
1// TestClient_AttachInstancesToInstanceGroup Add existing nodes to the node group
2func TestClient_AttachInstancesToInstanceGroup(t *testing.T) {
3 var (
4 ak = ""
5 sk = ""
6 endpoint = ""
7 clusterID = ""
8 instanceGroupID = ""
9 existInstanceID = ""
10 AdminPassword = ""
11 // rebuild Whether to reinstall OS
12 rebuild = true
13 // useInstanceGroupConfig Whether to use node group configurations
14 useInstanceGroupConfig = true
15 // useLocalDiskForContainer Whether to use local disk for storing data
16 useLocalDiskForContainer = false
17 imageID = ""
18 )
19 cceClient, err := NewClient(ak, sk, endpoint)
20 if err != nil {
21 fmt.Printf("NewClient error: %s", err.Error())
22 return
23 }
24 attachInstanceToInstanceGroupArgs := func() *AttachInstancesToInstanceGroupArgs {
25 args := AttachInstancesToInstanceGroupArgs{}
26 args.ClusterID = clusterID
27 args.InstanceGroupID = instanceGroupID
28 args.Request = &AttachInstancesToInstanceGroupRequest{
29 ExistedInstances: make([]*InstanceSet, 0),
30 UseInstanceGroupConfig: useInstanceGroupConfig,
31 }
32 existInstance := &InstanceSet{
33 InstanceSpec: types.InstanceSpec{
34 Existed: true,
35 ExistedOption: types.ExistedOption{
36 //BCC instance ID
37 ExistedInstanceID: existInstanceID,
38 Rebuild: &rebuild,
39 },
40 // Refer to specific types: bcc, bbc, ebc
41 MachineType: types.MachineTypeBCC,
42 ClusterRole: types.ClusterRoleNode,
43 // Either
44 AdminPassword: AdminPassword,
45 SSHKeyID: "",
46 },
47 }
48 // If the OS is reinstalled, configure it here
49 if rebuild {
50 existInstance.InstanceSpec.InstanceOS = types.InstanceOS{
51 ImageType: bccapi.ImageTypeSystem,
52 }
53 existInstance.InstanceSpec.ImageID = imageID
54 }
55 if useLocalDiskForContainer {
56 // Store container data in data disks or local disks
57 existInstance.InstanceSpec.InstanceResource = types.InstanceResource{
58 CDSList: types.CDSConfigList{
59 {
60 DataDevice: "/dev/xxx",
61 Path: "/home/cce",
62 },
63 },
64 }
65 }
66 if !useInstanceGroupConfig {
67 existInstance.InstanceSpec.VPCConfig = types.VPCConfig{
68 SecurityGroups: []types.SecurityGroupV2{
69 {
70 ID: "",
71 Name: "",
72 Type: types.SecurityGroupTypeNormal,
73 },
74 },
75 }
76 existInstance.InstanceSpec.DeployCustomConfig = types.DeployCustomConfig{
77 KubeletRootDir: "",
78 PreUserScript: "",
79 PostUserScript: "",
80 }
81 // Tag configuration
82 existInstance.InstanceSpec.Tags = types.TagList{
83 {
84 TagKey: "",
85 TagValue: "",
86 },
87 }
88 }
89 args.Request.ExistedInstances = append(args.Request.ExistedInstances, existInstance)
90 return &args
91 }()
92 commonResp, err := cceClient.AttachInstancesToInstanceGroup(attachInstanceToInstanceGroupArgs)
93 if err != nil {
94 fmt.Printf("attach instance to instance group failed, errir: %v", err)
95 return
96 }
97 fmt.Printf("Request ID: %s", commonResp.RequestID)
98}
Remove existing nodes from the node group
Plain Text
1func TestClient_CreateScaleDownInstanceGroupTask(t *testing.T) {
2 type fields struct {
3 ak, sk, endpoint string
4 }
5 type args struct {
6 args *CreateScaleDownInstanceGroupTaskArgs
7 }
8 tests := []struct {
9 name string
10 fields fields
11 args args
12 }{
13 {
14 Name: "Remove existing nodes",
15 fields: fields{
16 ak: "",
17 sk: "",
18 endpoint: "",
19 },
20 args: args{
21 args: &CreateScaleDownInstanceGroupTaskArgs{
22 InstancesToBeRemoved: []string{""},
23 ClusterID: "",
24 InstanceGroupID: "",
25 CleanPolicy: CleanPolicyDelete,
26 DeleteOption: &types.DeleteOption{
27 DeleteCDSSnapshot: false,
28 DeleteResource: false,
29 DrainNode: false,
30 MoveOut: true,
31 },
32 },
33 },
34 },
35 }
36 for _, tt := range tests {
37 t.Run(tt.name, func(t *testing.T) {
38 c, err := NewClient(tt.fields.ak, tt.fields.sk, tt.fields.endpoint)
39 if err != nil {
40 t.Errorf("failed init client, error = %v", err)
41 return
42 }
43 if resp, err := c.CreateScaleDownInstanceGroupTask(tt.args.args); err != nil {
44 t.Errorf("CreateScaleDownInstanceGroupTask() error = %v", err)
45 } else {
46 t.Logf("request id is: %s", resp.RequestID)
47 }
48 })
49 }
50}
