Skip to content

Commit 963196d

Browse files
committed
Refactor minResourceLimits to filter by resourceName parameter
1 parent ffa88bc commit 963196d

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

pkg/quota/admission/clusterresourceoverride/admission.go

+14-25
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ func (a *clusterResourceOverridePlugin) Admit(attr admission.Attributes) error {
193193

194194
// Don't mutate resource requirements below the namespace
195195
// limit minimums.
196-
nsCPUFloor, nsMemFloor := minResourceLimits(namespaceLimits)
196+
nsCPUFloor := minResourceLimits(namespaceLimits, kapi.ResourceCPU)
197+
nsMemFloor := minResourceLimits(namespaceLimits, kapi.ResourceMemory)
197198

198199
// Reuse LimitRanger logic to apply limit/req defaults from the project. Ignore validation
199200
// errors, assume that LimitRanger will run after this plugin to validate.
@@ -268,39 +269,27 @@ func updateContainerResources(config *internalConfig, container *kapi.Container,
268269
}
269270
}
270271

271-
// minResourceLimits finds the minimum CPU and minimum Memory resource
272-
// values across all the limits in limitRanges. Nil is returned if
273-
// there is CPU or Memory resource respectively.
274-
func minResourceLimits(limitRanges []*kapi.LimitRange) (*resource.Quantity, *resource.Quantity) {
275-
cpuLimits := []*resource.Quantity{}
276-
memLimits := []*resource.Quantity{}
272+
// minResourceLimits finds the Min limit for resourceName. Nil is
273+
// returned if limitRanges is empty or limits contains no resourceName
274+
// limits.
275+
func minResourceLimits(limitRanges []*kapi.LimitRange, resourceName kapi.ResourceName) *resource.Quantity {
276+
limits := []*resource.Quantity{}
277277

278278
for _, limitRange := range limitRanges {
279279
for _, limit := range limitRange.Spec.Limits {
280-
if limit.Type != kapi.LimitTypeContainer {
281-
continue
282-
}
283-
if cpuLimit, cpuFound := limit.Min[kapi.ResourceCPU]; cpuFound {
284-
cpuLimits = append(cpuLimits, cpuLimit.Copy())
285-
}
286-
if memLimit, memFound := limit.Min[kapi.ResourceMemory]; memFound {
287-
memLimits = append(memLimits, memLimit.Copy())
280+
if limit.Type == kapi.LimitTypeContainer {
281+
if limit, found := limit.Min[resourceName]; found {
282+
limits = append(limits, limit.Copy())
283+
}
288284
}
289285
}
290286
}
291287

292-
var cpuMin *resource.Quantity
293-
var memMin *resource.Quantity
294-
295-
if len(cpuLimits) > 0 {
296-
cpuMin = minQuantity(cpuLimits)
297-
}
298-
299-
if len(memLimits) > 0 {
300-
memMin = minQuantity(memLimits)
288+
if len(limits) == 0 {
289+
return nil
301290
}
302291

303-
return cpuMin, memMin
292+
return minQuantity(limits)
304293
}
305294

306295
func minQuantity(quantities []*resource.Quantity) *resource.Quantity {

0 commit comments

Comments
 (0)