Skip to content

Commit 2a20865

Browse files
wangrzneuschnell18
authored andcommitted
support uk8s custom image and custom boot disk size
1 parent 236a550 commit 2a20865

File tree

9 files changed

+65
-3
lines changed

9 files changed

+65
-3
lines changed

builder/ucloud/common/access_config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/ucloud/ucloud-sdk-go/services/uaccount"
1818
"github.com/ucloud/ucloud-sdk-go/services/ufile"
1919
"github.com/ucloud/ucloud-sdk-go/services/uhost"
20+
"github.com/ucloud/ucloud-sdk-go/services/uk8s"
2021
"github.com/ucloud/ucloud-sdk-go/services/unet"
2122
"github.com/ucloud/ucloud-sdk-go/services/vpc"
2223
"github.com/ucloud/ucloud-sdk-go/ucloud"
@@ -122,7 +123,7 @@ func (c *AccessConfig) Client() (*UCloudClient, error) {
122123
c.client.VPCConn = vpc.NewClient(&cfg, &cred)
123124
c.client.UAccountConn = uaccount.NewClient(&cfg, &cred)
124125
c.client.UFileConn = ufile.NewClient(&cfg, &cred)
125-
126+
c.client.UK8sConn = uk8s.NewClient(&cfg, &cred)
126127
if cloudShellCredHandler != nil {
127128
if err := c.client.UHostConn.AddHttpRequestHandler(cloudShellCredHandler); err != nil {
128129
return nil, err
@@ -139,6 +140,9 @@ func (c *AccessConfig) Client() (*UCloudClient, error) {
139140
if err := c.client.UFileConn.AddHttpRequestHandler(cloudShellCredHandler); err != nil {
140141
return nil, err
141142
}
143+
if err := c.client.UK8sConn.AddHttpRequestHandler(cloudShellCredHandler); err != nil {
144+
return nil, err
145+
}
142146
}
143147

144148
return c.client, nil

builder/ucloud/common/client.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/ucloud/ucloud-sdk-go/services/uaccount"
55
"github.com/ucloud/ucloud-sdk-go/services/ufile"
66
"github.com/ucloud/ucloud-sdk-go/services/uhost"
7+
"github.com/ucloud/ucloud-sdk-go/services/uk8s"
78
"github.com/ucloud/ucloud-sdk-go/services/unet"
89
"github.com/ucloud/ucloud-sdk-go/services/vpc"
910
"github.com/ucloud/ucloud-sdk-go/ucloud"
@@ -12,6 +13,7 @@ import (
1213

1314
type UCloudClient struct {
1415
UHostConn *uhost.UHostClient
16+
UK8sConn *uk8s.UK8SClient
1517
UNetConn *unet.UNetClient
1618
VPCConn *vpc.VPCClient
1719
UAccountConn *uaccount.UAccountClient
@@ -140,3 +142,24 @@ func (c *UCloudClient) DescribeImageByInfo(projectId, regionId, imageId string)
140142
return &resp.ImageSet[0], nil
141143

142144
}
145+
146+
func (c *UCloudClient) DescribeUK8sNodeImageById(imageId string) (*uk8s.ImageInfo, error) {
147+
if imageId == "" {
148+
return nil, NewNotFoundError("image", imageId)
149+
}
150+
req := c.UK8sConn.NewDescribeUK8SImageRequest()
151+
resp, err := c.UK8sConn.DescribeUK8SImage(req)
152+
if err != nil {
153+
return nil, err
154+
}
155+
156+
if len(resp.ImageSet) < 1 {
157+
return nil, NewNotFoundError("image", imageId)
158+
}
159+
for _, image := range resp.ImageSet {
160+
if image.ImageId == imageId {
161+
return &image, nil
162+
}
163+
}
164+
return nil, NewNotFoundError("image", imageId)
165+
}

builder/ucloud/common/consts.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ const (
2424
DefaultCreateImageTimeout = 3600
2525
)
2626

27+
const (
28+
UK8sImageSize = 40
29+
UK8sImageOsType = "Linux"
30+
)
31+
2732
var BootDiskTypeMap = NewStringConverter(map[string]string{
2833
"cloud_ssd": "CLOUD_SSD",
2934
"local_normal": "LOCAL_NORMAL",

builder/ucloud/common/run_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ type RunConfig struct {
3030
//
3131
//~> **Note:** It takes around 10 mins for boot disk initialization when `boot_disk_type` is `local_normal` or `local_ssd`.
3232
BootDiskType string `mapstructure:"boot_disk_type" required:"false"`
33+
// The size of boot disk associated to UHost instance, which cannot be smaller than the size of source image.
34+
// The unit is `GB`. Default value is the size of source image.
35+
BootDiskSize int `mapstructure:"boot_disk_size" required:"false"`
3336
// The ID of VPC linked to the UHost instance. If not defined `vpc_id`, the instance will use the default VPC in the current region.
3437
VPCId string `mapstructure:"vpc_id" required:"false"`
3538
// The ID of subnet under the VPC. If `vpc_id` is defined, the `subnet_id` is mandatory required.

builder/ucloud/uhost/builder.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
113113
SourceImageId: b.config.SourceImageId,
114114
InstanceName: b.config.InstanceName,
115115
BootDiskType: b.config.BootDiskType,
116+
BootDiskSize: b.config.BootDiskSize,
116117
UsePrivateIp: b.config.UseSSHPrivateIp,
117118
EipBandwidth: b.config.EipBandwidth,
118119
EipChargeMode: b.config.EipChargeMode,

builder/ucloud/uhost/builder.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/ucloud/uhost/step_check_source_image.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package uhost
33
import (
44
"context"
55
"fmt"
6+
"github.com/ucloud/ucloud-sdk-go/services/uhost"
67

78
"github.com/hashicorp/packer-plugin-sdk/multistep"
89
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
@@ -22,9 +23,21 @@ func (s *stepCheckSourceImageId) Run(ctx context.Context, state multistep.StateB
2223
imageSet, err := client.DescribeImageById(s.SourceUHostImageId)
2324
if err != nil {
2425
if ucloudcommon.IsNotFoundError(err) {
25-
return ucloudcommon.Halt(state, err, "")
26+
uk8sNodeImage, uk8sErr := client.DescribeUK8sNodeImageById(s.SourceUHostImageId)
27+
if ucloudcommon.IsNotFoundError(uk8sErr) {
28+
return ucloudcommon.Halt(state, fmt.Errorf("fail to find source_image_id %q", s.SourceUHostImageId), "")
29+
}
30+
if uk8sErr != nil {
31+
return ucloudcommon.Halt(state, uk8sErr, fmt.Sprintf("Error on querying specified source_image_id %q", s.SourceUHostImageId))
32+
}
33+
imageSet = &uhost.UHostImageSet{}
34+
imageSet.ImageName = uk8sNodeImage.ImageName
35+
imageSet.ImageSize = ucloudcommon.UK8sImageSize
36+
imageSet.OsType = ucloudcommon.UK8sImageOsType
37+
imageSet.Features = []string{"CloudInit"}
38+
} else {
39+
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on querying specified source_image_id %q", s.SourceUHostImageId))
2640
}
27-
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on querying specified source_image_id %q", s.SourceUHostImageId))
2841
}
2942

3043
if imageSet.OsType == ucloudcommon.OsTypeWindows {

builder/ucloud/uhost/step_create_instance.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package uhost
33
import (
44
"context"
55
"encoding/base64"
6+
"errors"
67
"fmt"
78
"io/ioutil"
89
"math/rand"
@@ -23,6 +24,7 @@ type stepCreateInstance struct {
2324
InstanceType string
2425
InstanceName string
2526
BootDiskType string
27+
BootDiskSize int
2628
SourceImageId string
2729
UsePrivateIp bool
2830

@@ -281,6 +283,12 @@ func (s *stepCreateInstance) buildCreateInstanceRequest(state multistep.StateBag
281283
bootDisk := uhost.UHostDisk{}
282284
bootDisk.IsBoot = ucloud.String("true")
283285
bootDisk.Size = ucloud.Int(srcImage.ImageSize)
286+
if s.BootDiskSize > 0 {
287+
if s.BootDiskSize < srcImage.ImageSize {
288+
return nil, errors.New("boot disk size should not be smaller than image size")
289+
}
290+
bootDisk.Size = ucloud.Int(s.BootDiskSize)
291+
}
284292
bootDisk.Type = ucloud.String(ucloudcommon.BootDiskTypeMap.Convert(s.BootDiskType))
285293

286294
req.Disks = append(req.Disks, bootDisk)

docs-partials/builder/ucloud/common/RunConfig-not-required.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
~> **Note:** It takes around 10 mins for boot disk initialization when `boot_disk_type` is `local_normal` or `local_ssd`.
1212

13+
- `boot_disk_size` (int) - The size of boot disk associated to UHost instance, which cannot be smaller than the size of source image.
14+
The unit is `GB`. Default value is the size of source image.
15+
1316
- `vpc_id` (string) - The ID of VPC linked to the UHost instance. If not defined `vpc_id`, the instance will use the default VPC in the current region.
1417

1518
- `subnet_id` (string) - The ID of subnet under the VPC. If `vpc_id` is defined, the `subnet_id` is mandatory required.

0 commit comments

Comments
 (0)