2017-09-06 02:48:10 +00:00
|
|
|
/*
|
2021-03-01 07:02:22 +00:00
|
|
|
Copyright 2017-2021 The Kubernetes Authors.
|
2017-09-06 02:48:10 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package source
|
|
|
|
|
2022-06-14 19:28:07 +00:00
|
|
|
//go:generate mockery --name=LabelSource --inpackage
|
2021-09-21 09:46:41 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-01 16:39:49 +00:00
|
|
|
|
2022-07-04 07:56:35 +00:00
|
|
|
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
|
2021-03-01 07:02:22 +00:00
|
|
|
)
|
2018-06-21 16:02:30 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Source is the base interface for all other source interfaces
|
|
|
|
type Source interface {
|
|
|
|
// Name returns a friendly name for this source
|
|
|
|
Name() string
|
|
|
|
}
|
2018-06-21 16:02:30 +00:00
|
|
|
|
2021-03-01 16:39:49 +00:00
|
|
|
// FeatureSource is an interface for discovering node features
|
|
|
|
type FeatureSource interface {
|
|
|
|
Source
|
|
|
|
|
|
|
|
// Discover does feature discovery
|
|
|
|
Discover() error
|
|
|
|
|
|
|
|
// GetFeatures returns discovered features in raw form
|
2022-07-04 07:56:35 +00:00
|
|
|
GetFeatures() *nfdv1alpha1.DomainFeatures
|
2021-03-01 16:39:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// LabelSource represents a source of node feature labels
|
|
|
|
type LabelSource interface {
|
2021-03-01 07:02:22 +00:00
|
|
|
Source
|
2017-09-06 02:48:10 +00:00
|
|
|
|
2021-03-01 16:39:49 +00:00
|
|
|
// GetLabels returns discovered feature labels
|
|
|
|
GetLabels() (FeatureLabels, error)
|
2020-04-21 19:03:37 +00:00
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// Priority returns the priority of the source
|
|
|
|
Priority() int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigurableSource is an interface for a source that can be configured
|
|
|
|
type ConfigurableSource interface {
|
|
|
|
Source
|
|
|
|
|
2020-04-21 19:03:37 +00:00
|
|
|
// NewConfig returns a new default config of the source
|
|
|
|
NewConfig() Config
|
|
|
|
|
|
|
|
// GetConfig returns the effective configuration of the source
|
|
|
|
GetConfig() Config
|
|
|
|
|
|
|
|
// SetConfig changes the effective configuration of the source
|
|
|
|
SetConfig(Config)
|
|
|
|
}
|
|
|
|
|
2021-11-26 09:22:39 +00:00
|
|
|
// SupplementalSource represents a source that does not belong to the core set
|
|
|
|
// sources to be used in production, e.g. is deprecated, very experimental or
|
|
|
|
// purposed for testing only.
|
|
|
|
type SupplementalSource interface {
|
2021-03-01 07:02:22 +00:00
|
|
|
Source
|
|
|
|
|
2021-11-26 09:22:39 +00:00
|
|
|
// DisableByDefault returns true if the source should be disabled by
|
|
|
|
// default in production.
|
|
|
|
DisableByDefault() bool
|
2021-03-01 07:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FeatureLabelValue represents the value of one feature label
|
|
|
|
type FeatureLabelValue interface{}
|
|
|
|
|
|
|
|
// FeatureLabels is a collection of feature labels
|
|
|
|
type FeatureLabels map[string]FeatureLabelValue
|
|
|
|
|
2021-03-01 05:45:32 +00:00
|
|
|
// Config is the generic interface for source configuration data
|
2020-04-21 19:03:37 +00:00
|
|
|
type Config interface {
|
2017-09-06 02:48:10 +00:00
|
|
|
}
|
2021-03-01 07:02:22 +00:00
|
|
|
|
|
|
|
// sources contain all registered sources
|
|
|
|
var sources = make(map[string]Source)
|
|
|
|
|
2022-01-19 07:12:06 +00:00
|
|
|
// Register registers a source.
|
2021-03-01 07:02:22 +00:00
|
|
|
func Register(s Source) {
|
|
|
|
if name, ok := sources[s.Name()]; ok {
|
|
|
|
panic(fmt.Sprintf("source %q already registered", name))
|
|
|
|
}
|
|
|
|
sources[s.Name()] = s
|
|
|
|
}
|
|
|
|
|
2021-03-01 16:39:49 +00:00
|
|
|
// GetFeatureSource returns a registered FeatureSource interface
|
|
|
|
func GetFeatureSource(name string) FeatureSource {
|
|
|
|
if s, ok := sources[name].(FeatureSource); ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllFeatureSources returns all registered label sources
|
|
|
|
func GetAllFeatureSources() map[string]FeatureSource {
|
|
|
|
all := make(map[string]FeatureSource)
|
|
|
|
for k, v := range sources {
|
|
|
|
if s, ok := v.(FeatureSource); ok {
|
|
|
|
all[k] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return all
|
|
|
|
}
|
|
|
|
|
2021-03-01 07:02:22 +00:00
|
|
|
// GetLabelSource a registered label source
|
|
|
|
func GetLabelSource(name string) LabelSource {
|
|
|
|
if s, ok := sources[name].(LabelSource); ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllLabelSources returns all registered label sources
|
|
|
|
func GetAllLabelSources() map[string]LabelSource {
|
|
|
|
all := make(map[string]LabelSource)
|
|
|
|
for k, v := range sources {
|
|
|
|
if s, ok := v.(LabelSource); ok {
|
|
|
|
all[k] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return all
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfigurableSource a registered configurable source
|
|
|
|
func GetConfigurableSource(name string) ConfigurableSource {
|
|
|
|
if s, ok := sources[name].(ConfigurableSource); ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllConfigurableSources returns all registered configurable sources
|
|
|
|
func GetAllConfigurableSources() map[string]ConfigurableSource {
|
|
|
|
all := make(map[string]ConfigurableSource)
|
|
|
|
for k, v := range sources {
|
|
|
|
if s, ok := v.(ConfigurableSource); ok {
|
|
|
|
all[k] = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return all
|
|
|
|
}
|