2
0

source.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package external
  2. import "fmt"
  3. type LabelSource interface {
  4. ExtractNodeLabels(map[string]string) (map[string]string, error)
  5. }
  6. func NewLabelSource(cfg *Config) (LabelSource, error) {
  7. if cfg == nil {
  8. return nil, fmt.Errorf("nil config")
  9. }
  10. if !cfg.HasNodeLabelConfig() {
  11. return nil, fmt.Errorf("no supported external label config")
  12. }
  13. nlConfig := cfg.NodeLabelConfig()
  14. if nlConfig.ConfigMapName() != "" {
  15. return &ConfigMapSource{
  16. cfg: cfg,
  17. }, nil
  18. }
  19. return nil, fmt.Errorf("no label source configured")
  20. }
  21. // WatchFunc bridges a LabelSource and a LabelProvider as a watcher callback.
  22. // It returns a func(string, map[string]string) error that passes the raw source
  23. // data through src.ExtractNodeLabels and forwards the resulting labels to provider.Update.
  24. func WatchFunc(src LabelSource, provider LabelProvider) func(string, map[string]string) error {
  25. if src == nil {
  26. return func(string, map[string]string) error {
  27. return fmt.Errorf("nil LabelSource")
  28. }
  29. }
  30. if provider == nil {
  31. return func(string, map[string]string) error {
  32. return fmt.Errorf("nil LabelProvider")
  33. }
  34. }
  35. return func(name string, data map[string]string) error {
  36. labels, err := src.ExtractNodeLabels(data)
  37. if err != nil {
  38. return err
  39. }
  40. return provider.Update(name, labels)
  41. }
  42. }