Skip to content

Commit f2cdd65

Browse files
committed
Enforce cgroup limits at pod level
Signed-off-by: Harsh Rawat <[email protected]>
1 parent 73fdc96 commit f2cdd65

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

internal/guest/runtime/hcsv2/uvm.go

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -303,26 +303,7 @@ func (h *Host) CreateContainer(ctx context.Context, id string, settings *prot.VM
303303
networkNamespace = fmt.Sprintf("virtual-pod-%s", virtualPodID)
304304
}
305305

306-
// Extract memory limit from sandbox container spec
307-
var memoryLimit *int64
308-
if settings.OCISpecification.Linux != nil &&
309-
settings.OCISpecification.Linux.Resources != nil &&
310-
settings.OCISpecification.Linux.Resources.Memory != nil &&
311-
settings.OCISpecification.Linux.Resources.Memory.Limit != nil {
312-
memoryLimit = settings.OCISpecification.Linux.Resources.Memory.Limit
313-
logrus.WithFields(logrus.Fields{
314-
"containerID": id,
315-
"virtualPodID": virtualPodID,
316-
"memoryLimit": *memoryLimit,
317-
}).Info("Extracted memory limit from sandbox container spec")
318-
} else {
319-
logrus.WithFields(logrus.Fields{
320-
"containerID": id,
321-
"virtualPodID": virtualPodID,
322-
}).Info("No memory limit found in sandbox container spec")
323-
}
324-
325-
if err := h.CreateVirtualPod(ctx, virtualPodID, virtualPodID, networkNamespace, memoryLimit); err != nil {
306+
if err := h.CreateVirtualPod(ctx, virtualPodID, virtualPodID, networkNamespace, settings.OCISpecification); err != nil {
326307
return nil, errors.Wrapf(err, "failed to create virtual pod %s", virtualPodID)
327308
}
328309
}
@@ -1305,7 +1286,7 @@ func (h *Host) InitializeVirtualPodSupport(virtualPodsCgroup cgroups.Cgroup) {
13051286
}
13061287

13071288
// CreateVirtualPod creates a new virtual pod with its own cgroup and network namespace
1308-
func (h *Host) CreateVirtualPod(ctx context.Context, virtualSandboxID, masterSandboxID, networkNamespace string, memoryLimit *int64) error {
1289+
func (h *Host) CreateVirtualPod(ctx context.Context, virtualSandboxID, masterSandboxID, networkNamespace string, pSpec *specs.Spec) error {
13091290
h.virtualPodsMutex.Lock()
13101291
defer h.virtualPodsMutex.Unlock()
13111292

@@ -1327,18 +1308,15 @@ func (h *Host) CreateVirtualPod(ctx context.Context, virtualSandboxID, masterSan
13271308
}
13281309
cgroupPath := path.Join(parentPath, virtualSandboxID)
13291310

1330-
// Create the cgroup for this virtual pod with memory limit if provided
1311+
// Create the cgroup for this virtual pod with resource limits if provided
13311312
resources := &specs.LinuxResources{}
1332-
if memoryLimit != nil {
1333-
resources.Memory = &specs.LinuxMemory{
1334-
Limit: memoryLimit,
1335-
}
1313+
if pSpec != nil && pSpec.Linux != nil && pSpec.Linux.Resources != nil {
1314+
resources = pSpec.Linux.Resources
13361315
logrus.WithFields(logrus.Fields{
13371316
"virtualSandboxID": virtualSandboxID,
1338-
"memoryLimit": *memoryLimit,
1339-
}).Info("Creating virtual pod with memory limit")
1317+
}).Info("Creating virtual pod with specified resources")
13401318
} else {
1341-
logrus.WithField("virtualSandboxID", virtualSandboxID).Info("Creating virtual pod without memory limit")
1319+
logrus.WithField("virtualSandboxID", virtualSandboxID).Info("Creating pod cgroup with default resources as none were specified")
13421320
}
13431321

13441322
cgroupControl, err := cgroups.New(cgroups.StaticPath(cgroupPath), resources)

internal/hcsoci/hcsdoc_lcow.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ func createLCOWSpec(ctx context.Context, coi *createOptionsInternal) (*specs.Spe
3838
// Hooks are not supported (they should be run in the host)
3939
spec.Hooks = nil
4040

41+
// Set default CPU period and quota if not set for LCOW containers.
42+
if spec.Linux != nil &&
43+
spec.Linux.Resources != nil &&
44+
spec.Linux.Resources.CPU != nil {
45+
46+
if spec.Linux.Resources.CPU.Period != nil && *spec.Linux.Resources.CPU.Period == 0 {
47+
*spec.Linux.Resources.CPU.Period = 100000 // Default CPU period
48+
}
49+
if spec.Linux.Resources.CPU.Quota != nil && *spec.Linux.Resources.CPU.Quota == 0 {
50+
*spec.Linux.Resources.CPU.Quota = -1 // No CPU limit
51+
}
52+
}
53+
4154
// Clear unsupported features
4255
spec.Linux.CgroupsPath = "" // GCS controls its cgroups hierarchy on its own.
4356
if spec.Linux.Resources != nil {

0 commit comments

Comments
 (0)