Skip to content

Commit

Permalink
runtime: newContainer: Handle the annotations of SWAP
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
teawater committed Jul 19, 2021
1 parent 2c835b6 commit a733f53
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/runtime/virtcontainers/container.go
Expand Up @@ -13,13 +13,15 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"syscall"
"time"

"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils/katatrace"
"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"
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions src/runtime/virtcontainers/pkg/annotations/annotations.go
Expand Up @@ -9,6 +9,7 @@ const (
kataAnnotationsPrefix = "io.katacontainers."
kataConfAnnotationsPrefix = kataAnnotationsPrefix + "config."
kataAnnotHypervisorPrefix = kataConfAnnotationsPrefix + "hypervisor."
kataAnnotContainerPrefix = kataAnnotationsPrefix + "container."

//
// OCI
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a733f53

Please sign in to comment.