Skip to content

Commit 4ab361b

Browse files
committed
Enforce cgroup limits at pod level
Signed-off-by: Harsh Rawat <[email protected]>
1 parent 9df3a80 commit 4ab361b

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

internal/guest/runtime/hcsv2/uvm.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (h *Host) CreateContainer(ctx context.Context, id string, settings *prot.VM
322322
}).Info("No memory limit found in sandbox container spec")
323323
}
324324

325-
if err := h.CreateVirtualPod(ctx, virtualPodID, virtualPodID, networkNamespace, memoryLimit); err != nil {
325+
if err := h.CreateVirtualPod(ctx, virtualPodID, virtualPodID, networkNamespace, settings.OCISpecification); err != nil {
326326
return nil, errors.Wrapf(err, "failed to create virtual pod %s", virtualPodID)
327327
}
328328
}
@@ -1305,7 +1305,7 @@ func (h *Host) InitializeVirtualPodSupport(virtualPodsCgroup cgroups.Cgroup) {
13051305
}
13061306

13071307
// 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 {
1308+
func (h *Host) CreateVirtualPod(ctx context.Context, virtualSandboxID, masterSandboxID, networkNamespace string, pSpec *specs.Spec) error {
13091309
h.virtualPodsMutex.Lock()
13101310
defer h.virtualPodsMutex.Unlock()
13111311

@@ -1327,18 +1327,15 @@ func (h *Host) CreateVirtualPod(ctx context.Context, virtualSandboxID, masterSan
13271327
}
13281328
cgroupPath := path.Join(parentPath, virtualSandboxID)
13291329

1330-
// Create the cgroup for this virtual pod with memory limit if provided
1330+
// Create the cgroup for this virtual pod with resource limits if provided
13311331
resources := &specs.LinuxResources{}
1332-
if memoryLimit != nil {
1333-
resources.Memory = &specs.LinuxMemory{
1334-
Limit: memoryLimit,
1335-
}
1332+
if pSpec != nil && pSpec.Linux != nil && pSpec.Linux.Resources != nil {
1333+
resources = pSpec.Linux.Resources
13361334
logrus.WithFields(logrus.Fields{
13371335
"virtualSandboxID": virtualSandboxID,
1338-
"memoryLimit": *memoryLimit,
1339-
}).Info("Creating virtual pod with memory limit")
1336+
}).Info("Creating virtual pod with specified resources")
13401337
} else {
1341-
logrus.WithField("virtualSandboxID", virtualSandboxID).Info("Creating virtual pod without memory limit")
1338+
logrus.WithField("virtualSandboxID", virtualSandboxID).Info("Creating pod cgroup with default resources as none were specified")
13421339
}
13431340

13441341
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)