Skip to content

Commit 2499a2c

Browse files
authored
Merge pull request kubernetes#129954 from iholder101/swap/capacity-on-node-sys-info
[KEP-2400] Report swap capacity as part of node.status.nodeSystemInfo
2 parents 5639e1f + 1ae091e commit 2499a2c

File tree

22 files changed

+1723
-1140
lines changed

22 files changed

+1723
-1140
lines changed

api/openapi-spec/swagger.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/openapi-spec/v3/api__v1_openapi.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/core/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5190,6 +5190,15 @@ type NodeSystemInfo struct {
51905190
OperatingSystem string
51915191
// The Architecture reported by the node
51925192
Architecture string
5193+
// Swap Info reported by the node.
5194+
Swap *NodeSwapStatus
5195+
}
5196+
5197+
// NodeSwapStatus represents swap memory information.
5198+
type NodeSwapStatus struct {
5199+
// Total amount of swap memory in bytes.
5200+
// +optional
5201+
Capacity *int64
51935202
}
51945203

51955204
// NodeConfigStatus describes the status of the config assigned by Node.Spec.ConfigSource.

pkg/apis/core/v1/zz_generated.conversion.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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+
}

pkg/apis/core/zz_generated.deepcopy.go

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/generated/openapi/zz_generated.openapi.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/kubelet/nodestatus/setters.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
4545
"k8s.io/kubernetes/pkg/kubelet/events"
4646
netutils "k8s.io/utils/net"
47+
"k8s.io/utils/ptr"
4748

4849
"k8s.io/klog/v2"
4950
)
@@ -354,6 +355,12 @@ func MachineInfo(nodeName string,
354355
// node status.
355356
node.Status.Capacity[v1.ResourceName(removedResource)] = *resource.NewQuantity(int64(0), resource.DecimalSI)
356357
}
358+
359+
if utilfeature.DefaultFeatureGate.Enabled(features.NodeSwap) && info.SwapCapacity != 0 {
360+
node.Status.NodeInfo.Swap = &v1.NodeSwapStatus{
361+
Capacity: ptr.To(int64(info.SwapCapacity)),
362+
}
363+
}
357364
}
358365

359366
// Set Allocatable.

0 commit comments

Comments
 (0)