123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- /*
- Copyright 2014 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package labels
- import (
- "fmt"
- "sort"
- "strings"
- )
- // Labels allows you to present labels independently from their storage.
- type Labels interface {
- // Has returns whether the provided label exists.
- Has(label string) (exists bool)
- // Get returns the value for the provided label.
- Get(label string) (value string)
- }
- // Set is a map of label:value. It implements Labels.
- type Set map[string]string
- // String returns all labels listed as a human readable string.
- // Conveniently, exactly the format that ParseSelector takes.
- func (ls Set) String() string {
- selector := make([]string, 0, len(ls))
- for key, value := range ls {
- selector = append(selector, key+"="+value)
- }
- // Sort for determinism.
- sort.StringSlice(selector).Sort()
- return strings.Join(selector, ",")
- }
- // Has returns whether the provided label exists in the map.
- func (ls Set) Has(label string) bool {
- _, exists := ls[label]
- return exists
- }
- // Get returns the value in the map for the provided label.
- func (ls Set) Get(label string) string {
- return ls[label]
- }
- // AsSelector converts labels into a selectors.
- func (ls Set) AsSelector() Selector {
- return SelectorFromSet(ls)
- }
- // AsSelectorPreValidated converts labels into a selector, but
- // assumes that labels are already validated and thus don't
- // preform any validation.
- // According to our measurements this is significantly faster
- // in codepaths that matter at high scale.
- func (ls Set) AsSelectorPreValidated() Selector {
- return SelectorFromValidatedSet(ls)
- }
- // FormatLabels convert label map into plain string
- func FormatLabels(labelMap map[string]string) string {
- l := Set(labelMap).String()
- if l == "" {
- l = "<none>"
- }
- return l
- }
- // Conflicts takes 2 maps and returns true if there a key match between
- // the maps but the value doesn't match, and returns false in other cases
- func Conflicts(labels1, labels2 Set) bool {
- small := labels1
- big := labels2
- if len(labels2) < len(labels1) {
- small = labels2
- big = labels1
- }
- for k, v := range small {
- if val, match := big[k]; match {
- if val != v {
- return true
- }
- }
- }
- return false
- }
- // Merge combines given maps, and does not check for any conflicts
- // between the maps. In case of conflicts, second map (labels2) wins
- func Merge(labels1, labels2 Set) Set {
- mergedMap := Set{}
- for k, v := range labels1 {
- mergedMap[k] = v
- }
- for k, v := range labels2 {
- mergedMap[k] = v
- }
- return mergedMap
- }
- // Equals returns true if the given maps are equal
- func Equals(labels1, labels2 Set) bool {
- if len(labels1) != len(labels2) {
- return false
- }
- for k, v := range labels1 {
- value, ok := labels2[k]
- if !ok {
- return false
- }
- if value != v {
- return false
- }
- }
- return true
- }
- // AreLabelsInWhiteList verifies if the provided label list
- // is in the provided whitelist and returns true, otherwise false.
- func AreLabelsInWhiteList(labels, whitelist Set) bool {
- if len(whitelist) == 0 {
- return true
- }
- for k, v := range labels {
- value, ok := whitelist[k]
- if !ok {
- return false
- }
- if value != v {
- return false
- }
- }
- return true
- }
- // ConvertSelectorToLabelsMap converts selector string to labels map
- // and validates keys and values
- func ConvertSelectorToLabelsMap(selector string) (Set, error) {
- labelsMap := Set{}
- if len(selector) == 0 {
- return labelsMap, nil
- }
- labels := strings.Split(selector, ",")
- for _, label := range labels {
- l := strings.Split(label, "=")
- if len(l) != 2 {
- return labelsMap, fmt.Errorf("invalid selector: %s", l)
- }
- key := strings.TrimSpace(l[0])
- if err := validateLabelKey(key); err != nil {
- return labelsMap, err
- }
- value := strings.TrimSpace(l[1])
- if err := validateLabelValue(value); err != nil {
- return labelsMap, err
- }
- labelsMap[key] = value
- }
- return labelsMap, nil
- }
|