-
Notifications
You must be signed in to change notification settings - Fork 1
/
relationship_selector.go
227 lines (190 loc) · 6.64 KB
/
relationship_selector.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package duty
import (
"fmt"
"strings"
"github.com/flanksource/commons/logger"
"github.com/flanksource/gomplate/v3"
"github.com/google/uuid"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/types"
)
// Lookup offers different ways to specify a lookup value
//
// +kubebuilder:object:generate=true
type Lookup struct {
Expr string `json:"expr,omitempty"`
Value string `json:"value,omitempty"`
Label string `json:"label,omitempty"`
}
func (t *Lookup) Eval(labels map[string]string, envVar map[string]any) (string, error) {
if t.Value != "" {
return t.Value, nil
}
if t.Label != "" {
return labels[t.Label], nil
}
if t.Expr != "" {
res, err := gomplate.RunTemplate(envVar, gomplate.Template{Expression: t.Expr})
if err != nil {
return "", err
}
return res, nil
}
return "", nil
}
func (t Lookup) IsEmpty() bool {
return t.Value == "" && t.Label == "" && t.Expr == ""
}
// +kubebuilder:object:generate=true
// RelationshipSelector is the evaluated output of RelationshipSelectorTemplate.
type RelationshipSelector struct {
ID string `json:"id,omitempty"`
ExternalID string `json:"external_id,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Type string `json:"type,omitempty"`
Agent string `json:"agent,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
// Scope is the id parent of the resource to select.
// Example: For config items, the scope is the scraper id
// - for checks, it's canaries and
// - for components, it's topology.
Scope string `json:"scope,omitempty"`
}
func (t *RelationshipSelector) IsEmpty() bool {
return t.ID == "" && t.ExternalID == "" && t.Name == "" && t.Namespace == "" && t.Type == "" && t.Agent == "" && len(t.Labels) == 0 && t.Scope == ""
}
func (t *RelationshipSelector) ToResourceSelector() types.ResourceSelector {
var labelSelector string
for k, v := range t.Labels {
labelSelector += fmt.Sprintf("%s=%s,", k, v)
}
labelSelector = strings.TrimSuffix(labelSelector, ",")
rs := types.ResourceSelector{
ID: t.ID,
Scope: t.Scope,
Name: t.Name,
Agent: t.Agent,
LabelSelector: labelSelector,
Namespace: t.Namespace,
}
if t.Type != "" {
rs.Types = []string{t.Type}
}
if t.ExternalID != "" {
rs.FieldSelector = fmt.Sprintf("external_id=%s", t.ExternalID)
}
return rs
}
// +kubebuilder:object:generate=true
type RelationshipSelectorTemplate struct {
ID Lookup `json:"id,omitempty"`
ExternalID Lookup `json:"external_id,omitempty"`
Name Lookup `json:"name,omitempty"`
Namespace Lookup `json:"namespace,omitempty"`
Type Lookup `json:"type,omitempty"`
// Agent can be one of
// - agent id
// - agent name
// - 'self' (no agent)
Agent Lookup `json:"agent,omitempty"`
// Scope is the id of the parent of the resource to select.
// Example: For config items, the scope is the scraper id
// - for checks, it's canaries and
// - for components, it's topology.
// If left empty, the scope is the requester's scope.
// Use `all` to disregard scope.
Scope Lookup `json:"scope,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
func (t *RelationshipSelectorTemplate) IsEmpty() bool {
return t.ID.IsEmpty() && t.ExternalID.IsEmpty() && t.Name.IsEmpty() && t.Namespace.IsEmpty() &&
t.Scope.IsEmpty() && t.Type.IsEmpty() && t.Agent.IsEmpty() && len(t.Labels) == 0
}
// Eval evaluates the template and returns a RelationshipSelector.
// If any of the filter returns an empty value, the evaluation results to a nil selector.
// i.e. if a lookup is non-empty, it must return a non-empty value.
func (t *RelationshipSelectorTemplate) Eval(labels map[string]string, env map[string]any) (*RelationshipSelector, error) {
if t.IsEmpty() {
return nil, nil
}
var err error
var output = RelationshipSelector{
Labels: t.Labels,
}
if !t.ID.IsEmpty() {
if output.ID, err = t.ID.Eval(labels, env); err != nil {
return nil, fmt.Errorf("failed to evaluate id: %v for relationship: %w", t.ID, err)
} else if output.ID == "" {
return nil, nil
}
}
if !t.ExternalID.IsEmpty() {
if output.ExternalID, err = t.ExternalID.Eval(labels, env); err != nil {
return nil, fmt.Errorf("failed to evaluate external id: %v for relationship: %w", t.ExternalID, err)
} else if output.ExternalID == "" {
return nil, nil
}
}
if !t.Name.IsEmpty() {
if output.Name, err = t.Name.Eval(labels, env); err != nil {
return nil, fmt.Errorf("failed to evaluate name: %v for relationship: %w", t.Name, err)
} else if output.Name == "" {
return nil, nil
}
}
if !t.Namespace.IsEmpty() {
if output.Namespace, err = t.Namespace.Eval(labels, env); err != nil {
return nil, fmt.Errorf("failed to evaluate namespace: %v for relationship: %w", t.Namespace, err)
} else if output.Namespace == "" {
return nil, nil
}
}
if !t.Type.IsEmpty() {
if output.Type, err = t.Type.Eval(labels, env); err != nil {
return nil, fmt.Errorf("failed to evaluate type: %v for relationship: %w", t.Type, err)
} else if output.Type == "" {
return nil, nil
}
}
if !t.Agent.IsEmpty() {
if output.Agent, err = t.Agent.Eval(labels, env); err != nil {
return nil, fmt.Errorf("failed to evaluate agent_id: %v for relationship: %w", t.Agent, err)
} else if output.Agent == "" {
return nil, nil
}
}
if !t.Scope.IsEmpty() {
if output.Scope, err = t.Scope.Eval(labels, env); err != nil {
return nil, fmt.Errorf("failed to evaluate scope: %v for relationship: %w", t.Scope, err)
} else if output.Scope == "" {
return nil, nil
}
}
return &output, nil
}
func LookupComponents(ctx context.Context, lookup RelationshipSelectorTemplate, labels map[string]string, env map[string]any) ([]uuid.UUID, error) {
lookupResult, err := lookup.Eval(labels, env)
if err != nil {
return nil, fmt.Errorf("error evaluating lookup spec: %w", err)
} else if lookupResult == nil {
return nil, nil
}
if ctx.IsTrace() {
logger.Tracef("finding all components (%s)", lookupResult)
}
return query.FindComponentIDs(ctx, 0, lookupResult.ToResourceSelector())
}
func LookupConfigs(ctx context.Context, lookup RelationshipSelectorTemplate, labels map[string]string, env map[string]any) ([]uuid.UUID, error) {
lookupResult, err := lookup.Eval(labels, env)
if err != nil {
return nil, fmt.Errorf("error evaluating lookup spec: %w", err)
} else if lookupResult == nil {
return nil, nil
}
if ctx.IsTrace() {
logger.Tracef("finding all config items (%s)", lookupResult)
}
return query.FindConfigIDsByResourceSelector(ctx, 0, lookupResult.ToResourceSelector())
}