Skip to content

Commit 4e4ce85

Browse files
committed
feat(vke): add example
1 parent a834e3f commit 4e4ce85

File tree

8 files changed

+543
-0
lines changed

8 files changed

+543
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.volcengine.vke.example;
2+
3+
import com.volcengine.ApiException;
4+
import com.volcengine.vke.VkeApi;
5+
import com.volcengine.vke.model.*;
6+
7+
import java.util.Collections;
8+
9+
public class CreateCluster {
10+
public static String Call(VkeApi vkeApi, final Option opt) throws ApiException {
11+
CreateClusterRequest request = new CreateClusterRequest().
12+
clusterConfig(new ClusterConfigForCreateClusterInput().
13+
apiServerPublicAccessEnabled(false).
14+
subnetIds(Collections.singletonList(opt.getSubnetID()))
15+
).
16+
name(opt.getClusterName()).
17+
podsConfig(new PodsConfigForCreateClusterInput().
18+
podNetworkMode(opt.getNetworkMode())).
19+
servicesConfig(new ServicesConfigForCreateClusterInput().
20+
serviceCidrsv4(Collections.singletonList(opt.getServiceCidr())));
21+
22+
if (opt.getNetworkMode() == PodsConfigForCreateClusterInput.PodNetworkModeEnum.FLANNEL) {
23+
request.getPodsConfig().flannelConfig(new FlannelConfigForCreateClusterInput().
24+
podCidrs(Collections.singletonList(opt.getPodCidr())).
25+
maxPodsPerNode(64));
26+
} else if (opt.getNetworkMode() == PodsConfigForCreateClusterInput.PodNetworkModeEnum.VPCCNISHARED) {
27+
request.getPodsConfig().vpcCniConfig(new VpcCniConfigForCreateClusterInput().
28+
subnetIds(Collections.singletonList(opt.getSubnetID())));
29+
}
30+
31+
CreateClusterResponse response = vkeApi.createCluster(request);
32+
return response.getId();
33+
}
34+
35+
public static class Option {
36+
private String clusterName;
37+
private String subnetID;
38+
private String podCidr;
39+
private String serviceCidr;
40+
private PodsConfigForCreateClusterInput.PodNetworkModeEnum networkMode;
41+
42+
public String getClusterName() {
43+
return clusterName;
44+
}
45+
46+
public Option setClusterName(String clusterName) {
47+
this.clusterName = clusterName;
48+
return this;
49+
}
50+
51+
public String getSubnetID() {
52+
return subnetID;
53+
}
54+
55+
public Option setSubnetID(String subnetID) {
56+
this.subnetID = subnetID;
57+
return this;
58+
}
59+
60+
public String getPodCidr() {
61+
return podCidr;
62+
}
63+
64+
public Option setPodCidr(String podCidr) {
65+
this.podCidr = podCidr;
66+
return this;
67+
}
68+
69+
public String getServiceCidr() {
70+
return serviceCidr;
71+
}
72+
73+
public Option setServiceCidr(String serviceCidr) {
74+
this.serviceCidr = serviceCidr;
75+
return this;
76+
}
77+
78+
public PodsConfigForCreateClusterInput.PodNetworkModeEnum getNetworkMode() {
79+
return networkMode;
80+
}
81+
82+
public Option setNetworkMode(PodsConfigForCreateClusterInput.PodNetworkModeEnum networkMode) {
83+
this.networkMode = networkMode;
84+
return this;
85+
}
86+
87+
}
88+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.volcengine.vke.example;
2+
3+
4+
import com.volcengine.ApiException;
5+
import com.volcengine.vke.VkeApi;
6+
import com.volcengine.vke.model.*;
7+
8+
import java.util.Collections;
9+
10+
public class CreateNodePool {
11+
public static String Call(VkeApi vkeApi, final Option opt) throws ApiException {
12+
CreateNodePoolRequest request = new CreateNodePoolRequest().
13+
clusterId(opt.getClusterId()).
14+
name(opt.getNodePoolName()).
15+
nodeConfig(new NodeConfigForCreateNodePoolInput().
16+
systemVolume(new SystemVolumeForCreateNodePoolInput().
17+
size(40).
18+
type(SystemVolumeForCreateNodePoolInput.TypeEnum.ESSD_PL0)).
19+
instanceTypeIds(Collections.singletonList(opt.getInstanceType())).
20+
security(new SecurityForCreateNodePoolInput().
21+
login(new LoginForCreateNodePoolInput().password(opt.getPassword()))).
22+
subnetIds(Collections.singletonList(opt.getSubnetID())));
23+
24+
CreateNodePoolResponse response = vkeApi.createNodePool(request);
25+
return response.getId();
26+
}
27+
28+
public static class Option {
29+
private String clusterId;
30+
private String nodePoolName;
31+
private String instanceType;
32+
private String password;
33+
private String subnetID;
34+
35+
public String getClusterId() {
36+
return clusterId;
37+
}
38+
39+
public Option setClusterId(String clusterId) {
40+
this.clusterId = clusterId;
41+
return this;
42+
}
43+
44+
public String getNodePoolName() {
45+
return nodePoolName;
46+
}
47+
48+
public Option setNodePoolName(String nodePoolName) {
49+
this.nodePoolName = nodePoolName;
50+
return this;
51+
}
52+
53+
public String getInstanceType() {
54+
return instanceType;
55+
}
56+
57+
public Option setInstanceType(String instanceType) {
58+
this.instanceType = instanceType;
59+
return this;
60+
}
61+
62+
public String getPassword() {
63+
return password;
64+
}
65+
66+
public Option setPassword(String password) {
67+
this.password = password;
68+
return this;
69+
}
70+
71+
public String getSubnetID() {
72+
return subnetID;
73+
}
74+
75+
public Option setSubnetID(String subnetID) {
76+
this.subnetID = subnetID;
77+
return this;
78+
}
79+
}
80+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.volcengine.vke.example;
2+
3+
import com.volcengine.ApiException;
4+
import com.volcengine.vke.VkeApi;
5+
import com.volcengine.vke.model.FilterForListClustersInput;
6+
import com.volcengine.vke.model.ItemForListClustersOutput;
7+
import com.volcengine.vke.model.ListClustersRequest;
8+
import com.volcengine.vke.model.ListClustersResponse;
9+
10+
import java.util.Collections;
11+
12+
public class GetCluster {
13+
public static ItemForListClustersOutput Call(VkeApi vkeApi, final Option opt) throws ApiException, ItemsCountException {
14+
ListClustersRequest request = new ListClustersRequest()
15+
.filter(new FilterForListClustersInput()
16+
.ids(Collections.singletonList(opt.getClusterId())));
17+
18+
ListClustersResponse response = vkeApi.listClusters(request);
19+
if (response.getItems().size() != 1) {
20+
throw new ItemsCountException("get cluster resp items must be 1");
21+
}
22+
return response.getItems().get(0);
23+
}
24+
25+
public static class Option {
26+
private String clusterId;
27+
28+
public String getClusterId() {
29+
return clusterId;
30+
}
31+
32+
public Option setClusterId(String clusterId) {
33+
this.clusterId = clusterId;
34+
return this;
35+
}
36+
}
37+
38+
static class ItemsCountException extends Exception {
39+
public ItemsCountException(String msg) {
40+
super(msg);
41+
}
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.volcengine.vke.example;
2+
3+
import com.volcengine.ApiException;
4+
import com.volcengine.vke.VkeApi;
5+
import com.volcengine.vke.model.FilterForListNodePoolsInput;
6+
import com.volcengine.vke.model.ItemForListNodePoolsOutput;
7+
import com.volcengine.vke.model.ListNodePoolsRequest;
8+
import com.volcengine.vke.model.ListNodePoolsResponse;
9+
10+
import java.util.Collections;
11+
12+
public class GetNodePool {
13+
public static ItemForListNodePoolsOutput Call(VkeApi vkeApi, final Option opt) throws ApiException, ItemsCountException {
14+
ListNodePoolsRequest request = new ListNodePoolsRequest()
15+
.filter(new FilterForListNodePoolsInput()
16+
.ids(Collections.singletonList(opt.getNodePoolId())));
17+
18+
ListNodePoolsResponse response = vkeApi.listNodePools(request);
19+
if (response.getItems().size() != 1) {
20+
throw new ItemsCountException("get node pool resp items must be 1");
21+
}
22+
return response.getItems().get(0);
23+
}
24+
25+
public static class Option {
26+
private String nodePoolId;
27+
28+
public String getNodePoolId() {
29+
return nodePoolId;
30+
}
31+
32+
public Option setNodePoolId(String nodePoolId) {
33+
this.nodePoolId = nodePoolId;
34+
return this;
35+
}
36+
}
37+
38+
static class ItemsCountException extends Exception {
39+
public ItemsCountException(String msg) {
40+
super(msg);
41+
}
42+
}
43+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.volcengine.vke.example;
2+
3+
4+
import com.volcengine.ApiException;
5+
import com.volcengine.vke.VkeApi;
6+
import com.volcengine.vke.model.CreateAddonRequest;
7+
8+
public class InstallAddon {
9+
public static void Call(VkeApi vkeApi, final Option opt) throws ApiException, InterruptedException {
10+
vkeApi.createAddon(new CreateAddonRequest().
11+
clusterId(opt.clusterId).
12+
name("metrics-server"));
13+
Thread.sleep(5 * 1000);
14+
vkeApi.createAddon(new CreateAddonRequest().
15+
clusterId(opt.clusterId).
16+
name("core-dns"));
17+
Thread.sleep(5 * 1000);
18+
vkeApi.createAddon(new CreateAddonRequest().
19+
clusterId(opt.clusterId).
20+
name("cluster-autoscaler").
21+
config("{\"Expander\":\"random\"}"));
22+
}
23+
24+
public static class Option {
25+
private String clusterId;
26+
27+
public String getClusterId() {
28+
return clusterId;
29+
}
30+
31+
public Option setClusterId(String clusterId) {
32+
this.clusterId = clusterId;
33+
return this;
34+
}
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.volcengine.vke.example;
2+
3+
4+
import com.volcengine.ApiException;
5+
import com.volcengine.vke.VkeApi;
6+
import com.volcengine.vke.model.FilterForListNodesInput;
7+
import com.volcengine.vke.model.ListNodesRequest;
8+
import com.volcengine.vke.model.ListNodesResponse;
9+
10+
import java.util.Collections;
11+
12+
public class ListNodes {
13+
public static ListNodesResponse Call(VkeApi vkeApi, final Option opt) throws ApiException {
14+
ListNodesRequest request = new ListNodesRequest()
15+
.filter(new FilterForListNodesInput()
16+
.nodePoolIds(Collections.singletonList(opt.getNodePoolId())));
17+
18+
return vkeApi.listNodes(request);
19+
}
20+
21+
public static class Option {
22+
private String nodePoolId;
23+
24+
public String getNodePoolId() {
25+
return nodePoolId;
26+
}
27+
28+
public Option setNodePoolId(String nodePoolId) {
29+
this.nodePoolId = nodePoolId;
30+
return this;
31+
}
32+
}
33+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.volcengine.vke.example;
2+
3+
import com.volcengine.ApiException;
4+
import com.volcengine.vke.VkeApi;
5+
import com.volcengine.vke.model.AutoScalingForUpdateNodePoolConfigInput;
6+
import com.volcengine.vke.model.UpdateNodePoolConfigRequest;
7+
8+
public class UpdateNodePool {
9+
public static void Call(VkeApi vkeApi, final Option opt) throws ApiException {
10+
Integer num = opt.autoScaling ? 0 : 1;
11+
vkeApi.updateNodePoolConfig(new UpdateNodePoolConfigRequest().
12+
clusterId(opt.getClusterId()).
13+
id(opt.getNodePoolId()).
14+
autoScaling(new AutoScalingForUpdateNodePoolConfigInput().
15+
enabled(opt.isAutoScaling()).
16+
desiredReplicas(num)));
17+
}
18+
19+
public static class Option {
20+
private String clusterId;
21+
private String nodePoolId;
22+
private Boolean autoScaling;
23+
24+
public String getClusterId() {
25+
return clusterId;
26+
}
27+
28+
public Option setClusterId(String clusterId) {
29+
this.clusterId = clusterId;
30+
return this;
31+
}
32+
33+
public String getNodePoolId() {
34+
return nodePoolId;
35+
}
36+
37+
public Option setNodePoolId(String nodePoolId) {
38+
this.nodePoolId = nodePoolId;
39+
return this;
40+
}
41+
42+
public Boolean isAutoScaling() {
43+
return autoScaling;
44+
}
45+
46+
public Option setAutoScaling(Boolean autoScaling) {
47+
this.autoScaling = autoScaling;
48+
return this;
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)