15
15
package allocation
16
16
17
17
import (
18
- "errors"
19
18
"fmt"
20
19
21
20
"github.com/buraksezer/consistent"
@@ -26,12 +25,14 @@ import (
26
25
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/target"
27
26
)
28
27
29
- type AllocatorProvider func (log logr.Logger , opts ... AllocationOption ) Allocator
28
+ type AllocatorProvider func (log logr.Logger , opts ... Option ) Allocator
30
29
31
30
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
+ }
35
36
36
37
// TargetsPerCollector records how many targets have been assigned to each collector.
37
38
// It is currently the responsibility of the strategy to track this information.
@@ -57,19 +58,19 @@ var (
57
58
})
58
59
)
59
60
60
- type AllocationOption func (Allocator )
61
+ type Option func (Allocator )
61
62
62
63
type Filter interface {
63
64
Apply (map [string ]* target.Item ) map [string ]* target.Item
64
65
}
65
66
66
- func WithFilter (filter Filter ) AllocationOption {
67
+ func WithFilter (filter Filter ) Option {
67
68
return func (allocator Allocator ) {
68
69
allocator .SetFilter (filter )
69
70
}
70
71
}
71
72
72
- func WithFallbackStrategy (fallbackStrategy string ) AllocationOption {
73
+ func WithFallbackStrategy (fallbackStrategy string ) Option {
73
74
var strategy , ok = strategies [fallbackStrategy ]
74
75
if fallbackStrategy != "" && ! ok {
75
76
panic (fmt .Errorf ("unregistered strategy used as fallback: %s" , fallbackStrategy ))
@@ -83,24 +84,16 @@ func RecordTargetsKept(targets map[string]*target.Item) {
83
84
TargetsRemaining .Set (float64 (len (targets )))
84
85
}
85
86
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
89
90
}
90
91
return nil , fmt .Errorf ("unregistered strategy: %s" , name )
91
92
}
92
93
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
-
101
94
func GetRegisteredAllocatorNames () []string {
102
95
var names []string
103
- for s := range registry {
96
+ for s := range strategies {
104
97
names = append (names , s )
105
98
}
106
99
return names
@@ -123,7 +116,7 @@ type Strategy interface {
123
116
// SetCollectors call. Strategies which don't need this information can just ignore it.
124
117
SetCollectors (map [string ]* Collector )
125
118
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
127
120
SetFallbackStrategy (Strategy )
128
121
}
129
122
@@ -149,20 +142,3 @@ func (c Collector) String() string {
149
142
func NewCollector (name , node string ) * Collector {
150
143
return & Collector {Name : name , NodeName : node }
151
144
}
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
- }
0 commit comments