/
label_selector.go
executable file
·53 lines (44 loc) · 1.33 KB
/
label_selector.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
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"fmt"
"sort"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func printMatchLabels(matchLabels map[string]string) string {
var keys []string
for k := range matchLabels {
keys = append(keys, k)
}
sort.Strings(keys)
var matchLabelStrings []string
for _, k := range keys {
v := matchLabels[k]
matchLabelStrings = append(matchLabelStrings, fmt.Sprintf("%s:%s", k, v))
}
return strings.Join(matchLabelStrings, ", ")
}
func printLabelSelectorRequirement(requirements []metav1.LabelSelectorRequirement) string {
var sections []string
for _, requirement := range requirements {
switch requirement.Operator {
case metav1.LabelSelectorOpIn:
sections = append(sections, fmt.Sprintf("%s in [%s]",
requirement.Key, strings.Join(requirement.Values, ", ")))
case metav1.LabelSelectorOpNotIn:
sections = append(sections, fmt.Sprintf("%s not in [%s]",
requirement.Key, strings.Join(requirement.Values, ", ")))
case metav1.LabelSelectorOpExists:
sections = append(sections, fmt.Sprintf("%s exists",
requirement.Key))
case metav1.LabelSelectorOpDoesNotExist:
sections = append(sections, fmt.Sprintf("%s does not exist",
requirement.Key))
}
}
return strings.Join(sections, ", ")
}