-
Notifications
You must be signed in to change notification settings - Fork 9
/
filter.go
189 lines (165 loc) · 4 KB
/
filter.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package openshift
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/opendevstack/tailor/pkg/utils"
)
var availableKinds = []string{
"svc",
"route",
"dc",
"deployment",
"bc",
"is",
"pvc",
"template",
"cm",
"secret",
"rolebinding",
"serviceaccount",
"cronjob",
"job",
"limitrange",
"quota",
"hpa",
}
type ResourceFilter struct {
Kinds []string
Name string
Label string
ExcludedKinds []string
ExcludedNames []string
ExcludedLabels []string
}
// NewResourceFilter returns a filter based on kinds and flags.
// kindArg might be blank, or a list of kinds (e.g. 'pvc,dc') or
// a kind/name combination (e.g. 'dc/foo').
// selectorFlag might be blank or a key and a label, e.g. 'name=foo'.
func NewResourceFilter(kindArg string, selectorFlag string, excludes []string) (*ResourceFilter, error) {
filter := &ResourceFilter{
Kinds: []string{},
Name: "",
Label: selectorFlag,
}
if len(kindArg) > 0 {
kindArg = strings.ToLower(kindArg)
if strings.Contains(kindArg, "/") {
if strings.Contains(kindArg, ",") {
return nil, errors.New(
"You cannot target more than one resource name",
)
}
nameParts := strings.Split(kindArg, "/")
filter.Name = KindMapping[nameParts[0]] + "/" + nameParts[1]
return filter, nil
}
targetedKinds := make(map[string]bool)
unknownKinds := []string{}
kinds := strings.Split(kindArg, ",")
for _, kind := range kinds {
if _, ok := KindMapping[kind]; !ok {
unknownKinds = append(unknownKinds, kind)
} else {
targetedKinds[KindMapping[kind]] = true
}
}
if len(unknownKinds) > 0 {
return nil, fmt.Errorf(
"Unknown resource kinds: %s",
strings.Join(unknownKinds, ","),
)
}
for kind := range targetedKinds {
filter.Kinds = append(filter.Kinds, kind)
}
sort.Strings(filter.Kinds)
}
unknownKinds := []string{}
for _, v := range excludes {
v = strings.ToLower(v)
if strings.Contains(v, "/") { // Name
nameParts := strings.Split(v, "/")
k := nameParts[0]
if _, ok := KindMapping[k]; !ok {
unknownKinds = append(unknownKinds, k)
} else {
filter.ExcludedNames = append(filter.ExcludedNames, KindMapping[k]+"/"+nameParts[1])
}
} else if strings.Contains(v, "=") { // Label
filter.ExcludedLabels = append(filter.ExcludedLabels, v)
} else { // Kind
if _, ok := KindMapping[v]; !ok {
unknownKinds = append(unknownKinds, v)
} else {
filter.ExcludedKinds = append(filter.ExcludedKinds, KindMapping[v])
}
}
}
if len(unknownKinds) > 0 {
return nil, fmt.Errorf(
"Unknown excluded resource kinds: %s",
strings.Join(unknownKinds, ","),
)
}
return filter, nil
}
func (f *ResourceFilter) String() string {
return fmt.Sprintf("Kinds: %s, Name: %s, Label: %s, ExcludedKinds: %s, ExcludedNames: %s, ExcludedLabels: %s", f.Kinds, f.Name, f.Label, f.ExcludedKinds, f.ExcludedNames, f.ExcludedLabels)
}
func (f *ResourceFilter) SatisfiedBy(item *ResourceItem) bool {
if len(f.Name) > 0 && f.Name != item.FullName() {
return false
}
if len(f.Kinds) > 0 && !utils.Includes(f.Kinds, item.Kind) {
return false
}
if len(f.Label) > 0 {
labels := strings.Split(f.Label, ",")
for _, label := range labels {
if !item.HasLabel(label) {
return false
}
}
}
if len(f.ExcludedNames) > 0 {
if utils.Includes(f.ExcludedNames, item.FullName()) {
return false
}
}
if len(f.ExcludedKinds) > 0 {
if utils.Includes(f.ExcludedKinds, item.Kind) {
return false
}
}
if len(f.ExcludedLabels) > 0 {
for _, el := range f.ExcludedLabels {
if item.HasLabel(el) {
return false
}
}
}
return true
}
func (f *ResourceFilter) ConvertToTarget() string {
if len(f.Name) > 0 {
return f.Name
}
kinds := f.Kinds
if len(kinds) == 0 {
kinds = availableKinds
}
return strings.Join(kinds, ",")
}
func (f *ResourceFilter) ConvertToKinds() string {
if len(f.Name) > 0 {
nameParts := strings.Split(f.Name, "/")
return nameParts[0]
}
kinds := f.Kinds
if len(kinds) == 0 {
kinds = availableKinds
}
return strings.Join(kinds, ",")
}