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.resource.swappiness" and
"io.katacontainers.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: kata-containers#2201

Signed-off-by: Hui Zhu <teawater@antfin.com>
  • Loading branch information
teawater committed Jul 11, 2021
1 parent 66a95ed commit 9ca0dc9
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/runtime/virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"syscall"
"time"

Expand All @@ -35,6 +36,11 @@ import (
"golang.org/x/sys/unix"
)

const (
resourceSwappinessLabel = "io.katacontainers.resource.swappiness"
resourceSwapInBytesLabel = "io.katacontainers.resource.swap_in_bytes"
)

// https://github.com/torvalds/linux/blob/master/include/uapi/linux/major.h
// This file has definitions for major device numbers.
var cdromMajors = map[int64]string{
Expand Down Expand Up @@ -772,6 +778,25 @@ 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[resourceSwappinessLabel]; 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", resourceSwappinessLabel, err)
}
c.config.Resources.Memory.Swappiness = &resourceSwappiness
}
if resourceSwapInBytesStr, ok := c.config.Annotations[resourceSwapInBytesLabel]; ok {
resourceSwapInBytes, err := strconv.ParseInt(resourceSwapInBytesStr, 0, 64)
if err != nil {
return &Container{}, fmt.Errorf("Invalid container configuration Annotations %s %v", resourceSwapInBytesLabel, err)
}
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

0 comments on commit 9ca0dc9

Please sign in to comment.