-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathselectors.go
201 lines (178 loc) · 6.74 KB
/
selectors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package manager
import (
"fmt"
"strings"
)
// ProbesSelector - A probe selector defines how a probe (or a group of probes) should be activated.
//
// For example, this can be used to specify that out of a group of optional probes, at least one should be activated.
type ProbesSelector interface {
// GetProbesIdentificationPairList - Returns the list of probes that this selector activates
GetProbesIdentificationPairList() []ProbeIdentificationPair
// RunValidator - Ensures that the probes that were successfully activated follow the selector goal.
// For example, see OneOf.
RunValidator(manager *Manager) error
// EditProbeIdentificationPair - Changes all the selectors looking for the old ProbeIdentificationPair so that they
// mow select the new one
EditProbeIdentificationPair(old ProbeIdentificationPair, new ProbeIdentificationPair)
}
// ProbeSelector - This selector is used to unconditionally select a probe by its identification pair and validate
// that it is activated
type ProbeSelector struct {
ProbeIdentificationPair
}
// GetProbesIdentificationPairList - Returns the list of probes that this selector activates
func (ps *ProbeSelector) GetProbesIdentificationPairList() []ProbeIdentificationPair {
if ps == nil {
return nil
}
return []ProbeIdentificationPair{ps.ProbeIdentificationPair}
}
// RunValidator - Ensures that the probes that were successfully activated follow the selector goal.
// For example, see OneOf.
func (ps *ProbeSelector) RunValidator(manager *Manager) error {
if ps == nil {
return nil
}
p, ok := manager.GetProbe(ps.ProbeIdentificationPair)
if !ok {
return fmt.Errorf("probe not found: %s", ps.ProbeIdentificationPair)
}
if !p.IsRunning() && p.Enabled {
return fmt.Errorf("%s: %w", ps.ProbeIdentificationPair.String(), p.GetLastError())
}
if !p.Enabled {
return fmt.Errorf(
"%s: is disabled, add it to the activation list and check that it was not explicitly excluded by the manager options",
ps.ProbeIdentificationPair.String())
}
return nil
}
// EditProbeIdentificationPair - Changes all the selectors looking for the old ProbeIdentificationPair so that they
// mow select the new one
func (ps *ProbeSelector) EditProbeIdentificationPair(old ProbeIdentificationPair, new ProbeIdentificationPair) {
if ps.ProbeIdentificationPair == old {
ps.ProbeIdentificationPair = new
}
}
// OneOf - This selector is used to ensure that at least of a list of probe selectors is valid. In other words, this
// can be used to ensure that at least one of a list of optional probes is activated.
type OneOf struct {
Selectors []ProbesSelector
}
// GetProbesIdentificationPairList - Returns the list of probes that this selector activates
func (oo *OneOf) GetProbesIdentificationPairList() []ProbeIdentificationPair {
var l []ProbeIdentificationPair
for _, selector := range oo.Selectors {
l = append(l, selector.GetProbesIdentificationPairList()...)
}
return l
}
// RunValidator - Ensures that the probes that were successfully activated follow the selector goal.
// For example, see OneOf.
func (oo *OneOf) RunValidator(manager *Manager) error {
var errs []string
for _, selector := range oo.Selectors {
if err := selector.RunValidator(manager); err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) == len(oo.Selectors) {
return fmt.Errorf(
"OneOf requirement failed, none of the following probes are running [%s]",
strings.Join(errs, " | "))
}
// at least one selector was successful
return nil
}
func (oo *OneOf) String() string {
var strs []string
for _, id := range oo.GetProbesIdentificationPairList() {
str := id.String()
strs = append(strs, str)
}
return "OneOf " + strings.Join(strs, ", ")
}
// EditProbeIdentificationPair - Changes all the selectors looking for the old ProbeIdentificationPair so that they
// now select the new one
func (oo *OneOf) EditProbeIdentificationPair(old ProbeIdentificationPair, new ProbeIdentificationPair) {
for _, selector := range oo.Selectors {
selector.EditProbeIdentificationPair(old, new)
}
}
// AllOf - This selector is used to ensure that all the proves in the provided list are running.
type AllOf struct {
Selectors []ProbesSelector
}
// GetProbesIdentificationPairList - Returns the list of probes that this selector activates
func (ao *AllOf) GetProbesIdentificationPairList() []ProbeIdentificationPair {
var l []ProbeIdentificationPair
for _, selector := range ao.Selectors {
l = append(l, selector.GetProbesIdentificationPairList()...)
}
return l
}
// RunValidator - Ensures that the probes that were successfully activated follow the selector goal.
// For example, see OneOf.
func (ao *AllOf) RunValidator(manager *Manager) error {
var errMsg []string
for _, selector := range ao.Selectors {
if err := selector.RunValidator(manager); err != nil {
errMsg = append(errMsg, err.Error())
}
}
if len(errMsg) > 0 {
return fmt.Errorf(
"AllOf requirement failed, the following probes are not running [%s]",
strings.Join(errMsg, " | "))
}
// no error means that all the selectors were successful
return nil
}
func (ao *AllOf) String() string {
var strs []string
for _, id := range ao.GetProbesIdentificationPairList() {
str := id.String()
strs = append(strs, str)
}
return "AllOf " + strings.Join(strs, ", ")
}
// EditProbeIdentificationPair - Changes all the selectors looking for the old ProbeIdentificationPair so that they
// now select the new one
func (ao *AllOf) EditProbeIdentificationPair(old ProbeIdentificationPair, new ProbeIdentificationPair) {
for _, selector := range ao.Selectors {
selector.EditProbeIdentificationPair(old, new)
}
}
// BestEffort - This selector is used to load probes in the best effort mode
type BestEffort struct {
Selectors []ProbesSelector
}
// GetProbesIdentificationPairList - Returns the list of probes that this selector activates
func (be *BestEffort) GetProbesIdentificationPairList() []ProbeIdentificationPair {
var l []ProbeIdentificationPair
for _, selector := range be.Selectors {
l = append(l, selector.GetProbesIdentificationPairList()...)
}
return l
}
// RunValidator - Ensures that the probes that were successfully activated follow the selector goal.
// For example, see OneOf.
func (be *BestEffort) RunValidator(_ *Manager) error {
return nil
}
func (be *BestEffort) String() string {
var strs []string
for _, id := range be.GetProbesIdentificationPairList() {
str := id.String()
strs = append(strs, str)
}
return "BestEffort " + strings.Join(strs, ", ")
}
// EditProbeIdentificationPair - Changes all the selectors looking for the old ProbeIdentificationPair so that they
// now select the new one
func (be *BestEffort) EditProbeIdentificationPair(old ProbeIdentificationPair, new ProbeIdentificationPair) {
for _, selector := range be.Selectors {
selector.EditProbeIdentificationPair(old, new)
}
}