From a19f2eacec0c19f2a14b94dfaa90b625ebe439fd Mon Sep 17 00:00:00 2001 From: Ajay Victor Date: Tue, 3 Sep 2024 23:36:45 +0530 Subject: [PATCH] runtime: Enable ImageName annotation for remote hypervisor Enables ImageName to support multiple VM images in remote hypervisor scenario Fixes https://github.com/kata-containers/kata-containers/issues/10240 Signed-off-by: Ajay Victor --- .../config/configuration-remote.toml.in | 2 +- src/runtime/virtcontainers/remote.go | 1 + src/runtime/virtcontainers/sandbox.go | 18 +++++++++++++++--- src/runtime/virtcontainers/sandbox_test.go | 18 ++++++++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/runtime/config/configuration-remote.toml.in b/src/runtime/config/configuration-remote.toml.in index dabbea7cca..6e3651cf21 100644 --- a/src/runtime/config/configuration-remote.toml.in +++ b/src/runtime/config/configuration-remote.toml.in @@ -38,7 +38,7 @@ remote_hypervisor_timeout = 600 # Each member of the list is a regular expression, which is the base name # of the annotation, e.g. "path" for io.katacontainers.config.hypervisor.path" # Note: Remote hypervisor is only handling the following annotations -enable_annotations = ["machine_type", "default_memory", "default_vcpus"] +enable_annotations = ["machine_type", "default_memory", "default_vcpus", "image"] # Optional space-separated list of options to pass to the guest kernel. # For example, use `kernel_params = "vsyscall=emulate"` if you are having diff --git a/src/runtime/virtcontainers/remote.go b/src/runtime/virtcontainers/remote.go index d0bb1674c3..047f09fe8c 100644 --- a/src/runtime/virtcontainers/remote.go +++ b/src/runtime/virtcontainers/remote.go @@ -77,6 +77,7 @@ func (rh *remoteHypervisor) CreateVM(ctx context.Context, id string, network Net annotations[cri.SandboxName] = hypervisorConfig.SandboxName annotations[cri.SandboxNamespace] = hypervisorConfig.SandboxNamespace annotations[hypannotations.MachineType] = hypervisorConfig.HypervisorMachineType + annotations[hypannotations.ImagePath] = hypervisorConfig.ImagePath annotations[hypannotations.DefaultVCPUs] = strconv.FormatUint(uint64(hypervisorConfig.NumVCPUs()), 10) annotations[hypannotations.DefaultMemory] = strconv.FormatUint(uint64(hypervisorConfig.MemorySize), 10) annotations[hypannotations.Initdata] = hypervisorConfig.Initdata diff --git a/src/runtime/virtcontainers/sandbox.go b/src/runtime/virtcontainers/sandbox.go index b58daccaa6..8df2b7cc53 100644 --- a/src/runtime/virtcontainers/sandbox.go +++ b/src/runtime/virtcontainers/sandbox.go @@ -448,13 +448,25 @@ func createAssets(ctx context.Context, sandboxConfig *SandboxConfig) error { defer span.End() for _, name := range types.AssetTypes() { - a, err := types.NewAsset(sandboxConfig.Annotations, name) + annotation, _, err := name.Annotations() if err != nil { return err } + // For remote hypervisor donot check for Absolute Path incase of ImagePath, as it denotes the name of the image. + if sandboxConfig.HypervisorType == RemoteHypervisor && annotation == annotations.ImagePath { + value := sandboxConfig.Annotations[annotation] + if value != "" { + sandboxConfig.HypervisorConfig.ImagePath = value + } + } else { + a, err := types.NewAsset(sandboxConfig.Annotations, name) + if err != nil { + return err + } - if err := sandboxConfig.HypervisorConfig.AddCustomAsset(a); err != nil { - return err + if err := sandboxConfig.HypervisorConfig.AddCustomAsset(a); err != nil { + return err + } } } diff --git a/src/runtime/virtcontainers/sandbox_test.go b/src/runtime/virtcontainers/sandbox_test.go index 29980fdffb..114ff891a1 100644 --- a/src/runtime/virtcontainers/sandbox_test.go +++ b/src/runtime/virtcontainers/sandbox_test.go @@ -771,6 +771,24 @@ func TestSandboxCreateAssets(t *testing.T) { err = createAssets(context.Background(), config) assert.Error(err, msg) } + + // Remote Hypervisor scenario for ImagePath + msg := "test[image]: imagePath" + imagePathData := &testData{ + assetType: types.ImageAsset, + annotations: map[string]string{ + annotations.ImagePath: "rhel9-os", + }, + } + + config := &SandboxConfig{ + Annotations: imagePathData.annotations, + HypervisorConfig: hc, + HypervisorType: RemoteHypervisor, + } + + err = createAssets(context.Background(), config) + assert.NoError(err, msg) } func testFindContainerFailure(t *testing.T, sandbox *Sandbox, cid string) {