Skip to content

Commit 1ae091e

Browse files
committed
Add validation for the NodeSwapStatus field
Signed-off-by: Itamar Holder <[email protected]>
1 parent 9d38a85 commit 1ae091e

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

pkg/apis/core/validation/validation.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6424,6 +6424,7 @@ func ValidateNode(node *core.Node) field.ErrorList {
64246424
// All status fields are optional and can be updated later.
64256425
// That said, if specified, we need to ensure they are valid.
64266426
allErrs = append(allErrs, ValidateNodeResources(node)...)
6427+
allErrs = append(allErrs, validateNodeSwapStatus(node.Status.NodeInfo.Swap, fldPath.Child("nodeSwapStatus"))...)
64276428

64286429
// validate PodCIDRS only if we need to
64296430
if len(node.Spec.PodCIDRs) > 0 {
@@ -8769,3 +8770,22 @@ func IsValidIPForLegacyField(fldPath *field.Path, value string, validOldIPs []st
87698770
func IsValidCIDRForLegacyField(fldPath *field.Path, value string, validOldCIDRs []string) field.ErrorList {
87708771
return validation.IsValidCIDRForLegacyField(fldPath, value, utilfeature.DefaultFeatureGate.Enabled(features.StrictIPCIDRValidation), validOldCIDRs)
87718772
}
8773+
8774+
func validateNodeSwapStatus(nodeSwapStatus *core.NodeSwapStatus, fldPath *field.Path) field.ErrorList {
8775+
allErrors := field.ErrorList{}
8776+
8777+
if nodeSwapStatus == nil {
8778+
return allErrors
8779+
}
8780+
8781+
if nodeSwapStatus.Capacity != nil {
8782+
capacityFld := fldPath.Child("capacity")
8783+
8784+
errs := ValidatePositiveField(*nodeSwapStatus.Capacity, capacityFld)
8785+
if len(errs) > 0 {
8786+
allErrors = append(allErrors, errs...)
8787+
}
8788+
}
8789+
8790+
return allErrors
8791+
}

pkg/apis/core/validation/validation_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26758,3 +26758,64 @@ func TestValidatePodResize(t *testing.T) {
2675826758
})
2675926759
}
2676026760
}
26761+
26762+
func TestValidateNodeSwapStatus(t *testing.T) {
26763+
makeNode := func(nodeSwapStatus *core.NodeSwapStatus) core.Node {
26764+
node := makeNode("test-node", nil)
26765+
node.Status.NodeInfo.Swap = nodeSwapStatus
26766+
26767+
return node
26768+
}
26769+
makeSwapStatus := func(capacity int64) *core.NodeSwapStatus {
26770+
return &core.NodeSwapStatus{
26771+
Capacity: ptr.To(capacity),
26772+
}
26773+
}
26774+
26775+
testCases := []struct {
26776+
name string
26777+
expectError bool
26778+
node core.Node
26779+
}{
26780+
{
26781+
name: "node with nil nodeSwapStatus",
26782+
expectError: false,
26783+
node: makeNode(nil),
26784+
},
26785+
{
26786+
name: "node with nil nodeSwapStatus.Capacity",
26787+
expectError: false,
26788+
node: makeNode(&core.NodeSwapStatus{}),
26789+
},
26790+
{
26791+
name: "node with positive capacity",
26792+
expectError: false,
26793+
node: makeNode(makeSwapStatus(123456)),
26794+
},
26795+
{
26796+
name: "node with zero capacity should be invalid (nodeSwapStatus should be nil)",
26797+
expectError: true,
26798+
node: makeNode(makeSwapStatus(0)),
26799+
},
26800+
{
26801+
name: "node with negative capacity should be invalid",
26802+
expectError: true,
26803+
node: makeNode(makeSwapStatus(-123456)),
26804+
},
26805+
}
26806+
26807+
for _, tc := range testCases {
26808+
t.Run(tc.name, func(t *testing.T) {
26809+
errs := ValidateNode(&tc.node)
26810+
26811+
if len(errs) == 0 && tc.expectError {
26812+
t.Errorf("expected failure for %s, but there were none", tc.name)
26813+
return
26814+
}
26815+
if len(errs) != 0 && !tc.expectError {
26816+
t.Errorf("expected success for %s, but there were errors: %v", tc.name, errs)
26817+
return
26818+
}
26819+
})
26820+
}
26821+
}

0 commit comments

Comments
 (0)