Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dilyar85 committed Dec 26, 2024
1 parent ff45797 commit bde1208
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .web-docs/components/builder/vsphere-supervisor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,11 @@ items are listed below as well as the _optional_ configurations.
- `bootstrap_data_file` (string) - Path to a file with bootstrap configuration data. Required if `bootstrap_provider` is not set to `CloudInit`.
Defaults to a basic cloud config that sets up the user account from the SSH communicator config.

- `guest_id` (string) - Name of the desired guest operating system identifier for source VM.
- `guest_os_type` (string) - The guest operating system identifier for the VM.
Defaults to `otherGuest`.

- `iso_boot_disk_size` (string) - Size of the PVC that will be used as the boot disk when deploying an ISO VM.
Supported units are `Gi`, `Mi`, `Ki`, `G`, `M`, `K`, etc.
Defaults to `20Gi`.

<!-- End of code generated from the comments of the CreateSourceConfig struct in builder/vsphere/supervisor/step_create_source.go; -->
Expand Down
4 changes: 2 additions & 2 deletions builder/vsphere/supervisor/config.hcl2spec.go

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

16 changes: 11 additions & 5 deletions builder/vsphere/supervisor/step_create_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ type CreateSourceConfig struct {
// Path to a file with bootstrap configuration data. Required if `bootstrap_provider` is not set to `CloudInit`.
// Defaults to a basic cloud config that sets up the user account from the SSH communicator config.
BootstrapDataFile string `mapstructure:"bootstrap_data_file"`
// Name of the desired guest operating system identifier for source VM.
// The guest operating system identifier for the VM.
// Defaults to `otherGuest`.
GuestID string `mapstructure:"guest_id"`
GuestOSType string `mapstructure:"guest_os_type"`
// Size of the PVC that will be used as the boot disk when deploying an ISO VM.
// Supported units are `Gi`, `Mi`, `Ki`, `G`, `M`, `K`, etc.
// Defaults to `20Gi`.
IsoBootDiskSize string `mapstructure:"iso_boot_disk_size"`
}
Expand Down Expand Up @@ -97,12 +98,17 @@ func (c *CreateSourceConfig) Prepare() []error {
errs = append(errs, fmt.Errorf("'source_name' must not exceed 15 characters (length: %d): %s", len(c.SourceName), c.SourceName))
}

