diff --git a/src/cloud-api-adaptor/pkg/adaptor/cloud/cloud.go b/src/cloud-api-adaptor/pkg/adaptor/cloud/cloud.go index 7afadd439..2ff72a7db 100644 --- a/src/cloud-api-adaptor/pkg/adaptor/cloud/cloud.go +++ b/src/cloud-api-adaptor/pkg/adaptor/cloud/cloud.go @@ -203,11 +203,15 @@ func (s *cloudService) CreateVM(ctx context.Context, req *pb.CreateVMRequest) (r // Get Pod VM cpu and memory from annotations vcpus, memory := util.GetCPUAndMemoryFromAnnotation(req.Annotations) + // Get Pod VM image from annotations + image := util.GetImageFromAnnotation(req.Annotations) + // Pod VM spec vmSpec := provider.InstanceTypeSpec{ InstanceType: instanceType, VCPUs: vcpus, Memory: memory, + Image: image, } // TODO: server name is also generated in each cloud provider, and possibly inconsistent diff --git a/src/cloud-api-adaptor/pkg/util/cloud.go b/src/cloud-api-adaptor/pkg/util/cloud.go index 775aa430d..38a651b9c 100644 --- a/src/cloud-api-adaptor/pkg/util/cloud.go +++ b/src/cloud-api-adaptor/pkg/util/cloud.go @@ -35,6 +35,14 @@ func GetInstanceTypeFromAnnotation(annotations map[string]string) string { return annotations[hypannotations.MachineType] } +// Method to get image from annotation +func GetImageFromAnnotation(annotations map[string]string) string { + // The image annotation in Kata refers to image path + // For example image for Kata/Qemu refers to /hypervisor/image.img etc. + // We use the same annotation for Kata/remote to refer to image name + return annotations[hypannotations.ImagePath] +} + // Method to get vCPU and memory from annotations func GetCPUAndMemoryFromAnnotation(annotations map[string]string) (int64, int64) { diff --git a/src/cloud-api-adaptor/pkg/util/cloud_test.go b/src/cloud-api-adaptor/pkg/util/cloud_test.go index c49a809f4..5da0a3693 100644 --- a/src/cloud-api-adaptor/pkg/util/cloud_test.go +++ b/src/cloud-api-adaptor/pkg/util/cloud_test.go @@ -114,3 +114,42 @@ func TestGetInstanceTypeFromAnnotation(t *testing.T) { }) } } + +func TestGetImageFromAnnotation(t *testing.T) { + type args struct { + annotations map[string]string + } + tests := []struct { + name string + args args + want string + }{ + // Add test cases with annotations for only image name + { + name: "image name only", + args: args{ + annotations: map[string]string{ + hypannotations.ImagePath: "rhel9-os", + }, + }, + want: "rhel9-os", + }, + // Add test cases with annotations for only image name with empty value + { + name: "image name only with empty value", + args: args{ + annotations: map[string]string{ + hypannotations.ImagePath: "", + }, + }, + want: "", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := GetImageFromAnnotation(tt.args.annotations); got != tt.want { + t.Errorf("GetImageFromAnnotation() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/src/cloud-providers/libvirt/provider.go b/src/cloud-providers/libvirt/provider.go index dce89a148..f795a384c 100644 --- a/src/cloud-providers/libvirt/provider.go +++ b/src/cloud-providers/libvirt/provider.go @@ -79,6 +79,11 @@ func (p *libvirtProvider) CreateInstance(ctx context.Context, podName, sandboxID } logger.Printf("LaunchSecurityType: %s", vm.launchSecurityType.String()) + if spec.Image != "" { + logger.Printf("Choosing %s as libvirt volume for the PodVM image", spec.Image) + p.libvirtClient.volName = spec.Image + } + result, err := CreateDomain(ctx, p.libvirtClient, vm) if err != nil { logger.Printf("failed to create an instance : %v", err) diff --git a/src/cloud-providers/types.go b/src/cloud-providers/types.go index 55bd8b1a9..82ea5b0c7 100644 --- a/src/cloud-providers/types.go +++ b/src/cloud-providers/types.go @@ -66,4 +66,5 @@ type InstanceTypeSpec struct { Memory int64 Arch string GPUs int64 + Image string }