|
| 1 | +package nfd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + source "sigs.k8s.io/node-feature-discovery/source" |
| 7 | + |
| 8 | + // Note that "fake" is removed from here |
| 9 | + _ "sigs.k8s.io/node-feature-discovery/source/cpu" |
| 10 | + _ "sigs.k8s.io/node-feature-discovery/source/custom" |
| 11 | + _ "sigs.k8s.io/node-feature-discovery/source/kernel" |
| 12 | + _ "sigs.k8s.io/node-feature-discovery/source/local" |
| 13 | + _ "sigs.k8s.io/node-feature-discovery/source/memory" |
| 14 | + _ "sigs.k8s.io/node-feature-discovery/source/network" |
| 15 | + _ "sigs.k8s.io/node-feature-discovery/source/pci" |
| 16 | + _ "sigs.k8s.io/node-feature-discovery/source/storage" |
| 17 | + _ "sigs.k8s.io/node-feature-discovery/source/system" |
| 18 | + _ "sigs.k8s.io/node-feature-discovery/source/usb" |
| 19 | + |
| 20 | + "github.com/supercontainers/compspec-go/pkg/extractor" |
| 21 | + "github.com/supercontainers/compspec-go/pkg/utils" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + ExtractorName = "nfd" |
| 26 | + ExtractorDescription = "node feature discovery" |
| 27 | + |
| 28 | + // Each of these corresponds to a source |
| 29 | + CPUSection = "cpu" |
| 30 | + |
| 31 | + // TODO can we do a check that this is desired / enabled before running? |
| 32 | + CustomSection = "custom" |
| 33 | + KernelSection = "kernel" |
| 34 | + LocalSection = "local" |
| 35 | + MemorySection = "memory" |
| 36 | + NetworkSection = "network" |
| 37 | + PCISection = "pci" |
| 38 | + StorageSection = "storage" |
| 39 | + SystemSection = "system" |
| 40 | + USBSection = "usb" |
| 41 | +) |
| 42 | + |
| 43 | +var ( |
| 44 | + validSections = []string{ |
| 45 | + CPUSection, |
| 46 | + CustomSection, |
| 47 | + KernelSection, |
| 48 | + LocalSection, |
| 49 | + MemorySection, |
| 50 | + NetworkSection, |
| 51 | + PCISection, |
| 52 | + StorageSection, |
| 53 | + SystemSection, |
| 54 | + USBSection, |
| 55 | + } |
| 56 | +) |
| 57 | + |
| 58 | +// NFDExtractor is an extractor for node feature discovery |
| 59 | +type NFDExtractor struct { |
| 60 | + sections []string |
| 61 | +} |
| 62 | + |
| 63 | +func (e NFDExtractor) Name() string { |
| 64 | + return ExtractorName |
| 65 | +} |
| 66 | + |
| 67 | +func (e NFDExtractor) Sections() []string { |
| 68 | + return e.sections |
| 69 | +} |
| 70 | + |
| 71 | +func (e NFDExtractor) Description() string { |
| 72 | + return ExtractorDescription |
| 73 | +} |
| 74 | + |
| 75 | +// Validate ensures that the sections provided are in the list we know |
| 76 | +func (e NFDExtractor) Validate() bool { |
| 77 | + invalids, valid := utils.StringArrayIsSubset(e.sections, validSections) |
| 78 | + for _, invalid := range invalids { |
| 79 | + fmt.Printf("Sections %s is not known for extractor plugin %s\n", invalid, e.Name()) |
| 80 | + } |
| 81 | + return valid |
| 82 | +} |
| 83 | + |
| 84 | +// Extract returns system metadata, for a set of named sections |
| 85 | +func (e NFDExtractor) Extract(interface{}) (extractor.ExtractorData, error) { |
| 86 | + |
| 87 | + sections := map[string]extractor.ExtractorSection{} |
| 88 | + data := extractor.ExtractorData{} |
| 89 | + |
| 90 | + // Get all registered feature sources |
| 91 | + sources := source.GetAllFeatureSources() |
| 92 | + |
| 93 | + // Only extract the sections we asked for |
| 94 | + for _, name := range e.sections { |
| 95 | + discovery, ok := sources[name] |
| 96 | + |
| 97 | + // This should not happen |
| 98 | + if !ok { |
| 99 | + fmt.Printf("%s is not a known feature source %s\n", name) |
| 100 | + continue |
| 101 | + } |
| 102 | + err := discovery.Discover() |
| 103 | + if err != nil { |
| 104 | + fmt.Printf("Issue discovering features for %s\n", discovery.Name()) |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + // Create a new section for the <name> group |
| 109 | + // For each of the below, "fs" is a feature set |
| 110 | + // AttributeFeatureSet |
| 111 | + section := extractor.ExtractorSection{} |
| 112 | + features := discovery.GetFeatures() |
| 113 | + for k, fs := range features.Attributes { |
| 114 | + for fName, feature := range fs.Elements { |
| 115 | + uid := fmt.Sprintf("%s.%s", k, fName) |
| 116 | + section[uid] = feature |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // FlagFeatureSet |
| 121 | + // Note that the second value to feature is v1alpha.Nil |
| 122 | + // I think this acts as a flag, double check |
| 123 | + for k, fs := range features.Flags { |
| 124 | + for feature, _ := range fs.Elements { |
| 125 | + uid := fmt.Sprintf("%s.%s", k, feature) |
| 126 | + section[uid] = "true" |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // InstanceFeatureSet |
| 131 | + for k, fs := range features.Instances { |
| 132 | + for idx, feature := range fs.Elements { |
| 133 | + for fName, attr := range feature.Attributes { |
| 134 | + uid := fmt.Sprintf("%s.%d.%s", k, idx, fName) |
| 135 | + section[uid] = attr |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + sections[name] = section |
| 140 | + } |
| 141 | + data.Sections = sections |
| 142 | + return data, nil |
| 143 | +} |
| 144 | + |
| 145 | +// NewPlugin validates and returns a new kernel plugin |
| 146 | +func NewPlugin(sections []string) (extractor.Extractor, error) { |
| 147 | + if len(sections) == 0 { |
| 148 | + sections = validSections |
| 149 | + } |
| 150 | + e := NFDExtractor{sections: sections} |
| 151 | + if !e.Validate() { |
| 152 | + return nil, fmt.Errorf("plugin %s is not valid\n", e.Name()) |
| 153 | + } |
| 154 | + return e, nil |
| 155 | +} |
0 commit comments