Skip to content

Commit 8d25118

Browse files
authored
Clean up config handling in target allocator (#2470)
1 parent a62bac6 commit 8d25118

File tree

5 files changed

+43
-45
lines changed

5 files changed

+43
-45
lines changed

cmd/otel-allocator/config/config.go

+11-19
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ import (
3535
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3636
)
3737

38-
const DefaultResyncTime = 5 * time.Minute
39-
const DefaultConfigFilePath string = "/conf/targetallocator.yaml"
40-
const DefaultCRScrapeInterval model.Duration = model.Duration(time.Second * 30)
38+
const (
39+
DefaultResyncTime = 5 * time.Minute
40+
DefaultConfigFilePath string = "/conf/targetallocator.yaml"
41+
DefaultCRScrapeInterval model.Duration = model.Duration(time.Second * 30)
42+
DefaultAllocationStrategy = "consistent-hashing"
43+
DefaultFilterStrategy = "relabel-config"
44+
)
4145

4246
type Config struct {
4347
ListenAddr string `yaml:"listen_addr,omitempty"`
@@ -46,8 +50,8 @@ type Config struct {
4650
RootLogger logr.Logger `yaml:"-"`
4751
CollectorSelector *metav1.LabelSelector `yaml:"collector_selector,omitempty"`
4852
PromConfig *promconfig.Config `yaml:"config"`
49-
AllocationStrategy *string `yaml:"allocation_strategy,omitempty"`
50-
FilterStrategy *string `yaml:"filter_strategy,omitempty"`
53+
AllocationStrategy string `yaml:"allocation_strategy,omitempty"`
54+
FilterStrategy string `yaml:"filter_strategy,omitempty"`
5155
PrometheusCR PrometheusCRConfig `yaml:"prometheus_cr,omitempty"`
5256
PodMonitorSelector map[string]string `yaml:"pod_monitor_selector,omitempty"`
5357
ServiceMonitorSelector map[string]string `yaml:"service_monitor_selector,omitempty"`
@@ -58,20 +62,6 @@ type PrometheusCRConfig struct {
5862
ScrapeInterval model.Duration `yaml:"scrape_interval,omitempty"`
5963
}
6064

61-
func (c Config) GetAllocationStrategy() string {
62-
if c.AllocationStrategy != nil {
63-
return *c.AllocationStrategy
64-
}
65-
return "consistent-hashing"
66-
}
67-
68-
func (c Config) GetTargetsFilterStrategy() string {
69-
if c.FilterStrategy != nil {
70-
return *c.FilterStrategy
71-
}
72-
return "relabel-config"
73-
}
74-
7565
func LoadFromFile(file string, target *Config) error {
7666
return unmarshal(target, file)
7767
}
@@ -127,6 +117,8 @@ func unmarshal(cfg *Config, configFile string) error {
127117

128118
func CreateDefaultConfig() Config {
129119
return Config{
120+
AllocationStrategy: DefaultAllocationStrategy,
121+
FilterStrategy: DefaultFilterStrategy,
130122
PrometheusCR: PrometheusCRConfig{
131123
ScrapeInterval: DefaultCRScrapeInterval,
132124
},

cmd/otel-allocator/config/config_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ func TestLoad(t *testing.T) {
4545
file: "./testdata/config_test.yaml",
4646
},
4747
want: Config{
48+
AllocationStrategy: DefaultAllocationStrategy,
4849
CollectorSelector: &metav1.LabelSelector{
4950
MatchLabels: map[string]string{
5051
"app.kubernetes.io/instance": "default.test",
5152
"app.kubernetes.io/managed-by": "opentelemetry-operator",
5253
},
5354
},
55+
FilterStrategy: DefaultFilterStrategy,
5456
PrometheusCR: PrometheusCRConfig{
5557
ScrapeInterval: model.Duration(time.Second * 60),
5658
},
@@ -111,12 +113,14 @@ func TestLoad(t *testing.T) {
111113
file: "./testdata/pod_service_selector_test.yaml",
112114
},
113115
want: Config{
116+
AllocationStrategy: DefaultAllocationStrategy,
114117
CollectorSelector: &metav1.LabelSelector{
115118
MatchLabels: map[string]string{
116119
"app.kubernetes.io/instance": "default.test",
117120
"app.kubernetes.io/managed-by": "opentelemetry-operator",
118121
},
119122
},
123+
FilterStrategy: DefaultFilterStrategy,
120124
PrometheusCR: PrometheusCRConfig{
121125
ScrapeInterval: DefaultCRScrapeInterval,
122126
},

cmd/otel-allocator/main.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ func main() {
8080
ctx := context.Background()
8181
log := ctrl.Log.WithName("allocator")
8282

83-
allocatorPrehook = prehook.New(cfg.GetTargetsFilterStrategy(), log)
84-
allocator, err = allocation.New(cfg.GetAllocationStrategy(), log, allocation.WithFilter(allocatorPrehook))
83+
allocatorPrehook = prehook.New(cfg.FilterStrategy, log)
84+
allocator, err = allocation.New(cfg.AllocationStrategy, log, allocation.WithFilter(allocatorPrehook))
8585
if err != nil {
8686
setupLog.Error(err, "Unable to initialize allocation strategy")
8787
os.Exit(1)
@@ -134,11 +134,16 @@ func main() {
134134
runGroup.Add(
135135
func() error {
136136
// Initial loading of the config file's scrape config
137-
err = targetDiscoverer.ApplyConfig(allocatorWatcher.EventSourceConfigMap, cfg.PromConfig)
138-
if err != nil {
139-
setupLog.Error(err, "Unable to apply initial configuration")
140-
return err
137+
if cfg.PromConfig != nil {
138+
err = targetDiscoverer.ApplyConfig(allocatorWatcher.EventSourceConfigMap, cfg.PromConfig.ScrapeConfigs)
139+
if err != nil {
140+
setupLog.Error(err, "Unable to apply initial configuration")
141+
return err
142+
}
143+
} else {
144+
setupLog.Info("Prometheus config empty, skipping initial discovery configuration")
141145
}
146+
142147
err := targetDiscoverer.Watch(allocator.SetTargets)
143148
setupLog.Info("Target discoverer exited")
144149
return err
@@ -180,7 +185,7 @@ func main() {
180185
setupLog.Error(err, "Unable to load configuration")
181186
continue
182187
}
183-
err = targetDiscoverer.ApplyConfig(event.Source, loadConfig)
188+
err = targetDiscoverer.ApplyConfig(event.Source, loadConfig.ScrapeConfigs)
184189
if err != nil {
185190
setupLog.Error(err, "Unable to apply configuration")
186191
continue

cmd/otel-allocator/target/discovery.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/prometheus/client_golang/prometheus"
2323
"github.com/prometheus/client_golang/prometheus/promauto"
2424
"github.com/prometheus/common/model"
25-
"github.com/prometheus/prometheus/config"
25+
promconfig "github.com/prometheus/prometheus/config"
2626
"github.com/prometheus/prometheus/discovery"
2727
"github.com/prometheus/prometheus/model/relabel"
2828
"gopkg.in/yaml.v3"
@@ -41,7 +41,7 @@ type Discoverer struct {
4141
log logr.Logger
4242
manager *discovery.Manager
4343
close chan struct{}
44-
configsMap map[allocatorWatcher.EventSource]*config.Config
44+
configsMap map[allocatorWatcher.EventSource][]*promconfig.ScrapeConfig
4545
hook discoveryHook
4646
scrapeConfigsHash hash.Hash
4747
scrapeConfigsUpdater scrapeConfigsUpdater
@@ -52,33 +52,30 @@ type discoveryHook interface {
5252
}
5353

5454
type scrapeConfigsUpdater interface {
55-
UpdateScrapeConfigResponse(map[string]*config.ScrapeConfig) error
55+
UpdateScrapeConfigResponse(map[string]*promconfig.ScrapeConfig) error
5656
}
5757

5858
func NewDiscoverer(log logr.Logger, manager *discovery.Manager, hook discoveryHook, scrapeConfigsUpdater scrapeConfigsUpdater) *Discoverer {
5959
return &Discoverer{
6060
log: log,
6161
manager: manager,
6262
close: make(chan struct{}),
63-
configsMap: make(map[allocatorWatcher.EventSource]*config.Config),
63+
configsMap: make(map[allocatorWatcher.EventSource][]*promconfig.ScrapeConfig),
6464
hook: hook,
65+
scrapeConfigsHash: nil, // we want the first update to succeed even if the config is empty
6566
scrapeConfigsUpdater: scrapeConfigsUpdater,
6667
}
6768
}
6869

69-
func (m *Discoverer) ApplyConfig(source allocatorWatcher.EventSource, cfg *config.Config) error {
70-
if cfg == nil {
71-
m.log.Info("Service Discovery got empty Prometheus config", "source", source.String())
72-
return nil
73-
}
74-
m.configsMap[source] = cfg
75-
jobToScrapeConfig := make(map[string]*config.ScrapeConfig)
70+
func (m *Discoverer) ApplyConfig(source allocatorWatcher.EventSource, scrapeConfigs []*promconfig.ScrapeConfig) error {
71+
m.configsMap[source] = scrapeConfigs
72+
jobToScrapeConfig := make(map[string]*promconfig.ScrapeConfig)
7673

7774
discoveryCfg := make(map[string]discovery.Configs)
7875
relabelCfg := make(map[string][]*relabel.Config)
7976

80-
for _, value := range m.configsMap {
81-
for _, scrapeConfig := range value.ScrapeConfigs {
77+
for _, configs := range m.configsMap {
78+
for _, scrapeConfig := range configs {
8279
jobToScrapeConfig[scrapeConfig.JobName] = scrapeConfig
8380
discoveryCfg[scrapeConfig.JobName] = scrapeConfig.ServiceDiscoveryConfigs
8481
relabelCfg[scrapeConfig.JobName] = scrapeConfig.RelabelConfigs
@@ -137,7 +134,7 @@ func (m *Discoverer) Close() {
137134

138135
// Calculate a hash for a scrape config map.
139136
// This is done by marshaling to YAML because it's the most straightforward and doesn't run into problems with unexported fields.
140-
func getScrapeConfigHash(jobToScrapeConfig map[string]*config.ScrapeConfig) (hash.Hash64, error) {
137+
func getScrapeConfigHash(jobToScrapeConfig map[string]*promconfig.ScrapeConfig) (hash.Hash64, error) {
141138
var err error
142139
hash := fnv.New64()
143140
yamlEncoder := yaml.NewEncoder(hash)

cmd/otel-allocator/target/discovery_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestDiscovery(t *testing.T) {
8989
err := config.LoadFromFile(tt.args.file, &cfg)
9090
assert.NoError(t, err)
9191
assert.True(t, len(cfg.PromConfig.ScrapeConfigs) > 0)
92-
err = manager.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, cfg.PromConfig)
92+
err = manager.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, cfg.PromConfig.ScrapeConfigs)
9393
assert.NoError(t, err)
9494

9595
gotTargets := <-results
@@ -306,11 +306,11 @@ func TestDiscovery_ScrapeConfigHashing(t *testing.T) {
306306

307307
for _, tc := range tests {
308308
t.Run(tc.description, func(t *testing.T) {
309-
err := manager.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, tc.cfg)
309+
err := manager.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, tc.cfg.ScrapeConfigs)
310310
if !tc.expectErr {
311311
expectedConfig = make(map[string]*promconfig.ScrapeConfig)
312-
for _, value := range manager.configsMap {
313-
for _, scrapeConfig := range value.ScrapeConfigs {
312+
for _, configs := range manager.configsMap {
313+
for _, scrapeConfig := range configs {
314314
expectedConfig[scrapeConfig.JobName] = scrapeConfig
315315
}
316316
}
@@ -389,7 +389,7 @@ func BenchmarkApplyScrapeConfig(b *testing.B) {
389389

390390
b.ResetTimer()
391391
for i := 0; i < b.N; i++ {
392-
err := manager.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, cfg)
392+
err := manager.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, cfg.ScrapeConfigs)
393393
require.NoError(b, err)
394394
}
395395
}

0 commit comments

Comments
 (0)