|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 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 export |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "maps" |
| 23 | + "regexp" |
| 24 | + "slices" |
| 25 | + "sort" |
| 26 | + |
| 27 | + "github.com/spf13/cobra" |
| 28 | + "k8s.io/klog/v2" |
| 29 | + |
| 30 | + worker "sigs.k8s.io/node-feature-discovery/pkg/nfd-worker" |
| 31 | + "sigs.k8s.io/node-feature-discovery/source" |
| 32 | +) |
| 33 | + |
| 34 | +func NewLabelsCmd() *cobra.Command { |
| 35 | + cmd := &cobra.Command{ |
| 36 | + Use: "labels", |
| 37 | + Short: "Export feature labels for given node", |
| 38 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 39 | + |
| 40 | + // Determine enabled feature sources |
| 41 | + featureSources := make(map[string]source.FeatureSource) |
| 42 | + for n, s := range source.GetAllFeatureSources() { |
| 43 | + if ts, ok := s.(source.SupplementalSource); !ok || !ts.DisableByDefault() { |
| 44 | + s.Discover() |
| 45 | + featureSources[n] = s |
| 46 | + } |
| 47 | + } |
| 48 | + featureSourceList := slices.Collect(maps.Values(featureSources)) |
| 49 | + sort.Slice(featureSourceList, func(i, j int) bool { return featureSourceList[i].Name() < featureSourceList[j].Name() }) |
| 50 | + |
| 51 | + // Determine enabled label sources |
| 52 | + labelSources := make(map[string]source.LabelSource) |
| 53 | + for n, s := range source.GetAllLabelSources() { |
| 54 | + if ts, ok := s.(source.SupplementalSource); !ok || !ts.DisableByDefault() { |
| 55 | + labelSources[n] = s |
| 56 | + } |
| 57 | + } |
| 58 | + labelSourcesList := slices.Collect(maps.Values(labelSources)) |
| 59 | + sort.Slice(labelSourcesList, func(i, j int) bool { |
| 60 | + iP, jP := labelSourcesList[i].Priority(), labelSourcesList[j].Priority() |
| 61 | + if iP != jP { |
| 62 | + return iP < jP |
| 63 | + } |
| 64 | + return labelSourcesList[i].Name() < labelSourcesList[j].Name() |
| 65 | + }) |
| 66 | + |
| 67 | + labels := worker.Labels{} |
| 68 | + labelWhiteList := *regexp.MustCompile("") |
| 69 | + |
| 70 | + // Get labels from all enabled label sources |
| 71 | + for _, source := range labelSourcesList { |
| 72 | + labelsFromSource, err := worker.GetFeatureLabels(source, labelWhiteList) |
| 73 | + if err != nil { |
| 74 | + klog.ErrorS(err, "discovery failed", "source", source.Name()) |
| 75 | + continue |
| 76 | + } |
| 77 | + maps.Copy(labels, labelsFromSource) |
| 78 | + } |
| 79 | + |
| 80 | + exportedLabels, err := json.MarshalIndent(labels, "", " ") |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + if outputPath != "" { |
| 86 | + err = writeToFile(outputPath, string(exportedLabels)) |
| 87 | + } else { |
| 88 | + fmt.Println(string(exportedLabels)) |
| 89 | + } |
| 90 | + return err |
| 91 | + }, |
| 92 | + } |
| 93 | + cmd.Flags().StringVar(&outputPath, "path", "", "export to this JSON path") |
| 94 | + return cmd |
| 95 | +} |
| 96 | + |
| 97 | +func init() { |
| 98 | + ExportCmd.AddCommand(NewLabelsCmd()) |
| 99 | +} |
0 commit comments