|
| 1 | +/** |
| 2 | +# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package lm |
| 18 | + |
| 19 | +import ( |
| 20 | + "bufio" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "math/rand" // nolint:gosec |
| 24 | + "net" |
| 25 | + "os" |
| 26 | + "sort" |
| 27 | + "strings" |
| 28 | + |
| 29 | + spec "github.com/NVIDIA/k8s-device-plugin/api/config/v1" |
| 30 | + "github.com/NVIDIA/k8s-device-plugin/internal/resource" |
| 31 | + |
| 32 | + "github.com/google/uuid" |
| 33 | + "k8s.io/klog/v2" |
| 34 | +) |
| 35 | + |
| 36 | +func newImexLabeler(config *spec.Config, devices []resource.Device) (Labeler, error) { |
| 37 | + if config.Flags.GFD.ImexNodesConfigFile == nil { |
| 38 | + // No imex config file, return empty labels |
| 39 | + return empty{}, nil |
| 40 | + } |
| 41 | + |
| 42 | + imexConfigFile, err := os.Open(*config.Flags.GFD.ImexNodesConfigFile) |
| 43 | + if os.IsNotExist(err) { |
| 44 | + // No imex config file, return empty labels |
| 45 | + return empty{}, nil |
| 46 | + } else if err != nil { |
| 47 | + return nil, fmt.Errorf("failed to open imex config file: %v", err) |
| 48 | + } |
| 49 | + defer imexConfigFile.Close() |
| 50 | + |
| 51 | + clusterUUID, cliqueID, err := getFabricIDs(devices) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + if clusterUUID == "" || cliqueID == "" { |
| 56 | + return empty{}, nil |
| 57 | + } |
| 58 | + |
| 59 | + imexDomainID, err := getImexDomainID(imexConfigFile) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + labels := Labels{ |
| 65 | + "nvidia.com/gpu.clique": strings.Join([]string{clusterUUID, cliqueID}, "."), |
| 66 | + "nvidia.com/gpu.imex-domain": strings.Join([]string{imexDomainID, cliqueID}, "."), |
| 67 | + } |
| 68 | + |
| 69 | + return labels, nil |
| 70 | +} |
| 71 | + |
| 72 | +func getFabricIDs(devices []resource.Device) (string, string, error) { |
| 73 | + uniqueClusterUUIDs := make(map[string][]int) |
| 74 | + uniqueCliqueIDs := make(map[string][]int) |
| 75 | + for i, device := range devices { |
| 76 | + isFabricAttached, err := device.IsFabricAttached() |
| 77 | + if err != nil { |
| 78 | + return "", "", fmt.Errorf("error checking imex capability: %v", err) |
| 79 | + } |
| 80 | + if !isFabricAttached { |
| 81 | + continue |
| 82 | + } |
| 83 | + |
| 84 | + clusterUUID, cliqueID, err := device.GetFabricIDs() |
| 85 | + if err != nil { |
| 86 | + |
| 87 | + return "", "", fmt.Errorf("error getting fabric IDs: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + uniqueClusterUUIDs[clusterUUID] = append(uniqueClusterUUIDs[clusterUUID], i) |
| 91 | + uniqueCliqueIDs[cliqueID] = append(uniqueCliqueIDs[cliqueID], i) |
| 92 | + } |
| 93 | + |
| 94 | + if len(uniqueClusterUUIDs) > 1 { |
| 95 | + klog.Warningf("Cluster UUIDs are non-unique: %v", uniqueClusterUUIDs) |
| 96 | + return "", "", nil |
| 97 | + } |
| 98 | + |
| 99 | + if len(uniqueCliqueIDs) > 1 { |
| 100 | + klog.Warningf("Clique IDs are non-unique: %v", uniqueCliqueIDs) |
| 101 | + return "", "", nil |
| 102 | + } |
| 103 | + |
| 104 | + for clusterUUID := range uniqueClusterUUIDs { |
| 105 | + for cliqueID := range uniqueCliqueIDs { |
| 106 | + return clusterUUID, cliqueID, nil |
| 107 | + } |
| 108 | + } |
| 109 | + return "", "", nil |
| 110 | +} |
| 111 | + |
| 112 | +// getImexDomainID reads the imex config file and returns a unique identifier |
| 113 | +// based on the sorted list of IP addresses in the file. |
| 114 | +func getImexDomainID(r io.Reader) (string, error) { |
| 115 | + // Read the file line by line |
| 116 | + var ips []string |
| 117 | + scanner := bufio.NewScanner(r) |
| 118 | + for scanner.Scan() { |
| 119 | + ip := strings.TrimSpace(scanner.Text()) |
| 120 | + if net.ParseIP(ip) == nil { |
| 121 | + return "", fmt.Errorf("invalid IP address in imex config file: %s", ip) |
| 122 | + } |
| 123 | + ips = append(ips, ip) |
| 124 | + } |
| 125 | + |
| 126 | + if err := scanner.Err(); err != nil { |
| 127 | + return "", fmt.Errorf("failed to read imex config file: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + if len(ips) == 0 { |
| 131 | + // No IPs in the file, return empty labels |
| 132 | + return "", nil |
| 133 | + } |
| 134 | + |
| 135 | + sort.Strings(ips) |
| 136 | + |
| 137 | + return generateContentUUID(strings.Join(ips, "\n")), nil |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +func generateContentUUID(seed string) string { |
| 142 | + // nolint:gosec |
| 143 | + rand := rand.New(rand.NewSource(hash(seed))) |
| 144 | + charset := make([]byte, 16) |
| 145 | + rand.Read(charset) |
| 146 | + uuid, _ := uuid.FromBytes(charset) |
| 147 | + return uuid.String() |
| 148 | +} |
| 149 | + |
| 150 | +func hash(s string) int64 { |
| 151 | + h := int64(0) |
| 152 | + for _, c := range s { |
| 153 | + h = 31*h + int64(c) |
| 154 | + } |
| 155 | + return h |
| 156 | +} |
0 commit comments