runtime: newContainer: Handle the annotations of SWAP

This commit add code to handle the annotations
"io.katacontainers.container.resource.swappiness" and
"io.katacontainers.container.resource.swap_in_bytes".
It will set the value of "io.katacontainers.resource.swappiness" to
c.config.Resources.Memory.Swappiness and set the value of
"io.katacontainers.resource.swap_in_bytes" to
c.config.Resources.Memory.Swap.

Fixes: #2201

Signed-off-by: Hui Zhu <teawater@antfin.com>
This commit is contained in:
Hui Zhu 2021-07-08 14:51:01 +08:00
parent 2c835b60ed
commit a733f537e5
2 changed files with 34 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"syscall"
"time"
@ -20,6 +21,7 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/config"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/device/manager"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/agent/protocols/grpc"
vcAnnotations "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/annotations"
vccgroups "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/cgroups"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/rootless"
vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/types"
@ -767,6 +769,26 @@ func newContainer(ctx context.Context, sandbox *Sandbox, contConfig *ContainerCo
ctx: sandbox.ctx,
}
// Set the Annotations of SWAP to Resources
if resourceSwappinessStr, ok := c.config.Annotations[vcAnnotations.ContainerResourcesSwappiness]; ok {
resourceSwappiness, err := strconv.ParseUint(resourceSwappinessStr, 0, 64)
if err == nil && resourceSwappiness > 200 {
err = fmt.Errorf("swapiness should not bigger than 200")
}
if err != nil {
return &Container{}, fmt.Errorf("Invalid container configuration Annotations %s %v", vcAnnotations.ContainerResourcesSwappiness, err)
}
c.config.Resources.Memory.Swappiness = &resourceSwappiness
}
if resourceSwapInBytesStr, ok := c.config.Annotations[vcAnnotations.ContainerResourcesSwapInBytes]; ok {
resourceSwapInBytesInUint, err := strconv.ParseUint(resourceSwapInBytesStr, 0, 64)
if err != nil {
return &Container{}, fmt.Errorf("Invalid container configuration Annotations %s %v", vcAnnotations.ContainerResourcesSwapInBytes, err)
}
resourceSwapInBytes := int64(resourceSwapInBytesInUint)
c.config.Resources.Memory.Swap = &resourceSwapInBytes
}
// experimental runtime use "persist.json" instead of legacy "state.json" as storage
err := c.Restore()
if err == nil {

View File

@ -9,6 +9,7 @@ const (
kataAnnotationsPrefix = "io.katacontainers."
kataConfAnnotationsPrefix = kataAnnotationsPrefix + "config."
kataAnnotHypervisorPrefix = kataConfAnnotationsPrefix + "hypervisor."
kataAnnotContainerPrefix = kataAnnotationsPrefix + "container."
//
// OCI
@ -277,6 +278,17 @@ const (
ContainerPipeSizeKernelParam = "agent." + ContainerPipeSizeOption
)
// Container resource related annotations
const (
kataAnnotContainerResourcePrefix = kataAnnotContainerPrefix + "resource."
// ContainerResourcesSwappiness is a container annotation to specify the Resources.Memory.Swappiness
ContainerResourcesSwappiness = kataAnnotContainerResourcePrefix + "swappiness"
// ContainerResourcesSwapInBytes is a container annotation to specify the Resources.Memory.Swap
ContainerResourcesSwapInBytes = kataAnnotContainerResourcePrefix + "swap_in_bytes"
)
const (
// SHA512 is the SHA-512 (64) hash algorithm
SHA512 string = "sha512"