if c.GuestID == "" {
c.GuestID = "otherGuest"
if c.GuestOSType == "" {
c.GuestOSType = "otherGuest"
}

if c.IsoBootDiskSize == "" {
c.IsoBootDiskSize = "20Gi"
} else {
_, err := resource.ParseQuantity(c.IsoBootDiskSize)
if err != nil {
errs = append(errs, fmt.Errorf("'iso_boot_disk_size' must be a valid quantity with units: %s", err))
}
}

return errs
Expand Down Expand Up @@ -358,7 +364,7 @@ func (s *StepCreateSource) createISO(ctx context.Context, logger *PackerLogger,
AllowGuestControl: &[]bool{true}[0],
},
},
GuestID: s.Config.GuestID,
GuestID: s.Config.GuestOSType,
Volumes: []vmopv1.VirtualMachineVolume{
{
Name: "vm-boot-disk",
Expand Down
4 changes: 2 additions & 2 deletions builder/vsphere/supervisor/step_create_source.hcl2spec.go

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

6 changes: 3 additions & 3 deletions builder/vsphere/supervisor/step_create_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func TestCreateSource_RunDefaultISO(t *testing.T) {
StorageClass: "test-storage-class",
SourceName: "test-source",
IsoBootDiskSize: "100Gi",
GuestID: "test-guest-id",
GuestOSType: "test-guest-id",

Check failure on line 255 in builder/vsphere/supervisor/step_create_source_test.go

View workflow job for this annotation

GitHub Actions / Lint check

File is not `goimports`-ed (goimports)
}
commConfig := &communicator.Config{
Type: "ssh",
Expand Down Expand Up @@ -324,8 +324,8 @@ func TestCreateSource_RunDefaultISO(t *testing.T) {
if vmObj.Spec.StorageClass != config.StorageClass {
t.Errorf("Expected VM storage class to be '%q', got %q", config.StorageClass, vmObj.Spec.StorageClass)
}
if vmObj.Spec.GuestID != config.GuestID {
t.Errorf("Expected VM guest ID to be '%q', got %q", config.GuestID, vmObj.Spec.GuestID)
if vmObj.Spec.GuestID != config.GuestOSType {
t.Errorf("Expected VM guest ID to be '%q', got %q", config.GuestOSType, vmObj.Spec.GuestID)
}
// Check if the source VM has the expected volume.
if len(vmObj.Spec.Volumes) != 1 {
Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/supervisor/step_import_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *ImportImageConfig) Prepare() []error {
case "":
if strings.HasSuffix(c.ImportSourceURL, ".iso") {
c.ImportTargetImageType = "iso"
} else if strings.HasSuffix(c.ImportSourceURL, ".ovf") {
} else if strings.HasSuffix(c.ImportSourceURL, ".ovf") || strings.HasSuffix(c.ImportSourceURL, ".ova") {
c.ImportTargetImageType = "ovf"
} else {
errs = append(errs, fmt.Errorf("cannot infer supported image type from source url %s", c.ImportSourceURL))
Expand Down
12 changes: 8 additions & 4 deletions builder/vsphere/supervisor/step_watch_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ func (s *StepWatchSource) waitForVMReady(ctx context.Context, logger *PackerLogg

url, err := s.getVMWebConsoleRequestURL(ctx, logger)
if err != nil {
logger.Error("Failed to generate a web-console URL for ISO VM")
logger.Error("Failed to generate a web console URL for ISO VM")
return "", err
}

logger.Info("%s", url)
logger.Info("Web console URL: %s", url)
logger.Info("Use the above URL to complete the guest OS installation.")
isoInfoDisplayed = true
}
Expand All @@ -210,6 +210,9 @@ func (s *StepWatchSource) waitForVMReady(ctx context.Context, logger *PackerLogg
}
}

// getVMWebConsoleRequestURL generates a web console URL for the VM guest OS access.
// It creates a VirtualMachineWebConsoleRequest object and waits for it to be processed.
// Once ready, it decrypts the response and returns the URL.
func (s *StepWatchSource) getVMWebConsoleRequestURL(ctx context.Context, logger *PackerLogger) (string, error) {
logger.Info("Generating a web console URL for VM guest OS access...")
privateKey, publicKeyPem, err := generateKeyPair()
Expand All @@ -234,15 +237,15 @@ func (s *StepWatchSource) getVMWebConsoleRequestURL(ctx context.Context, logger
timedCtx, cancel := context.WithTimeout(ctx, time.Duration(s.Config.WatchSourceTimeoutSec)*time.Second)
defer cancel()

wcr, err = s.waitForWcrStatusChange(timedCtx, logger)
wcr, err = s.waitForWCRStatusChange(timedCtx, logger)
if err != nil {
return "", err
}

return s.parseURL(wcr, privateKey)
}

func (s *StepWatchSource) waitForWcrStatusChange(ctx context.Context, logger *PackerLogger) (*vmopv1.VirtualMachineWebConsoleRequest, error) {
func (s *StepWatchSource) waitForWCRStatusChange(ctx context.Context, logger *PackerLogger) (*vmopv1.VirtualMachineWebConsoleRequest, error) {
watcher, err := s.KubeWatchClient.Watch(ctx, &vmopv1.VirtualMachineWebConsoleRequestList{}, &client.ListOptions{
FieldSelector: fields.OneTermEqualSelector("metadata.name", s.SourceName),
Namespace: s.Namespace,
Expand Down Expand Up @@ -360,6 +363,7 @@ func (s *StepWatchSource) getVMIngressIP(ctx context.Context, logger *PackerLogg
return vmIngressIP, nil
}

// generateKeyPair generates a new RSA key pair used for the web console URL.
func generateKeyPair() (*rsa.PrivateKey, []byte, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion builder/vsphere/supervisor/step_watch_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestWatchSource_RunISO(t *testing.T) {
networkConfigLines := strings.Split(string(c), "\n")
expectedOutput = append(expectedOutput, networkConfigLines...)
expectedOutput = append(expectedOutput, "Generating a web console URL for VM guest OS access...")
expectedOutput = append(expectedOutput, "https://test-proxy-addr/vm/web-console?host=1.2.3.4&namespace=test-ns&port=80&ticket=test-ticket&uuid=test-uuid")
expectedOutput = append(expectedOutput, "Web console URL: https://test-proxy-addr/vm/web-console?host=1.2.3.4&namespace=test-ns&port=80&ticket=test-ticket&uuid=test-uuid")
expectedOutput = append(expectedOutput, "Use the above URL to complete the guest OS installation.")
expectedOutput = append(expectedOutput, "Successfully obtained the source VM IP: 1.2.3.4")
expectedOutput = append(expectedOutput, "Source VM is now ready in Supervisor cluster")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
- `bootstrap_data_file` (string) - Path to a file with bootstrap configuration data. Required if `bootstrap_provider` is not set to `CloudInit`.
Defaults to a basic cloud config that sets up the user account from the SSH communicator config.

- `guest_id` (string) - Name of the desired guest operating system identifier for source VM.
- `guest_os_type` (string) - The guest operating system identifier for the VM.
Defaults to `otherGuest`.

- `iso_boot_disk_size` (string) - Size of the PVC that will be used as the boot disk when deploying an ISO VM.
Supported units are `Gi`, `Mi`, `Ki`, `G`, `M`, `K`, etc.
Defaults to `20Gi`.

<!-- End of code generated from the comments of the CreateSourceConfig struct in builder/vsphere/supervisor/step_create_source.go; -->

0 comments on commit bde1208

Please sign in to comment.