17
17
package lm
18
18
19
19
import (
20
+ "bufio"
20
21
"errors"
21
22
"fmt"
23
+ "math/rand"
24
+ "os"
25
+ "sort"
22
26
"strconv"
23
27
"strings"
24
28
@@ -28,6 +32,7 @@ import (
28
32
29
33
spec "github.com/NVIDIA/k8s-device-plugin/api/config/v1"
30
34
"github.com/NVIDIA/k8s-device-plugin/internal/resource"
35
+ "github.com/google/uuid"
31
36
)
32
37
33
38
var errMPSSharingNotSupported = errors .New ("MPS sharing is not supported" )
@@ -80,13 +85,22 @@ func NewDeviceLabeler(manager resource.Manager, config *spec.Config) (Labeler, e
80
85
return nil , fmt .Errorf ("error creating resource labeler: %v" , err )
81
86
}
82
87
88
+ var imexLabeler Labeler
89
+ if * config .Flags .GFD .ImexNodesConfig != "" {
90
+ imexLabeler , err = newImexDomainLabeler (* config .Flags .GFD .ImexNodesConfig )
91
+ if err != nil {
92
+ return nil , fmt .Errorf ("error creating imex domain labeler: %v" , err )
93
+ }
94
+ }
95
+
83
96
l := Merge (
84
97
machineTypeLabeler ,
85
98
versionLabeler ,
86
99
migCapabilityLabeler ,
87
100
sharingLabeler ,
88
101
resourceLabeler ,
89
102
gpuModeLabeler ,
103
+ imexLabeler ,
90
104
)
91
105
92
106
return l , nil
@@ -218,6 +232,41 @@ func newGPUModeLabeler(devices []resource.Device) (Labeler, error) {
218
232
return labels , nil
219
233
}
220
234
235
+ func newImexDomainLabeler (configFile string ) (Labeler , error ) {
236
+ // read file and parse it
237
+ imexConfig , err := os .Open (configFile )
238
+ if err != nil {
239
+ return nil , fmt .Errorf ("failed to read imex config file: %v" , err )
240
+ }
241
+ defer imexConfig .Close ()
242
+
243
+ // Read the file line by line
244
+ var ips []string
245
+ scanner := bufio .NewScanner (imexConfig )
246
+ for scanner .Scan () {
247
+ line := scanner .Text ()
248
+ ips = append (ips , line )
249
+ }
250
+
251
+ if err := scanner .Err (); err != nil {
252
+ return nil , fmt .Errorf ("failed to read imex config file: %v" , err )
253
+ }
254
+
255
+ // Sort the IP addresses
256
+ sort .Strings (ips )
257
+
258
+ // Join the sorted IPs into a single string
259
+ sortedIPs := strings .Join (ips , "\n " )
260
+
261
+ hashedconfig := generateUUIDs (sortedIPs , 1 )[0 ]
262
+
263
+ labels := Labels {
264
+ "nvidia.com/gpu.imex-domain" : hashedconfig ,
265
+ }
266
+
267
+ return labels , nil
268
+ }
269
+
221
270
func getModeForClasses (classes []uint32 ) string {
222
271
if len (classes ) == 0 {
223
272
return "unknown"
@@ -254,3 +303,25 @@ func getDeviceClasses(devices []resource.Device) ([]uint32, error) {
254
303
}
255
304
return classes , nil
256
305
}
306
+
307
+ func generateUUIDs (seed string , count int ) []string {
308
+ rand := rand .New (rand .NewSource (hash (seed )))
309
+
310
+ uuids := make ([]string , count )
311
+ for i := 0 ; i < count ; i ++ {
312
+ charset := make ([]byte , 16 )
313
+ rand .Read (charset )
314
+ uuid , _ := uuid .FromBytes (charset )
315
+ uuids [i ] = uuid .String ()
316
+ }
317
+
318
+ return uuids
319
+ }
320
+
321
+ func hash (s string ) int64 {
322
+ h := int64 (0 )
323
+ for _ , c := range s {
324
+ h = 31 * h + int64 (c )
325
+ }
326
+ return h
327
+ }
0 commit comments