Skip to content

Commit f49a5b6

Browse files
committed
Support singular and short resources names in filters
So a given k8s resources can be filter by it's name (plural, eg. "enpoints"), it's singular ("endpoint") or one of it's short names (eg. "ep", "deploy", "po"...).
1 parent ecde0b4 commit f49a5b6

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ will help to keep resources usage low, and a concise git history. Eg.:
3737

3838
katafygio -e /tmp/kfdump \
3939
-g https://user:[email protected]/myorg/myrepos.git \
40-
-x secret,pod,event,replicaset,node,endpoints \
40+
-x secret,pod,event,replicaset,node,endpoint \
4141
-y configmap:kube-system/leader-elector
4242
```
4343

pkg/observer/observer.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (c *Observer) expandAndFilterAPIResources(groups []*metav1.APIResourceList)
185185
}
186186

187187
// remove user filtered objet kinds
188-
if isExcluded(c.excludedkind, ar.Kind) {
188+
if isExcluded(c.excludedkind, ar) {
189189
continue
190190
}
191191

@@ -210,12 +210,26 @@ func (c *Observer) expandAndFilterAPIResources(groups []*metav1.APIResourceList)
210210
return resources
211211
}
212212

213-
func isExcluded(excluded []string, name string) bool {
214-
lname := strings.ToLower(name)
213+
func isExcluded(excluded []string, ar metav1.APIResource) bool {
214+
lname := strings.ToLower(ar.Name)
215+
singular := strings.ToLower(ar.SingularName)
216+
215217
for _, ctl := range excluded {
216-
if strings.Compare(lname, strings.ToLower(ctl)) == 0 {
218+
excl := strings.ToLower(ctl)
219+
220+
if strings.Compare(lname, excl) == 0 {
221+
return true
222+
}
223+
224+
if strings.Compare(singular, excl) == 0 {
217225
return true
218226
}
227+
228+
for _, alt := range ar.ShortNames {
229+
if strings.Compare(strings.ToLower(alt), excl) == 0 {
230+
return true
231+
}
232+
}
219233
}
220234

221235
return false

pkg/observer/observer_test.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/bpineau/katafygio/pkg/controller"
1010
"github.com/bpineau/katafygio/pkg/event"
1111

12-
appsv1beta2 "k8s.io/api/apps/v1beta2"
12+
appsv1 "k8s.io/api/apps/v1"
1313
corev1 "k8s.io/api/core/v1"
1414
extv1beta1 "k8s.io/api/extensions/v1beta1"
1515
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -92,7 +92,7 @@ var resourcesTests = []resTest{
9292
expect: []string{"deployment"},
9393
resources: []*metav1.APIResourceList{
9494
{
95-
GroupVersion: appsv1beta2.SchemeGroupVersion.String(),
95+
GroupVersion: appsv1.SchemeGroupVersion.String(),
9696
APIResources: []metav1.APIResource{
9797
{Name: "deployments", Namespaced: true, Kind: "Deployment", Verbs: stdVerbs},
9898
},
@@ -262,3 +262,27 @@ func TestObserverRecoverFromDicoveryFailure(t *testing.T) {
262262
t.Errorf("%s failed: expected %v actual %v", "Recover from failure", expected, factory.names)
263263
}
264264
}
265+
266+
func TestExclusion(t *testing.T) {
267+
excluded := []string{"rs", "poD", "endpoints"} // short, singular, plural forms
268+
269+
if isExcluded(excluded,
270+
metav1.APIResource{Name: "Foos", Kind: "Foo", SingularName: "Foo", ShortNames: []string{}}) {
271+
t.Error("exclusions shouldn't filter more than specified")
272+
}
273+
274+
if !isExcluded(excluded,
275+
metav1.APIResource{Name: "Endpoints", Kind: "Endpoints", SingularName: "Endpoint", ShortNames: []string{"ep"}}) {
276+
t.Error("exclusions should work on plural resource names")
277+
}
278+
279+
if !isExcluded(excluded,
280+
metav1.APIResource{Name: "Pods", Kind: "Pod", SingularName: "Pod", ShortNames: []string{"po"}}) {
281+
t.Error("exclusions should ignore objects case")
282+
}
283+
284+
if !isExcluded(excluded,
285+
metav1.APIResource{Name: "Replicasets", Kind: "ReplicaSet", SingularName: "replicaset", ShortNames: []string{"rs"}}) {
286+
t.Error("exclusions should support resources shortnames")
287+
}
288+
}

0 commit comments

Comments
 (0)