mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-04 18:00:08 +00:00
Merge pull request #57170 from jiayingz/validation
Automatic merge from submit-queue (batch tested with PRs 57037, 57170). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Invalidate resource requirements on extended resources with only request set **What this PR does / why we need it**: **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes https://github.com/kubernetes/kubernetes/issues/57276 **Special notes for your reviewer**: **Release note**: ```release-note Returns an error for non overcommitable resources if they don't have limit field set in container spec. ```
This commit is contained in:
commit
4bdf282e0e
@ -4420,14 +4420,14 @@ func ValidateResourceRequirements(requirements *core.ResourceRequirements, fldPa
|
|||||||
// Check that request <= limit.
|
// Check that request <= limit.
|
||||||
limitQuantity, exists := requirements.Limits[resourceName]
|
limitQuantity, exists := requirements.Limits[resourceName]
|
||||||
if exists {
|
if exists {
|
||||||
// For GPUs, not only requests can't exceed limits, they also can't be lower, i.e. must be equal.
|
// For non overcommitable resources, not only requests can't exceed limits, they also can't be lower, i.e. must be equal.
|
||||||
if quantity.Cmp(limitQuantity) != 0 && !helper.IsOvercommitAllowed(resourceName) {
|
if quantity.Cmp(limitQuantity) != 0 && !helper.IsOvercommitAllowed(resourceName) {
|
||||||
allErrs = append(allErrs, field.Invalid(reqPath, quantity.String(), fmt.Sprintf("must be equal to %s limit", resourceName)))
|
allErrs = append(allErrs, field.Invalid(reqPath, quantity.String(), fmt.Sprintf("must be equal to %s limit", resourceName)))
|
||||||
} else if quantity.Cmp(limitQuantity) > 0 {
|
} else if quantity.Cmp(limitQuantity) > 0 {
|
||||||
allErrs = append(allErrs, field.Invalid(reqPath, quantity.String(), fmt.Sprintf("must be less than or equal to %s limit", resourceName)))
|
allErrs = append(allErrs, field.Invalid(reqPath, quantity.String(), fmt.Sprintf("must be less than or equal to %s limit", resourceName)))
|
||||||
}
|
}
|
||||||
} else if resourceName == core.ResourceNvidiaGPU {
|
} else if !helper.IsOvercommitAllowed(resourceName) {
|
||||||
allErrs = append(allErrs, field.Invalid(reqPath, quantity.String(), fmt.Sprintf("must be equal to %s request", core.ResourceNvidiaGPU)))
|
allErrs = append(allErrs, field.Required(limPath, "Limit must be set for non overcommitable resources"))
|
||||||
}
|
}
|
||||||
if helper.IsHugePageResourceName(resourceName) {
|
if helper.IsHugePageResourceName(resourceName) {
|
||||||
reqContainsHugePages = true
|
reqContainsHugePages = true
|
||||||
|
@ -6879,6 +6879,28 @@ func TestValidatePod(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"invalid extended resource requirement without limit": {
|
||||||
|
expectedError: "Limit must be set",
|
||||||
|
spec: core.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Name: "123", Namespace: "ns"},
|
||||||
|
Spec: core.PodSpec{
|
||||||
|
Containers: []core.Container{
|
||||||
|
{
|
||||||
|
Name: "invalid",
|
||||||
|
Image: "image",
|
||||||
|
ImagePullPolicy: "IfNotPresent",
|
||||||
|
Resources: core.ResourceRequirements{
|
||||||
|
Requests: core.ResourceList{
|
||||||
|
core.ResourceName("example.com/a"): resource.MustParse("2"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
RestartPolicy: core.RestartPolicyAlways,
|
||||||
|
DNSPolicy: core.DNSClusterFirst,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
"invalid fractional extended resource in container request": {
|
"invalid fractional extended resource in container request": {
|
||||||
expectedError: "must be an integer",
|
expectedError: "must be an integer",
|
||||||
spec: core.Pod{
|
spec: core.Pod{
|
||||||
|
Loading…
Reference in New Issue
Block a user