Debian Codegraph @

kyaml/yaml/internal/k8sgen/pkg/labels/labels.go

raw symbols SCIP
1 // Code generated by k8scopy from k8s.io/[email protected]; DO NOT EDIT.
2 // File content copied from k8s.io/[email protected]/pkg/labels/labels.go
3
4 /*
5 Copyright 2014 The Kubernetes Authors.
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 */
19
20 package labels
21
22 import (
23 "fmt"
24 "sort"
25 "strings"
26 )
27
28 // Labels allows you to present labels independently from their storage.
29 type Labels interface {
30 // Has returns whether the provided label exists.
31 Has(label string) (exists bool)
32
33 // Get returns the value for the provided label.
34 Get(label string) (value string)
35 }
36
37 // Set is a map of label:value. It implements Labels.
38 type Set map[string]string
39
40 // String returns all labels listed as a human readable string.
41 // Conveniently, exactly the format that ParseSelector takes.
42 func (ls Set) String() string {
43 selector := make([]string, 0, len(ls))
44 for key, value := range ls {
45 selector = append(selector, key+"="+value)
46 }
47 // Sort for determinism.
48 sort.StringSlice(selector).Sort()
49 return strings.Join(selector, ",")
50 }
51
52 // Has returns whether the provided label exists in the map.
53 func (ls Set) Has(label string) bool {
54 _, exists := ls[label]
55 return exists
56 }
57
58 // Get returns the value in the map for the provided label.
59 func (ls Set) Get(label string) string {
60 return ls[label]
61 }
62
63 // AsSelector converts labels into a selectors. It does not
64 // perform any validation, which means the server will reject
65 // the request if the Set contains invalid values.
66 func (ls Set) AsSelector() Selector {
67 return SelectorFromSet(ls)
68 }
69
70 // AsValidatedSelector converts labels into a selectors.
71 // The Set is validated client-side, which allows to catch errors early.
72 func (ls Set) AsValidatedSelector() (Selector, error) {
73 return ValidatedSelectorFromSet(ls)
74 }
75
76 // AsSelectorPreValidated converts labels into a selector, but
77 // assumes that labels are already validated and thus doesn't
78 // perform any validation.
79 // According to our measurements this is significantly faster
80 // in codepaths that matter at high scale.
81 func (ls Set) AsSelectorPreValidated() Selector {
82 return SelectorFromValidatedSet(ls)
83 }
84
85 // FormatLabels convert label map into plain string
86 func FormatLabels(labelMap map[string]string) string {
87 l := Set(labelMap).String()
88 if l == "" {
89 l = "<none>"
90 }
91 return l
92 }
93
94 // Conflicts takes 2 maps and returns true if there a key match between
95 // the maps but the value doesn't match, and returns false in other cases
96 func Conflicts(labels1, labels2 Set) bool {
97 small := labels1
98 big := labels2
99 if len(labels2) < len(labels1) {
100 small = labels2
101 big = labels1
102 }
103
104 for k, v := range small {
105 if val, match := big[k]; match {
106 if val != v {
107 return true
108 }
109 }
110 }
111
112 return false
113 }
114
115 // Merge combines given maps, and does not check for any conflicts
116 // between the maps. In case of conflicts, second map (labels2) wins
117 func Merge(labels1, labels2 Set) Set {
118 mergedMap := Set{}
119
120 for k, v := range labels1 {
121 mergedMap[k] = v
122 }
123 for k, v := range labels2 {
124 mergedMap[k] = v
125 }
126 return mergedMap
127 }
128
129 // Equals returns true if the given maps are equal
130 func Equals(labels1, labels2 Set) bool {
131 if len(labels1) != len(labels2) {
132 return false
133 }
134
135 for k, v := range labels1 {
136 value, ok := labels2[k]
137 if !ok {
138 return false
139 }
140 if value != v {
141 return false
142 }
143 }
144 return true
145 }
146
147 // AreLabelsInWhiteList verifies if the provided label list
148 // is in the provided whitelist and returns true, otherwise false.
149 func AreLabelsInWhiteList(labels, whitelist Set) bool {
150 if len(whitelist) == 0 {
151 return true
152 }
153
154 for k, v := range labels {
155 value, ok := whitelist[k]
156 if !ok {
157 return false
158 }
159 if value != v {
160 return false
161 }
162 }
163 return true
164 }
165
166 // ConvertSelectorToLabelsMap converts selector string to labels map
167 // and validates keys and values
168 func ConvertSelectorToLabelsMap(selector string) (Set, error) {
169 labelsMap := Set{}
170
171 if len(selector) == 0 {
172 return labelsMap, nil
173 }
174
175 labels := strings.Split(selector, ",")
176 for _, label := range labels {
177 l := strings.Split(label, "=")
178 if len(l) != 2 {
179 return labelsMap, fmt.Errorf("invalid selector: %s", l)
180 }
181 key := strings.TrimSpace(l[0])
182 if err := validateLabelKey(key); err != nil {
183 return labelsMap, err
184 }
185 value := strings.TrimSpace(l[1])
186 if err := validateLabelValue(key, value); err != nil {
187 return labelsMap, err
188 }
189 labelsMap[key] = value
190 }
191 return labelsMap, nil
192 }