Skip to content

Commit 20419fe

Browse files
committed
cri, sandbox: pass sandbox resource details if available, applicable
CRI API has been updated to include a an optional `resources` field in the LinuxPodSandboxConfig field, as part of the RunPodSandbox request. Having sandbox level resource details at sandbox creation time will have large benefits for sandboxed runtimes. In the case of Kata Containers, for example, this'll allow for better support of SW/HW architectures which don't allow for CPU/memory hotplug, and it'll allow for better queue sizing for virtio devices associated with the sandbox (in the VM case). If this sandbox resource information is provided as part of the run sandbox request, let's introduce a pattern where we will update the pause container's runtiem spec to include this information in the annotations field. Signed-off-by: Eric Ernst <eric_ernst@apple.com>
1 parent 6e9e759 commit 20419fe

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

pkg/cri/annotations/annotations.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ const (
3232
// SandboxID is the sandbox ID annotation
3333
SandboxID = "io.kubernetes.cri.sandbox-id"
3434

35+
// SandboxCPU annotations are based on the initial CPU configuration for the sandbox. This is calculated as the
36+
// sum of container CPU resources, optionally provided by Kubelet (introduced in 1.23) as part of the PodSandboxConfig
37+
SandboxCPUPeriod = "io.kubernetes.cri.sandbox-cpu-period"
38+
SandboxCPUQuota = "io.kubernetes.cri.sandbox-cpu-quota"
39+
SandboxCPUShares = "io.kubernetes.cri.sandbox-cpu-shares"
40+
41+
// SandboxMemory is the initial amount of memory associated with this sandbox. This is calculated as the sum
42+
// of container memory, optionally provided by Kubelet (introduced in 1.23) as part of the PodSandboxConfig.
43+
SandboxMem = "io.kubernetes.cri.sandbox-memory"
44+
3545
// SandboxLogDir is the pod log directory annotation.
3646
// If the sandbox needs to generate any log, it will put it into this directory.
3747
// Kubelet will be responsible for:

pkg/cri/server/sandbox_run_linux.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package server
1919
import (
2020
"fmt"
2121
"os"
22+
"strconv"
2223
"strings"
2324

2425
"github.com/containerd/containerd"
@@ -155,6 +156,15 @@ func (c *criService) sandboxContainerSpec(id string, config *runtime.PodSandboxC
155156
if !c.config.DisableCgroup {
156157
specOpts = append(specOpts, customopts.WithDefaultSandboxShares)
157158
}
159+
160+
if res := config.GetLinux().GetResources(); res != nil {
161+
specOpts = append(specOpts,
162+
customopts.WithAnnotation(annotations.SandboxCPUPeriod, strconv.FormatInt(res.CpuPeriod, 10)),
163+
customopts.WithAnnotation(annotations.SandboxCPUQuota, strconv.FormatInt(res.CpuQuota, 10)),
164+
customopts.WithAnnotation(annotations.SandboxCPUShares, strconv.FormatInt(res.CpuShares, 10)),
165+
customopts.WithAnnotation(annotations.SandboxMem, strconv.FormatInt(res.MemoryLimitInBytes, 10)))
166+
}
167+
158168
specOpts = append(specOpts, customopts.WithPodOOMScoreAdj(int(defaultSandboxOOMAdj), c.config.RestrictOOMScoreAdj))
159169

160170
for pKey, pValue := range getPassthroughAnnotations(config.Annotations,

pkg/cri/server/sandbox_run_linux_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package server
1919
import (
2020
"os"
2121
"path/filepath"
22+
"strconv"
2223
"testing"
2324

2425
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -27,6 +28,7 @@ import (
2728
"github.com/stretchr/testify/assert"
2829
"github.com/stretchr/testify/require"
2930
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
31+
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
3032

3133
"github.com/containerd/containerd/pkg/cri/annotations"
3234
"github.com/containerd/containerd/pkg/cri/opts"
@@ -173,6 +175,65 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
173175
assert.Contains(t, spec.Linux.Sysctl["net.ipv4.ping_group_range"], "1 1000")
174176
},
175177
},
178+
"sandbox sizing annotations should be set if LinuxContainerResources were provided": {
179+
configChange: func(c *runtime.PodSandboxConfig) {
180+
c.Linux.Resources = &v1.LinuxContainerResources{
181+
CpuPeriod: 100,
182+
CpuQuota: 200,
183+
CpuShares: 5000,
184+
MemoryLimitInBytes: 1024,
185+
}
186+
},
187+
specCheck: func(t *testing.T, spec *runtimespec.Spec) {
188+
value, ok := spec.Annotations[annotations.SandboxCPUPeriod]
189+
assert.True(t, ok)
190+
assert.EqualValues(t, strconv.FormatInt(100, 10), value)
191+
assert.EqualValues(t, "100", value)
192+
193+
value, ok = spec.Annotations[annotations.SandboxCPUQuota]
194+
assert.True(t, ok)
195+
assert.EqualValues(t, "200", value)
196+
197+
value, ok = spec.Annotations[annotations.SandboxCPUShares]
198+
assert.True(t, ok)
199+
assert.EqualValues(t, "5000", value)
200+
201+
value, ok = spec.Annotations[annotations.SandboxMem]
202+
assert.True(t, ok)
203+
assert.EqualValues(t, "1024", value)
204+
},
205+
},
206+
"sandbox sizing annotations should not be set if LinuxContainerResources were not provided": {
207+
specCheck: func(t *testing.T, spec *runtimespec.Spec) {
208+
_, ok := spec.Annotations[annotations.SandboxCPUPeriod]
209+
assert.False(t, ok)
210+
_, ok = spec.Annotations[annotations.SandboxCPUQuota]
211+
assert.False(t, ok)
212+
_, ok = spec.Annotations[annotations.SandboxCPUShares]
213+
assert.False(t, ok)
214+
_, ok = spec.Annotations[annotations.SandboxMem]
215+
assert.False(t, ok)
216+
},
217+
},
218+
"sandbox sizing annotations are zero if the resources are set to 0": {
219+
configChange: func(c *runtime.PodSandboxConfig) {
220+
c.Linux.Resources = &v1.LinuxContainerResources{}
221+
},
222+
specCheck: func(t *testing.T, spec *runtimespec.Spec) {
223+
value, ok := spec.Annotations[annotations.SandboxCPUPeriod]
224+
assert.True(t, ok)
225+
assert.EqualValues(t, "0", value)
226+
value, ok = spec.Annotations[annotations.SandboxCPUQuota]
227+
assert.True(t, ok)
228+
assert.EqualValues(t, "0", value)
229+
value, ok = spec.Annotations[annotations.SandboxCPUShares]
230+
assert.True(t, ok)
231+
assert.EqualValues(t, "0", value)
232+
value, ok = spec.Annotations[annotations.SandboxMem]
233+
assert.True(t, ok)
234+
assert.EqualValues(t, "0", value)
235+
},
236+
},
176237
} {
177238
t.Logf("TestCase %q", desc)
178239
c := newTestCRIService()

0 commit comments

Comments
 (0)