runtime: Disable number of cpu comparison on remote hypervisor scenario

Fixes https://github.com/kata-containers/kata-containers/issues/9238

Signed-off-by: Ajay Victor <ajvictor@in.ibm.com>
This commit is contained in:
Ajay Victor
2024-05-17 19:21:10 +05:30
parent 9a6d8d8330
commit abe607b0c7
2 changed files with 49 additions and 4 deletions

View File

@@ -608,7 +608,7 @@ func addHypervisorHotColdPlugVfioOverrides(ocispec specs.Spec, sbConfig *vc.Sand
func addHypervisorMemoryOverrides(ocispec specs.Spec, sbConfig *vc.SandboxConfig, runtime RuntimeConfig) error {
if err := newAnnotationConfiguration(ocispec, vcAnnotations.DefaultMemory).setUintWithCheck(func(memorySz uint64) error {
if memorySz < vc.MinHypervisorMemory {
if memorySz < vc.MinHypervisorMemory && sbConfig.HypervisorType != vc.RemoteHypervisor {
return fmt.Errorf("Memory specified in annotation %s is less than minimum required %d, please specify a larger value", vcAnnotations.DefaultMemory, vc.MinHypervisorMemory)
}
sbConfig.HypervisorConfig.MemorySize = uint32(memorySz)
@@ -689,7 +689,7 @@ func addHypervisorCPUOverrides(ocispec specs.Spec, sbConfig *vc.SandboxConfig) e
numCPUs := goruntime.NumCPU()
if err := newAnnotationConfiguration(ocispec, vcAnnotations.DefaultVCPUs).setFloat32WithCheck(func(vcpus float32) error {
if vcpus > float32(numCPUs) {
if vcpus > float32(numCPUs) && sbConfig.HypervisorType != vc.RemoteHypervisor {
return fmt.Errorf("Number of cpus %f specified in annotation default_vcpus is greater than the number of CPUs %d on the system", vcpus, numCPUs)
}
sbConfig.HypervisorConfig.NumVCPUsF = float32(vcpus)
@@ -701,11 +701,11 @@ func addHypervisorCPUOverrides(ocispec specs.Spec, sbConfig *vc.SandboxConfig) e
return newAnnotationConfiguration(ocispec, vcAnnotations.DefaultMaxVCPUs).setUintWithCheck(func(maxVCPUs uint64) error {
max := uint32(maxVCPUs)
if max > uint32(numCPUs) {
if max > uint32(numCPUs) && sbConfig.HypervisorType != vc.RemoteHypervisor {
return fmt.Errorf("Number of cpus %d in annotation default_maxvcpus is greater than the number of CPUs %d on the system", max, numCPUs)
}
if sbConfig.HypervisorType == vc.QemuHypervisor && max > govmm.MaxVCPUs() {
if sbConfig.HypervisorType == vc.QemuHypervisor && max > govmm.MaxVCPUs() && sbConfig.HypervisorType != vc.RemoteHypervisor {
return fmt.Errorf("Number of cpus %d in annotation default_maxvcpus is greater than max no of CPUs %d supported for qemu", max, govmm.MaxVCPUs())
}
sbConfig.HypervisorConfig.DefaultMaxVCPUs = max

View File

@@ -726,6 +726,51 @@ func TestAddHypervisorAnnotations(t *testing.T) {
assert.Error(err)
}
func TestAddRemoteHypervisorAnnotations(t *testing.T) {
// Remote hypervisor uses DefaultVCPUs, DefaultMemory etc as annotations to pick the size of the separate VM to create,
// so doesn't need to be bound by the host's capacity limits.
assert := assert.New(t)
config := vc.SandboxConfig{
Annotations: make(map[string]string),
}
sbConfig := vc.SandboxConfig{
Annotations: make(map[string]string),
HypervisorType: vc.RemoteHypervisor,
}
ocispec := specs.Spec{
Annotations: make(map[string]string),
}
runtimeConfig := RuntimeConfig{
HypervisorType: vc.RemoteHypervisor,
}
err := addAnnotations(ocispec, &config, runtimeConfig)
assert.NoError(err)
assert.Exactly(vc.HypervisorConfig{}, config.HypervisorConfig)
// Enable annotations
runtimeConfig.HypervisorConfig.EnableAnnotations = []string{".*"}
// When DefaultVCPUs is more than the number of cpus on the host, remote hypervisor annotations don't throw an error
ocispec.Annotations[vcAnnotations.DefaultVCPUs] = "2000"
err = addAnnotations(ocispec, &sbConfig, runtimeConfig)
assert.NoError(err)
// When DefaultMaxVCPUs is more than the number of cpus on the host, remote hypervisor annotations don't throw an error
ocispec.Annotations[vcAnnotations.DefaultMaxVCPUs] = "2000"
err = addAnnotations(ocispec, &sbConfig, runtimeConfig)
assert.NoError(err)
// When memory is smaller than the minimum Hypervisor memory, remote hypervisor annotations don't throw an error
ocispec.Annotations[vcAnnotations.DefaultMemory] = "1"
err = addAnnotations(ocispec, &sbConfig, runtimeConfig)
assert.NoError(err)
}
func TestAddProtectedHypervisorAnnotations(t *testing.T) {
assert := assert.New(t)