-
Notifications
You must be signed in to change notification settings - Fork 18
/
labels.go
390 lines (338 loc) · 8.78 KB
/
labels.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package v1
import (
"encoding/json"
"reflect"
"regexp"
"github.com/mandelsoft/goutils/errors"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/equivalent"
"github.com/open-component-model/ocm/pkg/listformat"
"github.com/open-component-model/ocm/pkg/runtime"
)
const (
KIND_LABEL = "label"
KIND_VALUE_MERGE_ALGORITHM = "label merge algorithm"
)
type MergeAlgorithmSpecification struct {
// Algorithm optionally described the Merge algorithm used to
// merge the label value during a transfer.
Algorithm string `json:"algorithm"`
// eConfig contains optional config for the merge algorithm.
Config json.RawMessage `json:"config,omitempty"`
}
var _ listformat.DirectDescriptionSource = (*MergeAlgorithmSpecification)(nil)
func (s *MergeAlgorithmSpecification) Description() string {
return s.Algorithm
}
func NewMergeAlgorithmSpecification(algo string, spec interface{}) (*MergeAlgorithmSpecification, error) {
m, err := runtime.AsRawMessage(spec)
if err != nil {
return nil, err
}
return &MergeAlgorithmSpecification{
Algorithm: algo,
Config: m,
}, nil
}
////////////////////////////////////////////////////////////////////////////////
// Label is a label that can be set on objects.
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type Label struct {
// Name is the unique name of the label.
Name string `json:"name"`
// Value is the json/yaml data of the label
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Schemaless
Value json.RawMessage `json:"value"`
// Version is the optional specification version of the attribute value
Version string `json:"version,omitempty"`
// Signing describes whether the label should be included into the signature
Signing bool `json:"signing,omitempty"`
// MergeAlgorithm optionally describes the desired merge handling used to
// merge the label value during a transfer.
Merge *MergeAlgorithmSpecification `json:"merge,omitempty"`
}
// DeepCopyInto copies labels.
func (in *Label) DeepCopyInto(out *Label) {
*out = *in
out.Value = append(out.Value[:0:0], in.Value...)
}
// GetValue returns the label value with the given name as parsed object.
func (in *Label) GetValue(dest interface{}) error {
return json.Unmarshal(in.Value, dest)
}
// SetValue sets the label value by marshalling the given object.
// A passed byte slice is validated to be valid json.
func (in *Label) SetValue(value interface{}) error {
var v runtime.RawValue
err := v.SetValue(value)
if err != nil {
return err
}
in.Value = v.RawMessage
return nil
}
var versionRegex = regexp.MustCompile("^v[0-9]+$")
func NewLabel(name string, value interface{}, opts ...LabelOption) (*Label, error) {
l := Label{Name: name}
err := l.SetValue(value)
if err != nil {
return nil, err
}
for _, o := range opts {
if err := o.ApplyToLabel(&l); err != nil {
return nil, errors.Wrapf(err, "label %q", name)
}
}
return &l, nil
}
// Labels describe a list of labels
// +k8s:deepcopy-gen=true
// +k8s:openapi-gen=true
type Labels []Label
// GetIndex returns the index of the given label or -1 if not found.
func (l Labels) GetIndex(name string) int {
for i, label := range l {
if label.Name == name {
return i
}
}
return -1
}
// GetDef returns the label definition of the given label.
func (l Labels) GetDef(name string) *Label {
for i, label := range l {
if label.Name == name {
return &l[i]
}
}
return nil
}
// SetDef ets a label definition.
func (l *Labels) SetDef(name string, value *Label) {
for i, label := range *l {
if label.Name == name {
(*l)[i] = *value
return
}
}
*l = append(*l, *value)
}
// Get returns the label value with the given name as json string.
func (l Labels) Get(name string) ([]byte, bool) {
for _, label := range l {
if label.Name == name {
return label.Value, true
}
}
return nil, false
}
// GetValue returns the label value with the given name as parsed object.
func (l Labels) GetValue(name string, dest interface{}) (bool, error) {
for _, label := range l {
if label.Name == name {
return true, label.GetValue(dest)
}
}
return false, nil
}
// Set sets or modifies a label including its meta data.
func (l *Labels) Set(name string, value interface{}, opts ...LabelOption) error {
newLabel, err := NewLabel(name, value, opts...)
if err != nil {
return err
}
for i, label := range *l {
if label.Name == name {
(*l)[i] = *newLabel
return nil
}
}
*l = append(*l, *newLabel)
return nil
}
// Set sets or modifies the label meta data.
func (l *Labels) SetOptions(name string, opts ...LabelOption) error {
newLabel, err := NewLabel(name, nil, opts...)
if err != nil {
return err
}
for i, label := range *l {
if label.Name == name {
newLabel.Value = label.Value
(*l)[i] = *newLabel
return nil
}
}
return errors.ErrNotFound(KIND_LABEL, name)
}
// SetValue sets or modifies the value of a label, the label metadata
// is not touched.
func (l *Labels) SetValue(name string, value interface{}) error {
newLabel, err := NewLabel(name, value)
if err != nil {
return err
}
for i, label := range *l {
if label.Name == name {
(*l)[i].Value = newLabel.Value
return nil
}
}
*l = append(*l, *newLabel)
return nil
}
func (l *Labels) Remove(name string) bool {
for i, label := range *l {
if label.Name == name {
*l = append((*l)[:i], (*l)[i+1:]...)
return true
}
}
return false
}
func (l *Labels) Clear() {
*l = nil
}
func (l Labels) Equivalent(o Labels) equivalent.EqualState {
state := equivalent.StateEquivalent()
for _, ol := range o {
ll := l.GetDef(ol.Name)
if ol.Signing {
if ll == nil || !reflect.DeepEqual(&ol, ll) {
state = state.NotLocalHashEqual()
}
} else {
if ll != nil {
if ll.Signing {
state = state.NotLocalHashEqual()
}
if !reflect.DeepEqual(&ol, ll) {
state = state.NotEquivalent()
}
} else {
state = state.NotEquivalent()
}
}
}
for _, ll := range l {
ol := o.GetDef(ll.Name)
if ol == nil {
if ll.Signing {
state = state.NotLocalHashEqual()
}
state = state.NotEquivalent()
}
}
return state
}
// AsMap return an unmarshalled map representation.
func (l *Labels) AsMap() map[string]interface{} {
labels := map[string]interface{}{}
if l != nil {
for _, label := range *l {
var m interface{}
json.Unmarshal(label.Value, &m)
labels[label.Name] = m
}
}
return labels
}
// Copy copies labels.
func (l Labels) Copy() Labels {
if l == nil {
return nil
}
n := make(Labels, len(l))
copy(n, l)
return n
}
// ValidateLabels validates a list of labels.
func ValidateLabels(fldPath *field.Path, labels Labels) field.ErrorList {
allErrs := field.ErrorList{}
labelNames := make(map[string]struct{})
for i, label := range labels {
labelPath := fldPath.Index(i)
if len(label.Name) == 0 {
allErrs = append(allErrs, field.Required(labelPath.Child("name"), "must specify a name"))
continue
}
if _, ok := labelNames[label.Name]; ok {
allErrs = append(allErrs, field.Duplicate(labelPath, "duplicate label name"))
continue
}
labelNames[label.Name] = struct{}{}
}
return allErrs
}
type LabelOption interface {
ApplyToLabel(l *Label) error
}
type labelOptVersion struct {
version string
}
var _ LabelOption = (*labelOptVersion)(nil)
func WithVersion(v string) LabelOption {
return &labelOptVersion{v}
}
func CheckLabelVersion(v string) bool {
return versionRegex.MatchString(v)
}
func (o labelOptVersion) ApplyToLabel(l *Label) error {
if !CheckLabelVersion(o.version) {
return errors.ErrInvalid("version", o.version)
}
l.Version = o.version
return nil
}
////////////////////////////////////////////////////////////////////////////////
type labelOptSigning struct {
sign bool
}
var _ LabelOption = (*labelOptSigning)(nil)
func WithSigning(b ...bool) LabelOption {
s := true
for _, o := range b {
s = o
}
return &labelOptSigning{s}
}
func (o *labelOptSigning) ApplyToLabel(l *Label) error {
l.Signing = o.sign
return nil
}
////////////////////////////////////////////////////////////////////////////////
// LabelMergeHandlerConfig must be label merge handler config. but cannot be checked
// because of cyclic package dependencies.
type LabelMergeHandlerConfig interface{}
type labelOptMerge struct {
cfg json.RawMessage
algo string
}
var _ LabelOption = (*labelOptMerge)(nil)
func WithMerging(algo string, cfg LabelMergeHandlerConfig) LabelOption {
var data []byte
if cfg != nil {
var err error
data, err = json.Marshal(cfg)
if err != nil {
return nil
}
}
return &labelOptMerge{algo: algo, cfg: data}
}
func (o *labelOptMerge) ApplyToLabel(l *Label) error {
if o.algo != "" || len(o.cfg) > 0 {
l.Merge = &MergeAlgorithmSpecification{}
if o.algo != "" {
l.Merge.Algorithm = o.algo
}
if len(o.cfg) > 0 {
l.Merge.Config = o.cfg
}
} else {
l.Merge = nil
}
return nil
}