Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support uk8s custom image and custom boot disk size #70

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion builder/ucloud/common/access_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ucloud/ucloud-sdk-go/services/uaccount"
"github.com/ucloud/ucloud-sdk-go/services/ufile"
"github.com/ucloud/ucloud-sdk-go/services/uhost"
"github.com/ucloud/ucloud-sdk-go/services/uk8s"
"github.com/ucloud/ucloud-sdk-go/services/unet"
"github.com/ucloud/ucloud-sdk-go/services/vpc"
"github.com/ucloud/ucloud-sdk-go/ucloud"
Expand Down Expand Up @@ -122,7 +123,7 @@ func (c *AccessConfig) Client() (*UCloudClient, error) {
c.client.VPCConn = vpc.NewClient(&cfg, &cred)
c.client.UAccountConn = uaccount.NewClient(&cfg, &cred)
c.client.UFileConn = ufile.NewClient(&cfg, &cred)

c.client.UK8sConn = uk8s.NewClient(&cfg, &cred)
if cloudShellCredHandler != nil {
if err := c.client.UHostConn.AddHttpRequestHandler(cloudShellCredHandler); err != nil {
return nil, err
Expand All @@ -139,6 +140,9 @@ func (c *AccessConfig) Client() (*UCloudClient, error) {
if err := c.client.UFileConn.AddHttpRequestHandler(cloudShellCredHandler); err != nil {
return nil, err
}
if err := c.client.UK8sConn.AddHttpRequestHandler(cloudShellCredHandler); err != nil {
return nil, err
}
}

return c.client, nil
Expand Down
23 changes: 23 additions & 0 deletions builder/ucloud/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/ucloud/ucloud-sdk-go/services/uaccount"
"github.com/ucloud/ucloud-sdk-go/services/ufile"
"github.com/ucloud/ucloud-sdk-go/services/uhost"
"github.com/ucloud/ucloud-sdk-go/services/uk8s"
"github.com/ucloud/ucloud-sdk-go/services/unet"
"github.com/ucloud/ucloud-sdk-go/services/vpc"
"github.com/ucloud/ucloud-sdk-go/ucloud"
Expand All @@ -12,6 +13,7 @@ import (

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

}

func (c *UCloudClient) DescribeUK8sNodeImageById(imageId string) (*uk8s.ImageInfo, error) {
if imageId == "" {
return nil, NewNotFoundError("image", imageId)
}
req := c.UK8sConn.NewDescribeUK8SImageRequest()
resp, err := c.UK8sConn.DescribeUK8SImage(req)
if err != nil {
return nil, err
}

if len(resp.ImageSet) < 1 {
return nil, NewNotFoundError("image", imageId)
}
for _, image := range resp.ImageSet {
if image.ImageId == imageId {
return &image, nil
}
}
return nil, NewNotFoundError("image", imageId)
}
5 changes: 5 additions & 0 deletions builder/ucloud/common/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const (
DefaultCreateImageTimeout = 3600
)

const (
UK8sImageSize = 40
UK8sImageOsType = "Linux"
)

var BootDiskTypeMap = NewStringConverter(map[string]string{
"cloud_ssd": "CLOUD_SSD",
"local_normal": "LOCAL_NORMAL",
Expand Down
3 changes: 3 additions & 0 deletions builder/ucloud/common/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type RunConfig struct {
//
//~> **Note:** It takes around 10 mins for boot disk initialization when `boot_disk_type` is `local_normal` or `local_ssd`.
BootDiskType string `mapstructure:"boot_disk_type" required:"false"`
// The size of boot disk associated to UHost instance, which cannot be smaller than the size of source image.
// The unit is `GB`. Default value is the size of source image.
BootDiskSize int `mapstructure:"boot_disk_size" required:"false"`
// 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.
VPCId string `mapstructure:"vpc_id" required:"false"`
// The ID of subnet under the VPC. If `vpc_id` is defined, the `subnet_id` is mandatory required.
Expand Down
1 change: 1 addition & 0 deletions builder/ucloud/uhost/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
SourceImageId: b.config.SourceImageId,
InstanceName: b.config.InstanceName,
BootDiskType: b.config.BootDiskType,
BootDiskSize: b.config.BootDiskSize,
UsePrivateIp: b.config.UseSSHPrivateIp,
EipBandwidth: b.config.EipBandwidth,
EipChargeMode: b.config.EipChargeMode,
Expand Down
2 changes: 2 additions & 0 deletions builder/ucloud/uhost/builder.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions builder/ucloud/uhost/step_check_source_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uhost
import (
"context"
"fmt"
"github.com/ucloud/ucloud-sdk-go/services/uhost"

"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand All @@ -22,9 +23,21 @@ func (s *stepCheckSourceImageId) Run(ctx context.Context, state multistep.StateB
imageSet, err := client.DescribeImageById(s.SourceUHostImageId)
if err != nil {
if ucloudcommon.IsNotFoundError(err) {
return ucloudcommon.Halt(state, err, "")
uk8sNodeImage, uk8sErr := client.DescribeUK8sNodeImageById(s.SourceUHostImageId)
if ucloudcommon.IsNotFoundError(uk8sErr) {
return ucloudcommon.Halt(state, fmt.Errorf("fail to find source_image_id %q", s.SourceUHostImageId), "")
}
if uk8sErr != nil {
return ucloudcommon.Halt(state, uk8sErr, fmt.Sprintf("Error on querying specified source_image_id %q", s.SourceUHostImageId))
}
imageSet = &uhost.UHostImageSet{}
imageSet.ImageName = uk8sNodeImage.ImageName
imageSet.ImageSize = ucloudcommon.UK8sImageSize
imageSet.OsType = ucloudcommon.UK8sImageOsType
imageSet.Features = []string{"CloudInit"}
} else {
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on querying specified source_image_id %q", s.SourceUHostImageId))
}
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on querying specified source_image_id %q", s.SourceUHostImageId))
}

if imageSet.OsType == ucloudcommon.OsTypeWindows {
Expand Down
8 changes: 8 additions & 0 deletions builder/ucloud/uhost/step_create_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uhost
import (
"context"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"math/rand"
Expand All @@ -23,6 +24,7 @@ type stepCreateInstance struct {
InstanceType string
InstanceName string
BootDiskType string
BootDiskSize int
SourceImageId string
UsePrivateIp bool

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

req.Disks = append(req.Disks, bootDisk)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

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

- `boot_disk_size` (int) - The size of boot disk associated to UHost instance, which cannot be smaller than the size of source image.
The unit is `GB`. Default value is the size of source image.

- `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.

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