@@ -20,6 +20,7 @@ import (
2020 "encoding/json"
2121 "fmt"
2222 "net/http"
23+ "net/url"
2324 "os"
2425 "path/filepath"
2526 "regexp"
@@ -38,8 +39,10 @@ import (
3839 "k8s.io/apimachinery/pkg/util/validation"
3940 k8sclient "k8s.io/client-go/kubernetes"
4041 "k8s.io/klog/v2"
42+ kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
4143 "k8s.io/utils/ptr"
4244 klogutils "sigs.k8s.io/node-feature-discovery/pkg/utils/klog"
45+ "sigs.k8s.io/node-feature-discovery/pkg/utils/kubeconf"
4346 "sigs.k8s.io/yaml"
4447
4548 apiequality "k8s.io/apimachinery/pkg/api/equality"
@@ -56,7 +59,7 @@ import (
5659 _ "sigs.k8s.io/node-feature-discovery/source/fake"
5760 _ "sigs.k8s.io/node-feature-discovery/source/kernel"
5861 _ "sigs.k8s.io/node-feature-discovery/source/local"
59- _ "sigs.k8s.io/node-feature-discovery/source/memory"
62+ memory "sigs.k8s.io/node-feature-discovery/source/memory"
6063 _ "sigs.k8s.io/node-feature-discovery/source/network"
6164 _ "sigs.k8s.io/node-feature-discovery/source/pci"
6265 _ "sigs.k8s.io/node-feature-discovery/source/storage"
@@ -94,13 +97,16 @@ type Labels map[string]string
9497
9598// Args are the command line arguments of NfdWorker.
9699type Args struct {
97- ConfigFile string
98- Klog map [string ]* utils.KlogFlagVal
99- Kubeconfig string
100- Oneshot bool
101- Options string
102- Port int
103- NoOwnerRefs bool
100+ ConfigFile string
101+ Klog map [string ]* utils.KlogFlagVal
102+ Kubeconfig string
103+ Oneshot bool
104+ Options string
105+ Port int
106+ NoOwnerRefs bool
107+ KubeletConfigPath string
108+ KubeletConfigURI string
109+ APIAuthTokenFile string
104110
105111 Overrides ConfigOverrideArgs
106112}
@@ -124,6 +130,7 @@ type nfdWorker struct {
124130 featureSources []source.FeatureSource
125131 labelSources []source.LabelSource
126132 ownerReference []metav1.OwnerReference
133+ kubeletConfigFunc func () (* kubeletconfigv1beta1.KubeletConfiguration , error )
127134}
128135
129136// This ticker can represent infinite and normal intervals.
@@ -169,12 +176,25 @@ func NewNfdWorker(opts ...NfdWorkerOption) (NfdWorker, error) {
169176 stop : make (chan struct {}),
170177 }
171178
179+ if nfd .args .ConfigFile != "" {
180+ nfd .configFilePath = filepath .Clean (nfd .args .ConfigFile )
181+ }
182+
172183 for _ , o := range opts {
173184 o .apply (nfd )
174185 }
175186
176- if nfd .args .ConfigFile != "" {
177- nfd .configFilePath = filepath .Clean (nfd .args .ConfigFile )
187+ kubeletConfigFunc , err := getKubeletConfigFunc (nfd .args .KubeletConfigURI , nfd .args .APIAuthTokenFile )
188+ if err != nil {
189+ return nil , err
190+ }
191+
192+ nfd = & nfdWorker {
193+ kubeletConfigFunc : kubeletConfigFunc ,
194+ }
195+
196+ for _ , o := range opts {
197+ o .apply (nfd )
178198 }
179199
180200 // k8sClient might've been set via opts by tests
@@ -239,6 +259,8 @@ func (w *nfdWorker) runFeatureDiscovery() error {
239259 }
240260 // Get the set of feature labels.
241261 labels := createFeatureLabels (w .labelSources , w .config .Core .LabelWhiteList .Regexp )
262+ // Append a label with app=nfd
263+ labels ["app" ] = "nfd"
242264
243265 // Update the node with the feature labels.
244266 if ! w .config .Core .NoPublish {
@@ -255,9 +277,10 @@ func (w *nfdWorker) setOwnerReference() error {
255277 if ! w .config .Core .NoOwnerRefs {
256278 // Get pod owner reference
257279 podName := os .Getenv ("POD_NAME" )
280+ podNamespace := os .Getenv ("POD_NAMESPACE" )
258281 // Add pod owner reference if it exists
259282 if podName != "" {
260- if selfPod , err := w .k8sClient .CoreV1 ().Pods (w . kubernetesNamespace ).Get (context .TODO (), podName , metav1.GetOptions {}); err != nil {
283+ if selfPod , err := w .k8sClient .CoreV1 ().Pods (podNamespace ).Get (context .TODO (), podName , metav1.GetOptions {}); err != nil {
261284 klog .ErrorS (err , "failed to get self pod, cannot inherit ownerReference for NodeFeature" )
262285 return err
263286 } else {
@@ -312,6 +335,12 @@ func (w *nfdWorker) Run() error {
312335 httpMux .Handle ("/metrics" , promhttp .HandlerFor (promRegistry , promhttp.HandlerOpts {}))
313336 registerVersion (version .Get ())
314337
338+ klConfig , err := w .kubeletConfigFunc ()
339+ if err != nil {
340+ return err
341+ }
342+ memory .SetSwapMode (klConfig .MemorySwap .SwapBehavior )
343+
315344 err = w .runFeatureDiscovery ()
316345 if err != nil {
317346 return err
@@ -624,7 +653,7 @@ func (m *nfdWorker) updateNodeFeatureObject(labels Labels) error {
624653 return err
625654 }
626655 nodename := utils .NodeName ()
627- namespace := m . kubernetesNamespace
656+ namespace := os . Getenv ( "POD_NAMESPACE" )
628657
629658 features := source .GetAllFeatures ()
630659
@@ -720,3 +749,38 @@ func (c *sourcesConfig) UnmarshalJSON(data []byte) error {
720749
721750 return nil
722751}
752+
753+ func getKubeletConfigFunc (uri , apiAuthTokenFile string ) (func () (* kubeletconfigv1beta1.KubeletConfiguration , error ), error ) {
754+ u , err := url .ParseRequestURI (uri )
755+ if err != nil {
756+ return nil , fmt .Errorf ("failed to parse -kubelet-config-uri: %w" , err )
757+ }
758+
759+ // init kubelet API client
760+ var klConfig * kubeletconfigv1beta1.KubeletConfiguration
761+ switch u .Scheme {
762+ case "file" :
763+ return func () (* kubeletconfigv1beta1.KubeletConfiguration , error ) {
764+ klConfig , err = kubeconf .GetKubeletConfigFromLocalFile (u .Path )
765+ if err != nil {
766+ return nil , fmt .Errorf ("failed to read kubelet config: %w" , err )
767+ }
768+ return klConfig , err
769+ }, nil
770+ case "https" :
771+ restConfig , err := kubeconf .InsecureConfig (u .String (), apiAuthTokenFile )
772+ if err != nil {
773+ return nil , fmt .Errorf ("failed to initialize rest config for kubelet config uri: %w" , err )
774+ }
775+
776+ return func () (* kubeletconfigv1beta1.KubeletConfiguration , error ) {
777+ klConfig , err = kubeconf .GetKubeletConfiguration (restConfig )
778+ if err != nil {
779+ return nil , fmt .Errorf ("failed to get kubelet config from configz endpoint: %w" , err )
780+ }
781+ return klConfig , nil
782+ }, nil
783+ }
784+
785+ return nil , fmt .Errorf ("unsupported URI scheme: %v" , u .Scheme )
786+ }
0 commit comments