diff --git a/src/runtime/virtcontainers/container.go b/src/runtime/virtcontainers/container.go index b98c4f7eb4..6440bd7fb5 100644 --- a/src/runtime/virtcontainers/container.go +++ b/src/runtime/virtcontainers/container.go @@ -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 { diff --git a/src/runtime/virtcontainers/pkg/annotations/annotations.go b/src/runtime/virtcontainers/pkg/annotations/annotations.go index 8e4306aef6..1ac2497b6a 100644 --- a/src/runtime/virtcontainers/pkg/annotations/annotations.go +++ b/src/runtime/virtcontainers/pkg/annotations/annotations.go @@ -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"