Skip to content

Commit ddc0d94

Browse files
committed
dra API: ensure that pod status contains no duplicate resource claims
This is a follow-up to kubernetes#117351 which just got merged.
1 parent 745cfa3 commit ddc0d94

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

pkg/apis/core/validation/validation.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4871,13 +4871,19 @@ func validatePodConditions(conditions []core.PodCondition, fldPath *field.Path)
48714871
func validatePodResourceClaimStatuses(statuses []core.PodResourceClaimStatus, podClaims []core.PodResourceClaim, fldPath *field.Path) field.ErrorList {
48724872
var allErrs field.ErrorList
48734873

4874+
claimNames := sets.New[string]()
48744875
for i, status := range statuses {
48754876
idxPath := fldPath.Index(i)
48764877
// There's no need to check the content of the name. If it matches an entry,
48774878
// then it is valid, otherwise we reject it here.
48784879
if !havePodClaim(podClaims, status.Name) {
48794880
allErrs = append(allErrs, field.Invalid(idxPath.Child("name"), status.Name, "must match the name of an entry in `spec.resourceClaims`"))
48804881
}
4882+
if claimNames.Has(status.Name) {
4883+
allErrs = append(allErrs, field.Duplicate(idxPath.Child("name"), status.Name))
4884+
} else {
4885+
claimNames.Insert(status.Name)
4886+
}
48814887
if status.ResourceClaimName != nil {
48824888
for _, detail := range ValidateResourceClaimName(*status.ResourceClaimName, false) {
48834889
allErrs = append(allErrs, field.Invalid(idxPath.Child("name"), status.ResourceClaimName, detail))

pkg/apis/core/validation/validation_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13675,6 +13675,37 @@ func TestValidatePodStatusUpdate(t *testing.T) {
1367513675
},
1367613676
`status.resourceClaimStatuses[0].name: Invalid value: "%$!#": a lowercase RFC 1123 subdomain must consist of`,
1367713677
"Invalid ResourceClaim name",
13678+
}, {
13679+
core.Pod{
13680+
ObjectMeta: metav1.ObjectMeta{
13681+
Name: "foo",
13682+
},
13683+
Spec: core.PodSpec{
13684+
ResourceClaims: []core.PodResourceClaim{
13685+
{Name: "my-claim"},
13686+
{Name: "my-other-claim"},
13687+
},
13688+
},
13689+
Status: core.PodStatus{
13690+
ResourceClaimStatuses: []core.PodResourceClaimStatus{
13691+
{Name: "my-claim", ResourceClaimName: utilpointer.String("foo-my-claim-12345")},
13692+
{Name: "my-other-claim", ResourceClaimName: nil},
13693+
{Name: "my-other-claim", ResourceClaimName: nil},
13694+
},
13695+
},
13696+
},
13697+
core.Pod{
13698+
ObjectMeta: metav1.ObjectMeta{
13699+
Name: "foo",
13700+
},
13701+
Spec: core.PodSpec{
13702+
ResourceClaims: []core.PodResourceClaim{
13703+
{Name: "my-claim"},
13704+
},
13705+
},
13706+
},
13707+
`status.resourceClaimStatuses[2].name: Duplicate value: "my-other-claim"`,
13708+
"Duplicate ResourceClaimStatuses.Name",
1367813709
}, {
1367913710
core.Pod{
1368013711
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)