@@ -31,6 +31,7 @@ import (
31
31
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1"
32
32
"sigs.k8s.io/node-feature-discovery/pkg/utils"
33
33
"sigs.k8s.io/node-feature-discovery/pkg/utils/hostpath"
34
+ "sigs.k8s.io/node-feature-discovery/pkg/utils/kubeconf"
34
35
"sigs.k8s.io/node-feature-discovery/source"
35
36
)
36
37
@@ -54,13 +55,25 @@ type memorySource struct {
54
55
features * nfdv1alpha1.Features
55
56
}
56
57
58
+ // KubeletConfigPath holds the path to the kubelet configuration file.
59
+ type KubeletConfigPath struct {
60
+ ConfigFilePath string
61
+ }
62
+
57
63
// Singleton source instance
58
64
var (
59
- src memorySource
60
- _ source.FeatureSource = & src
61
- _ source.LabelSource = & src
65
+ src memorySource
66
+ _ source.FeatureSource = & src
67
+ _ source.LabelSource = & src
68
+ defaultSwapBehavior = "NoSwap"
62
69
)
63
70
71
+ var kubelet = KubeletConfigPath {}
72
+
73
+ func SetKubeletConfigPath (path string ) {
74
+ kubelet .ConfigFilePath = path
75
+ }
76
+
64
77
// Name returns an identifier string for this feature source.
65
78
func (s * memorySource ) Name () string { return Name }
66
79
@@ -80,6 +93,7 @@ func (s *memorySource) GetLabels() (source.FeatureLabels, error) {
80
93
// Swap
81
94
if isSwap , ok := features .Attributes [SwapFeature ].Elements ["enabled" ]; ok && isSwap == "true" {
82
95
labels ["swap" ] = true
96
+ labels ["swap.behavior" ] = features .Attributes [SwapFeature ].Elements ["behavior" ]
83
97
}
84
98
85
99
// NVDIMM
@@ -107,11 +121,19 @@ func (s *memorySource) Discover() error {
107
121
s .features .Attributes [NumaFeature ] = nfdv1alpha1.AttributeFeatureSet {Elements : numa }
108
122
}
109
123
110
- // Detect Swap
124
+ // Detect Swap and Swap Behavior
111
125
if swap , err := detectSwap (); err != nil {
112
126
klog .ErrorS (err , "failed to detect Swap nodes" )
113
127
} else {
114
128
s .features .Attributes [SwapFeature ] = nfdv1alpha1.AttributeFeatureSet {Elements : swap }
129
+ swapBehavior , err := detectSwapBehavior (kubelet .ConfigFilePath )
130
+ if err != nil {
131
+ klog .V (3 ).ErrorS (err , "failed to detect swap behavior; kubelet swapBehavior configuration may be missing or misconfigured" )
132
+ } else if swapBehavior == "" {
133
+ swap ["behavior" ] = defaultSwapBehavior
134
+ } else {
135
+ swap ["behavior" ] = swapBehavior
136
+ }
115
137
}
116
138
117
139
// Detect NVDIMM
@@ -155,6 +177,16 @@ func detectSwap() (map[string]string, error) {
155
177
}, nil
156
178
}
157
179
180
+ // detectSwapBehavior detects the swap behavior as configured in the kubelet.
181
+ func detectSwapBehavior (configFilePath string ) (string , error ) {
182
+ kubeletConfig , err := kubeconf .ReadKubeletConfig (configFilePath )
183
+ if err != nil {
184
+ return "" , fmt .Errorf ("failed to read kubelet configuration file %q: %w" , configFilePath , err )
185
+ }
186
+
187
+ return kubeletConfig .MemorySwap .SwapBehavior , nil
188
+ }
189
+
158
190
// detectNuma detects NUMA node information
159
191
func detectNuma () (map [string ]string , error ) {
160
192
sysfsBasePath := hostpath .SysfsDir .Path ("bus/node/devices" )
0 commit comments