|
| 1 | +/* |
| 2 | +Copyright 2024 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 | + "os" |
| 23 | + |
| 24 | + "github.com/spf13/cobra" |
| 25 | + |
| 26 | + "sigs.k8s.io/node-feature-discovery/source" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + outputPath string |
| 31 | +) |
| 32 | + |
| 33 | +func NewExportCmd() *cobra.Command { |
| 34 | + cmd := &cobra.Command{ |
| 35 | + Use: "features", |
| 36 | + Short: "Export features for given node", |
| 37 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 38 | + sources := map[string]source.FeatureSource{} |
| 39 | + for k, v := range source.GetAllFeatureSources() { |
| 40 | + if ts, ok := v.(source.SupplementalSource); ok && ts.DisableByDefault() { |
| 41 | + continue |
| 42 | + } |
| 43 | + sources[k] = v |
| 44 | + } |
| 45 | + |
| 46 | + // Discover all feature sources |
| 47 | + for _, s := range sources { |
| 48 | + if err := s.Discover(); err != nil { |
| 49 | + fmt.Errorf("error during discovery of source %s: %w", s.Name(), err) |
| 50 | + return err |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Make into flat format. |
| 55 | + features := source.GetAllFeatures() |
| 56 | + featureListing := map[string]string{} |
| 57 | + |
| 58 | + // A flag's presence == true |
| 59 | + for flagName := range features.Flags { |
| 60 | + featureListing[flagName] = "true" |
| 61 | + } |
| 62 | + |
| 63 | + // Attributes are sets of elements (key value pairs) |
| 64 | + for featureName, featureSet := range features.Attributes { |
| 65 | + for elementName, elementValue := range featureSet.Elements { |
| 66 | + featureListing[fmt.Sprintf("%s.%s", featureName, elementName)] = elementValue |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + for featureName, featureSet := range features.Instances { |
| 71 | + for _, element := range featureSet.Elements { |
| 72 | + for attrName, attrValue := range element.Attributes { |
| 73 | + featureListing[fmt.Sprintf("%s.%s", featureName, attrName)] = attrValue |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + exportedLabels, err := json.MarshalIndent(featureListing, "", " ") |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + if outputPath != "" { |
| 83 | + fd, err := os.Create(outputPath) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + defer fd.Close() |
| 88 | + _, err = fmt.Fprint(fd, string(exportedLabels)) |
| 89 | + return err |
| 90 | + } else { |
| 91 | + fmt.Println(string(exportedLabels)) |
| 92 | + } |
| 93 | + return nil |
| 94 | + }, |
| 95 | + } |
| 96 | + cmd.Flags().StringVar(&outputPath, "path", "", "export to this JSON path") |
| 97 | + return cmd |
| 98 | +} |
| 99 | + |
| 100 | +func init() { |
| 101 | + ExportCmd.AddCommand(NewExportCmd()) |
| 102 | +} |
0 commit comments