@@ -31,6 +31,7 @@ import (
3131 nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1"
3232 "sigs.k8s.io/node-feature-discovery/pkg/utils"
3333 "sigs.k8s.io/node-feature-discovery/pkg/utils/hostpath"
34+ "sigs.k8s.io/node-feature-discovery/pkg/utils/kubeconf"
3435 "sigs.k8s.io/node-feature-discovery/source"
3536)
3637
@@ -54,13 +55,25 @@ type memorySource struct {
5455 features * nfdv1alpha1.Features
5556}
5657
58+ // KubeletConfigPath holds the path to the kubelet configuration file.
59+ type KubeletConfigPath struct {
60+ ConfigFilePath string
61+ }
62+
5763// Singleton source instance
5864var (
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"
6269)
6370
71+ var kubelet = KubeletConfigPath {}
72+
73+ func SetKubeletConfigPath (path string ) {
74+ kubelet .ConfigFilePath = path
75+ }
76+
6477// Name returns an identifier string for this feature source.
6578func (s * memorySource ) Name () string { return Name }
6679
@@ -80,6 +93,7 @@ func (s *memorySource) GetLabels() (source.FeatureLabels, error) {
8093 // Swap
8194 if isSwap , ok := features .Attributes [SwapFeature ].Elements ["enabled" ]; ok && isSwap == "true" {
8295 labels ["swap" ] = true
96+ labels ["swap.behavior" ] = features .Attributes [SwapFeature ].Elements ["behavior" ]
8397 }
8498
8599 // NVDIMM
@@ -107,11 +121,19 @@ func (s *memorySource) Discover() error {
107121 s .features .Attributes [NumaFeature ] = nfdv1alpha1.AttributeFeatureSet {Elements : numa }
108122 }
109123
110- // Detect Swap
124+ // Detect Swap and Swap Behavior
111125 if swap , err := detectSwap (); err != nil {
112126 klog .ErrorS (err , "failed to detect Swap nodes" )
113127 } else {
114128 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+ }
115137 }
116138
117139 // Detect NVDIMM
@@ -155,6 +177,16 @@ func detectSwap() (map[string]string, error) {
155177 }, nil
156178}
157179
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+
158190// detectNuma detects NUMA node information
159191func detectNuma () (map [string ]string , error ) {
160192 sysfsBasePath := hostpath .SysfsDir .Path ("bus/node/devices" )
0 commit comments