Skip to content

Commit b56983d

Browse files
authored
[chore] simplify allocation Option and registry (#3645)
1 parent 459968a commit b56983d

File tree

3 files changed

+16
-46
lines changed

3 files changed

+16
-46
lines changed

cmd/otel-allocator/allocation/allocator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434

3535
var _ Allocator = &allocator{}
3636

37-
func newAllocator(log logr.Logger, strategy Strategy, opts ...AllocationOption) Allocator {
37+
func newAllocator(log logr.Logger, strategy Strategy, opts ...Option) Allocator {
3838
chAllocator := &allocator{
3939
strategy: strategy,
4040
collectors: make(map[string]*Collector),

cmd/otel-allocator/allocation/strategy.go

+14-38
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package allocation
1616

1717
import (
18-
"errors"
1918
"fmt"
2019

2120
"github.com/buraksezer/consistent"
@@ -26,12 +25,14 @@ import (
2625
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/target"
2726
)
2827

29-
type AllocatorProvider func(log logr.Logger, opts ...AllocationOption) Allocator
28+
type AllocatorProvider func(log logr.Logger, opts ...Option) Allocator
3029

3130
var (
32-
strategies = map[string]Strategy{}
33-
34-
registry = map[string]AllocatorProvider{}
31+
strategies = map[string]Strategy{
32+
leastWeightedStrategyName: newleastWeightedStrategy(),
33+
consistentHashingStrategyName: newConsistentHashingStrategy(),
34+
perNodeStrategyName: newPerNodeStrategy(),
35+
}
3536

3637
// TargetsPerCollector records how many targets have been assigned to each collector.
3738
// It is currently the responsibility of the strategy to track this information.
@@ -57,19 +58,19 @@ var (
5758
})
5859
)
5960

60-
type AllocationOption func(Allocator)
61+
type Option func(Allocator)
6162

6263
type Filter interface {
6364
Apply(map[string]*target.Item) map[string]*target.Item
6465
}
6566

66-
func WithFilter(filter Filter) AllocationOption {
67+
func WithFilter(filter Filter) Option {
6768
return func(allocator Allocator) {
6869
allocator.SetFilter(filter)
6970
}
7071
}
7172

72-
func WithFallbackStrategy(fallbackStrategy string) AllocationOption {
73+
func WithFallbackStrategy(fallbackStrategy string) Option {
7374
var strategy, ok = strategies[fallbackStrategy]
7475
if fallbackStrategy != "" && !ok {
7576
panic(fmt.Errorf("unregistered strategy used as fallback: %s", fallbackStrategy))
@@ -83,24 +84,16 @@ func RecordTargetsKept(targets map[string]*target.Item) {
8384
TargetsRemaining.Set(float64(len(targets)))
8485
}
8586

86-
func New(name string, log logr.Logger, opts ...AllocationOption) (Allocator, error) {
87-
if p, ok := registry[name]; ok {
88-
return p(log.WithValues("allocator", name), opts...), nil
87+
func New(name string, log logr.Logger, opts ...Option) (Allocator, error) {
88+
if strategy, ok := strategies[name]; ok {
89+
return newAllocator(log.WithValues("allocator", name), strategy, opts...), nil
8990
}
9091
return nil, fmt.Errorf("unregistered strategy: %s", name)
9192
}
9293

93-
func Register(name string, provider AllocatorProvider) error {
94-
if _, ok := registry[name]; ok {
95-
return errors.New("already registered")
96-
}
97-
registry[name] = provider
98-
return nil
99-
}
100-
10194
func GetRegisteredAllocatorNames() []string {
10295
var names []string
103-
for s := range registry {
96+
for s := range strategies {
10497
names = append(names, s)
10598
}
10699
return names
@@ -123,7 +116,7 @@ type Strategy interface {
123116
// SetCollectors call. Strategies which don't need this information can just ignore it.
124117
SetCollectors(map[string]*Collector)
125118
GetName() string
126-
// Add fallback strategy for strategies whose main allocation method can sometimes leave targets unassigned
119+
// SetFallbackStrategy adds fallback strategy for strategies whose main allocation method can sometimes leave targets unassigned
127120
SetFallbackStrategy(Strategy)
128121
}
129122

@@ -149,20 +142,3 @@ func (c Collector) String() string {
149142
func NewCollector(name, node string) *Collector {
150143
return &Collector{Name: name, NodeName: node}
151144
}
152-
153-
func init() {
154-
strategies = map[string]Strategy{
155-
leastWeightedStrategyName: newleastWeightedStrategy(),
156-
consistentHashingStrategyName: newConsistentHashingStrategy(),
157-
perNodeStrategyName: newPerNodeStrategy(),
158-
}
159-
160-
for strategyName, strategy := range strategies {
161-
err := Register(strategyName, func(log logr.Logger, opts ...AllocationOption) Allocator {
162-
return newAllocator(log, strategy, opts...)
163-
})
164-
if err != nil {
165-
panic(err)
166-
}
167-
}
168-
}

cmd/otel-allocator/main.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ func main() {
8181
log := ctrl.Log.WithName("allocator")
8282

8383
allocatorPrehook = prehook.New(cfg.FilterStrategy, log)
84-
85-
var allocationOptions []allocation.AllocationOption
86-
allocationOptions = append(allocationOptions, allocation.WithFilter(allocatorPrehook))
87-
if cfg.AllocationFallbackStrategy != "" {
88-
allocationOptions = append(allocationOptions, allocation.WithFallbackStrategy(cfg.AllocationFallbackStrategy))
89-
}
90-
allocator, err = allocation.New(cfg.AllocationStrategy, log, allocationOptions...)
84+
allocator, err = allocation.New(cfg.AllocationStrategy, log, allocation.WithFilter(allocatorPrehook), allocation.WithFallbackStrategy(cfg.AllocationFallbackStrategy))
9185
if err != nil {
9286
setupLog.Error(err, "Unable to initialize allocation strategy")
9387
os.Exit(1)

0 commit comments

Comments
 (0)