-
Notifications
You must be signed in to change notification settings - Fork 19
/
labelset.go
58 lines (50 loc) · 1.59 KB
/
labelset.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
package lang
import "reflect"
// LabelCluster is a special label name where cluster should be stored. It's required by the engine during policy processing
const LabelCluster = "cluster"
// LabelSet defines the set of labels that will be manipulated throughout policy execution. All labels are stored
// in a 'key' -> 'value' map
type LabelSet struct {
Labels map[string]string
}
// NewLabelSet creates a new LabelSet from a given map of text labels
func NewLabelSet(labels map[string]string) *LabelSet {
result := &LabelSet{Labels: make(map[string]string, len(labels))}
result.AddLabels(labels)
return result
}
// AddLabels adds new labels to the current set of labels
func (src *LabelSet) AddLabels(addMap map[string]string) {
for k, v := range addMap {
src.Labels[k] = v
}
}
// ApplyTransform applies a given set of label transformations to the current set of labels.
// The method teturns true if changes have been made to the current set
func (src *LabelSet) ApplyTransform(ops LabelOperations) bool {
changed := false
if ops != nil {
// set labels
for k, v := range ops["set"] {
if src.Labels[k] != v {
src.Labels[k] = v
changed = true
}
}
// remove labels
for k := range ops["remove"] {
if _, exists := src.Labels[k]; exists {
delete(src.Labels, k)
changed = true
}
}
}
return changed
}
// Equal compares two labels sets. If one is nil and another one is empty, it will return true as well
func (src *LabelSet) Equal(dst *LabelSet) bool {
if len(src.Labels) == 0 && len(dst.Labels) == 0 {
return true
}
return reflect.DeepEqual(src.Labels, dst.Labels)
}