Skip to content

Commit

Permalink
libvirt: Enable multiple PodVM image scenario
Browse files Browse the repository at this point in the history
Enable support for multiple PodVM images for libvirt provider with the merge of kata-containers/kata-containers#10274

Fixes #1282

Signed-off-by: Ajay Victor <[email protected]>
  • Loading branch information
ajaypvictor committed Sep 30, 2024
1 parent 945ee79 commit 077b833
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cloud-api-adaptor/pkg/adaptor/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/cloud-api-adaptor/pkg/util/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
39 changes: 39 additions & 0 deletions src/cloud-api-adaptor/pkg/util/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
5 changes: 5 additions & 0 deletions src/cloud-providers/libvirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/cloud-providers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ type InstanceTypeSpec struct {
Memory int64
Arch string
GPUs int64
Image string
}

0 comments on commit 077b833

Please sign in to comment.