@@ -20,6 +20,7 @@ import (
20
20
"encoding/json"
21
21
"fmt"
22
22
"net/http"
23
+ "net/url"
23
24
"os"
24
25
"path/filepath"
25
26
"regexp"
@@ -38,8 +39,10 @@ import (
38
39
"k8s.io/apimachinery/pkg/util/validation"
39
40
k8sclient "k8s.io/client-go/kubernetes"
40
41
"k8s.io/klog/v2"
42
+ kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
41
43
"k8s.io/utils/ptr"
42
44
klogutils "sigs.k8s.io/node-feature-discovery/pkg/utils/klog"
45
+ "sigs.k8s.io/node-feature-discovery/pkg/utils/kubeconf"
43
46
"sigs.k8s.io/yaml"
44
47
45
48
apiequality "k8s.io/apimachinery/pkg/api/equality"
@@ -57,6 +60,7 @@ import (
57
60
_ "sigs.k8s.io/node-feature-discovery/source/kernel"
58
61
_ "sigs.k8s.io/node-feature-discovery/source/local"
59
62
_ "sigs.k8s.io/node-feature-discovery/source/memory"
63
+ memory "sigs.k8s.io/node-feature-discovery/source/memory"
60
64
_ "sigs.k8s.io/node-feature-discovery/source/network"
61
65
_ "sigs.k8s.io/node-feature-discovery/source/pci"
62
66
_ "sigs.k8s.io/node-feature-discovery/source/storage"
@@ -94,13 +98,16 @@ type Labels map[string]string
94
98
95
99
// Args are the command line arguments of NfdWorker.
96
100
type 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
101
+ ConfigFile string
102
+ Klog map [string ]* utils.KlogFlagVal
103
+ Kubeconfig string
104
+ Oneshot bool
105
+ Options string
106
+ Port int
107
+ NoOwnerRefs bool
108
+ KubeletConfigPath string
109
+ KubeletConfigURI string
110
+ APIAuthTokenFile string
104
111
105
112
Overrides ConfigOverrideArgs
106
113
}
@@ -124,6 +131,7 @@ type nfdWorker struct {
124
131
featureSources []source.FeatureSource
125
132
labelSources []source.LabelSource
126
133
ownerReference []metav1.OwnerReference
134
+ kubeletConfigFunc func () (* kubeletconfigv1beta1.KubeletConfiguration , error )
127
135
}
128
136
129
137
// This ticker can represent infinite and normal intervals.
@@ -169,12 +177,25 @@ func NewNfdWorker(opts ...NfdWorkerOption) (NfdWorker, error) {
169
177
stop : make (chan struct {}),
170
178
}
171
179
180
+ if nfd .args .ConfigFile != "" {
181
+ nfd .configFilePath = filepath .Clean (nfd .args .ConfigFile )
182
+ }
183
+
172
184
for _ , o := range opts {
173
185
o .apply (nfd )
174
186
}
175
187
176
- if nfd .args .ConfigFile != "" {
177
- nfd .configFilePath = filepath .Clean (nfd .args .ConfigFile )
188
+ kubeletConfigFunc , err := getKubeletConfigFunc (nfd .args .KubeletConfigURI , nfd .args .APIAuthTokenFile )
189
+ if err != nil {
190
+ return nil , err
191
+ }
192
+
193
+ nfd = & nfdWorker {
194
+ kubeletConfigFunc : kubeletConfigFunc ,
195
+ }
196
+
197
+ for _ , o := range opts {
198
+ o .apply (nfd )
178
199
}
179
200
180
201
// k8sClient might've been set via opts by tests
@@ -239,6 +260,8 @@ func (w *nfdWorker) runFeatureDiscovery() error {
239
260
}
240
261
// Get the set of feature labels.
241
262
labels := createFeatureLabels (w .labelSources , w .config .Core .LabelWhiteList .Regexp )
263
+ // Append a label with app=nfd
264
+ labels ["app" ] = "nfd"
242
265
243
266
// Update the node with the feature labels.
244
267
if ! w .config .Core .NoPublish {
@@ -255,9 +278,10 @@ func (w *nfdWorker) setOwnerReference() error {
255
278
if ! w .config .Core .NoOwnerRefs {
256
279
// Get pod owner reference
257
280
podName := os .Getenv ("POD_NAME" )
281
+ podNamespace := os .Getenv ("POD_NAMESPACE" )
258
282
// Add pod owner reference if it exists
259
283
if podName != "" {
260
- if selfPod , err := w .k8sClient .CoreV1 ().Pods (w . kubernetesNamespace ).Get (context .TODO (), podName , metav1.GetOptions {}); err != nil {
284
+ if selfPod , err := w .k8sClient .CoreV1 ().Pods (podNamespace ).Get (context .TODO (), podName , metav1.GetOptions {}); err != nil {
261
285
klog .ErrorS (err , "failed to get self pod, cannot inherit ownerReference for NodeFeature" )
262
286
return err
263
287
} else {
@@ -312,6 +336,12 @@ func (w *nfdWorker) Run() error {
312
336
httpMux .Handle ("/metrics" , promhttp .HandlerFor (promRegistry , promhttp.HandlerOpts {}))
313
337
registerVersion (version .Get ())
314
338
339
+ klConfig , err := w .kubeletConfigFunc ()
340
+ if err != nil {
341
+ return err
342
+ }
343
+ memory .SetSwapMode (klConfig .MemorySwap .SwapBehavior )
344
+
315
345
err = w .runFeatureDiscovery ()
316
346
if err != nil {
317
347
return err
@@ -624,7 +654,7 @@ func (m *nfdWorker) updateNodeFeatureObject(labels Labels) error {
624
654
return err
625
655
}
626
656
nodename := utils .NodeName ()
627
- namespace := m . kubernetesNamespace
657
+ namespace := os . Getenv ( "POD_NAMESPACE" )
628
658
629
659
features := source .GetAllFeatures ()
630
660
@@ -720,3 +750,38 @@ func (c *sourcesConfig) UnmarshalJSON(data []byte) error {
720
750
721
751
return nil
722
752
}
753
+
754
+ func getKubeletConfigFunc (uri , apiAuthTokenFile string ) (func () (* kubeletconfigv1beta1.KubeletConfiguration , error ), error ) {
755
+ u , err := url .ParseRequestURI (uri )
756
+ if err != nil {
757
+ return nil , fmt .Errorf ("failed to parse -kubelet-config-uri: %w" , err )
758
+ }
759
+
760
+ // init kubelet API client
761
+ var klConfig * kubeletconfigv1beta1.KubeletConfiguration
762
+ switch u .Scheme {
763
+ case "file" :
764
+ return func () (* kubeletconfigv1beta1.KubeletConfiguration , error ) {
765
+ klConfig , err = kubeconf .GetKubeletConfigFromLocalFile (u .Path )
766
+ if err != nil {
767
+ return nil , fmt .Errorf ("failed to read kubelet config: %w" , err )
768
+ }
769
+ return klConfig , err
770
+ }, nil
771
+ case "https" :
772
+ restConfig , err := kubeconf .InsecureConfig (u .String (), apiAuthTokenFile )
773
+ if err != nil {
774
+ return nil , fmt .Errorf ("failed to initialize rest config for kubelet config uri: %w" , err )
775
+ }
776
+
777
+ return func () (* kubeletconfigv1beta1.KubeletConfiguration , error ) {
778
+ klConfig , err = kubeconf .GetKubeletConfiguration (restConfig )
779
+ if err != nil {
780
+ return nil , fmt .Errorf ("failed to get kubelet config from configz endpoint: %w" , err )
781
+ }
782
+ return klConfig , nil
783
+ }, nil
784
+ }
785
+
786
+ return nil , fmt .Errorf ("unsupported URI scheme: %v" , u .Scheme )
787
+ }
0 commit comments