From e6408fe670be13f81053158da0694b597819444a Mon Sep 17 00:00:00 2001 From: Hui Zhu Date: Mon, 2 Aug 2021 15:56:26 +0800 Subject: [PATCH] Container: Add initConfigResourcesMemory and call it in newContainer The swappiness is not right if just set io.katacontainers.container.resource.swappiness: $ pod_yaml=pod.yaml $ container_yaml=container.yaml $ image="quay.io/prometheus/busybox:latest" $ cat << EOF > "${pod_yaml}" metadata: name: busybox-sandbox1 EOF $ cat << EOF > "${container_yaml}" metadata: name: busybox-killed-vmm annotations: io.katacontainers.container.resource.swappiness: "100" image: image: "$image" command: - top EOF $ sudo crictl pull $image $ podid=$(sudo crictl runp $pod_yaml) $ cid=$(sudo crictl create $podid $container_yaml $pod_yaml) $ sudo crictl start $cid crictl exec $cid cat /sys/fs/cgroup/memory/memory.swappiness 60 The cause of this issue is there are two elements store the resources infomation. They are c.config.Resources for calculateSandboxMemory and c.GetPatchedOCISpec() for agent. This add initConfigResourcesMemory to Container and call it in newContainer to handle the issue. Fixes: #2372 Signed-off-by: Hui Zhu --- src/runtime/virtcontainers/container.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/runtime/virtcontainers/container.go b/src/runtime/virtcontainers/container.go index 4e6fc5380..a2d1eefe8 100644 --- a/src/runtime/virtcontainers/container.go +++ b/src/runtime/virtcontainers/container.go @@ -745,6 +745,12 @@ func (c *Container) createBlockDevices(ctx context.Context) error { return nil } +func (c *Container) initConfigResourcesMemory() { + ociSpec := c.GetPatchedOCISpec() + c.config.Resources.Memory = &specs.LinuxMemory{} + ociSpec.Linux.Resources.Memory = c.config.Resources.Memory +} + // newContainer creates a Container structure from a sandbox and a container configuration. func newContainer(ctx context.Context, sandbox *Sandbox, contConfig *ContainerConfig) (*Container, error) { span, ctx := katatrace.Trace(ctx, sandbox.Logger(), "newContainer", sandbox.tracingTags()) @@ -778,7 +784,7 @@ func newContainer(ctx context.Context, sandbox *Sandbox, contConfig *ContainerCo return &Container{}, fmt.Errorf("Invalid container configuration Annotations %s %v", vcAnnotations.ContainerResourcesSwappiness, err) } if c.config.Resources.Memory == nil { - c.config.Resources.Memory = &specs.LinuxMemory{} + c.initConfigResourcesMemory() } c.config.Resources.Memory.Swappiness = &resourceSwappiness } @@ -788,7 +794,7 @@ func newContainer(ctx context.Context, sandbox *Sandbox, contConfig *ContainerCo return &Container{}, fmt.Errorf("Invalid container configuration Annotations %s %v", vcAnnotations.ContainerResourcesSwapInBytes, err) } if c.config.Resources.Memory == nil { - c.config.Resources.Memory = &specs.LinuxMemory{} + c.initConfigResourcesMemory() } resourceSwapInBytes := int64(resourceSwapInBytesInUint) c.config.Resources.Memory.Swap = &resourceSwapInBytes