-
Notifications
You must be signed in to change notification settings - Fork 7
/
resourcemeta.go
197 lines (164 loc) · 5.04 KB
/
resourcemeta.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
190
191
192
193
194
195
196
197
/*
Copyright 2021 GramLabs, Inc.
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 filters
import (
"regexp"
"strings"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// ResourceMetaFilter filters nodes based on their resource metadata using regular
// expressions or Kubernetes selectors.
type ResourceMetaFilter struct {
// Regular expression matching the group.
Group string `json:"group,omitempty" yaml:"group,omitempty"`
// Regular expression matching the version.
Version string `json:"version,omitempty" yaml:"version,omitempty"`
// Regular expression matching the kind.
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
// Regular expression matching the namespace.
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
// Regular expression matching the name.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// Kubernetes selector matching labels.
LabelSelector string `json:"labelSelector,omitempty" yaml:"labelSelector,omitempty"`
// Kubernetes selector matching annotations
AnnotationSelector string `json:"annotationSelector,omitempty" yaml:"annotationSelector,omitempty"`
// Invert the matching behavior (i.e. keep non-matching nodes).
InvertMatch bool `json:"invertMatch,omitempty" yaml:"invertMatch,omitempty"`
}
func (f *ResourceMetaFilter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
m, err := newMetaMatcher(f)
if err != nil {
return nil, err
}
if m == nil && f.LabelSelector == "" && f.AnnotationSelector == "" {
return nodes, nil
}
result := make([]*yaml.RNode, 0, len(nodes))
for _, n := range nodes {
if m != nil {
if meta, err := n.GetMeta(); err != nil {
return nil, err
} else if m.matchesMeta(meta) == f.InvertMatch {
continue
}
}
if f.LabelSelector != "" {
if matched, err := n.MatchesLabelSelector(f.LabelSelector); err != nil {
return nil, err
} else if matched == f.InvertMatch {
continue
}
}
if f.AnnotationSelector != "" {
if matched, err := n.MatchesAnnotationSelector(f.AnnotationSelector); err != nil {
return nil, err
} else if matched == f.InvertMatch {
continue
}
}
result = append(result, n)
}
return result, nil
}
type metaMatcher struct {
namespaceRegex *regexp.Regexp
nameRegex *regexp.Regexp
groupRegex *regexp.Regexp
versionRegex *regexp.Regexp
kindRegex *regexp.Regexp
}
func newMetaMatcher(g *ResourceMetaFilter) (m *metaMatcher, err error) {
m = &metaMatcher{}
notEmpty := false
m.namespaceRegex, err = compileAnchored(g.Namespace)
if err != nil {
return nil, err
}
notEmpty = notEmpty || m.namespaceRegex != nil
m.nameRegex, err = compileAnchored(g.Name)
if err != nil {
return nil, err
}
notEmpty = notEmpty || m.nameRegex != nil
m.groupRegex, err = compileAnchored(g.Group)
if err != nil {
return nil, err
}
notEmpty = notEmpty || m.groupRegex != nil
m.versionRegex, err = compileAnchored(g.Version)
if err != nil {
return nil, err
}
notEmpty = notEmpty || m.versionRegex != nil
m.kindRegex, err = compileAnchored(g.Kind)
if err != nil {
return nil, err
}
notEmpty = notEmpty || m.kindRegex != nil
if notEmpty {
return m, nil
}
return nil, nil
}
func (m *metaMatcher) matchesMeta(meta yaml.ResourceMeta) bool {
if m.namespaceRegex != nil && !m.namespaceRegex.MatchString(meta.Namespace) {
return false
}
if m.nameRegex != nil && !m.nameRegex.MatchString(meta.Name) {
return false
}
if m.groupRegex != nil || m.versionRegex != nil {
group, version := "", meta.APIVersion
if pos := strings.Index(version, "/"); pos >= 0 {
group, version = version[0:pos], version[pos+1:]
}
if m.groupRegex != nil && !m.groupRegex.MatchString(group) {
return false
}
if m.versionRegex != nil && !m.versionRegex.MatchString(version) {
return false
}
}
if m.kindRegex != nil && !m.kindRegex.MatchString(meta.Kind) {
return false
}
return true
}
func compileAnchored(pattern string) (*regexp.Regexp, error) {
if pattern == "" {
return nil, nil
}
return regexp.Compile("^(?:" + pattern + ")$")
}
// SetNamespace is like `SetK8sNamespace` except it skips cluster scoped resources.
func SetNamespace(namespace string) yaml.Filter {
return yaml.FilterFunc(func(node *yaml.RNode) (*yaml.RNode, error) {
return node.Pipe(
yaml.Tee(
yaml.FilterFunc(func(node *yaml.RNode) (*yaml.RNode, error) {
md, err := node.GetMeta()
if err != nil {
return nil, err
}
if openapi.IsCertainlyClusterScoped(md.TypeMeta) {
return nil, nil
}
return node, nil
}),
yaml.SetK8sNamespace(namespace),
),
)
})
}