-
Notifications
You must be signed in to change notification settings - Fork 11
/
label_query.go
195 lines (168 loc) · 4.51 KB
/
label_query.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package resource
import (
"slices"
)
// LabelOp is a match operation on labels.
type LabelOp int
// LabelOp values.
const (
// Label with the key exists.
LabelOpExists LabelOp = iota
// Label with the key matches the value.
LabelOpEqual
// Label value is in the set.
LabelOpIn
// Label value is less.
LabelOpLT
// Label value is less or equal.
LabelOpLTE
// Label value is less than number.
LabelOpLTNumeric
// Label value is less or equal numeric.
LabelOpLTENumeric
)
func (l LabelOp) isComparison() bool {
//nolint:exhaustive
switch l {
case LabelOpLT:
case LabelOpLTE:
case LabelOpLTNumeric:
case LabelOpLTENumeric:
default:
return false
}
return true
}
// LabelTerm describes a filter on metadata labels.
type LabelTerm struct {
Key string
Value []string
Op LabelOp
Invert bool
}
// LabelQueries is the list of label queries.
type LabelQueries []LabelQuery
// Matches iterates through the list of label queries and matches the labels using OR condition.
func (queries LabelQueries) Matches(labels Labels) bool {
if len(queries) == 0 {
return true
}
for _, query := range queries {
if query.Matches(labels) {
return true
}
}
return false
}
// LabelQuery is a set of LabelTerms applied with AND semantics.
type LabelQuery struct {
Terms []LabelTerm
}
// Matches if the metadata labels matches the label query.
func (query LabelQuery) Matches(labels Labels) bool {
if len(query.Terms) == 0 {
return true
}
for _, term := range query.Terms {
if !labels.Matches(term) {
return false
}
}
return true
}
// LabelQueryOption allows to build a LabelQuery with functional parameters.
type LabelQueryOption func(*LabelQuery)
// TermOption defines additional term options.
type TermOption int
const (
// NotMatches the condition.
NotMatches TermOption = iota
)
func getInvert(opts []TermOption) bool {
return slices.Contains(opts, NotMatches)
}
// LabelExists checks that the label is set.
func LabelExists(label string, opts ...TermOption) LabelQueryOption {
return func(q *LabelQuery) {
q.Terms = append(q.Terms, LabelTerm{
Key: label,
Op: LabelOpExists,
Invert: getInvert(opts),
})
}
}
// LabelEqual checks that the label is set to the specified value.
func LabelEqual(label, value string, opts ...TermOption) LabelQueryOption {
return func(q *LabelQuery) {
q.Terms = append(q.Terms, LabelTerm{
Key: label,
Value: []string{value},
Op: LabelOpEqual,
Invert: getInvert(opts),
})
}
}
// LabelIn checks that the label value is in the provided set.
func LabelIn(label string, set []string, opts ...TermOption) LabelQueryOption {
return func(q *LabelQuery) {
q.Terms = append(q.Terms, LabelTerm{
Key: label,
Value: set,
Op: LabelOpIn,
Invert: getInvert(opts),
})
}
}
// LabelLT checks that the label value is less than value, peforms string comparison.
func LabelLT(label, value string, opts ...TermOption) LabelQueryOption {
return func(q *LabelQuery) {
q.Terms = append(q.Terms, LabelTerm{
Key: label,
Value: []string{value},
Op: LabelOpLT,
Invert: getInvert(opts),
})
}
}
// LabelLTE checks that the label value is less or equal to value, peforms string comparison.
func LabelLTE(label, value string, opts ...TermOption) LabelQueryOption {
return func(q *LabelQuery) {
q.Terms = append(q.Terms, LabelTerm{
Key: label,
Value: []string{value},
Op: LabelOpLTE,
Invert: getInvert(opts),
})
}
}
// LabelLTNumeric checks that the label value is less than value, peforms numeric comparison, if possible.
func LabelLTNumeric(label, value string, opts ...TermOption) LabelQueryOption {
return func(q *LabelQuery) {
q.Terms = append(q.Terms, LabelTerm{
Key: label,
Value: []string{value},
Op: LabelOpLTNumeric,
Invert: getInvert(opts),
})
}
}
// LabelLTENumeric checks that the label value is less or equal to value, peforms numeric comparison, if possible.
func LabelLTENumeric(label, value string, opts ...TermOption) LabelQueryOption {
return func(q *LabelQuery) {
q.Terms = append(q.Terms, LabelTerm{
Key: label,
Value: []string{value},
Op: LabelOpLTENumeric,
Invert: getInvert(opts),
})
}
}
// RawLabelQuery sets the label query to the verbatim value.
func RawLabelQuery(query LabelQuery) LabelQueryOption {
return func(q *LabelQuery) {
*q = query
}
}