2 Copyright 2014 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
25 // Labels allows you to present labels independently from their storage.
26 type Labels interface {
27 // Has returns whether the provided label exists.
28 Has(label string) (exists bool)
30 // Get returns the value for the provided label.
31 Get(label string) (value string)
34 // Set is a map of label:value. It implements Labels.
35 type Set map[string]string
37 // String returns all labels listed as a human readable string.
38 // Conveniently, exactly the format that ParseSelector takes.
39 func (ls Set) String() string {
40 selector := make([]string, 0, len(ls))
41 for key, value := range ls {
42 selector = append(selector, key+"="+value)
44 // Sort for determinism.
45 sort.StringSlice(selector).Sort()
46 return strings.Join(selector, ",")
49 // Has returns whether the provided label exists in the map.
50 func (ls Set) Has(label string) bool {
51 _, exists := ls[label]
55 // Get returns the value in the map for the provided label.
56 func (ls Set) Get(label string) string {
60 // AsSelector converts labels into a selectors.
61 func (ls Set) AsSelector() Selector {
62 return SelectorFromSet(ls)
65 // AsSelectorPreValidated converts labels into a selector, but
66 // assumes that labels are already validated and thus don't
67 // preform any validation.
68 // According to our measurements this is significantly faster
69 // in codepaths that matter at high scale.
70 func (ls Set) AsSelectorPreValidated() Selector {
71 return SelectorFromValidatedSet(ls)
74 // FormatLabels convert label map into plain string
75 func FormatLabels(labelMap map[string]string) string {
76 l := Set(labelMap).String()
83 // Conflicts takes 2 maps and returns true if there a key match between
84 // the maps but the value doesn't match, and returns false in other cases
85 func Conflicts(labels1, labels2 Set) bool {
88 if len(labels2) < len(labels1) {
93 for k, v := range small {
94 if val, match := big[k]; match {
104 // Merge combines given maps, and does not check for any conflicts
105 // between the maps. In case of conflicts, second map (labels2) wins
106 func Merge(labels1, labels2 Set) Set {
109 for k, v := range labels1 {
112 for k, v := range labels2 {
118 // Equals returns true if the given maps are equal
119 func Equals(labels1, labels2 Set) bool {
120 if len(labels1) != len(labels2) {
124 for k, v := range labels1 {
125 value, ok := labels2[k]
136 // AreLabelsInWhiteList verifies if the provided label list
137 // is in the provided whitelist and returns true, otherwise false.
138 func AreLabelsInWhiteList(labels, whitelist Set) bool {
139 if len(whitelist) == 0 {
143 for k, v := range labels {
144 value, ok := whitelist[k]
155 // ConvertSelectorToLabelsMap converts selector string to labels map
156 // and validates keys and values
157 func ConvertSelectorToLabelsMap(selector string) (Set, error) {
160 if len(selector) == 0 {
161 return labelsMap, nil
164 labels := strings.Split(selector, ",")
165 for _, label := range labels {
166 l := strings.Split(label, "=")
168 return labelsMap, fmt.Errorf("invalid selector: %s", l)
170 key := strings.TrimSpace(l[0])
171 if err := validateLabelKey(key); err != nil {
172 return labelsMap, err
174 value := strings.TrimSpace(l[1])
175 if err := validateLabelValue(value); err != nil {
176 return labelsMap, err
178 labelsMap[key] = value
180 return labelsMap, nil