Skip to content

Commit cc2de6f

Browse files
author
Luis Davim
committed
feat: resource filtering
Signed-off-by: Luis Davim <[email protected]>
1 parent d497da0 commit cc2de6f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ func main() {
4848
output := ""
4949
burst := 100
5050
qps := 25
51+
var groups []string
5152
pflag.StringVarP(&output, "output", "o", output, "Output format. May be '' or 'json'.")
5253
pflag.IntVar(&burst, "burst", burst, "API requests allowed per second (burst).")
5354
pflag.IntVar(&qps, "qps", qps, "API requests allowed per second (steady state). Set to -1 to disable rate limiter.")
55+
pflag.StringSliceVarP(&groups, "groups", "g", nil, "list of API groups")
5456

5557
// set up logging
5658
klog.InitFlags(nil)
@@ -104,6 +106,8 @@ func main() {
104106
Output: output,
105107
Stderr: os.Stderr,
106108
Stdout: os.Stdout,
109+
Namespace: *configFlags.Namespace,
110+
Groups: groups,
107111
}
108112
checkErr(opts.Validate())
109113
checkErr(opts.Run())

pkg/verify.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"io"
25+
"path/filepath"
2526
"sort"
2627
"strings"
2728

@@ -46,6 +47,8 @@ type VerifyGCOptions struct {
4647
Output string
4748
Stderr io.Writer
4849
Stdout io.Writer
50+
Namespace string
51+
Groups []string
4952
}
5053

5154
// Validate ensures the specified options are valid
@@ -106,6 +109,20 @@ func (v *VerifyGCOptions) Run() error {
106109
return err
107110
}
108111
gcResources := discovery.FilteredBy(discovery.SupportsAllVerbs{Verbs: []string{"list", "get", "delete"}}, preferredResources)
112+
if len(v.Groups) > 0 {
113+
gcResources = discovery.FilteredBy(discovery.ResourcePredicateFunc(func(groupVersion string, r *metav1.APIResource) bool {
114+
for _, g := range v.Groups {
115+
match, _ := filepath.Match(g, groupVersion)
116+
if match {
117+
return true
118+
}
119+
}
120+
if klog.V(2).Enabled() {
121+
fmt.Fprintf(v.Stderr, "skipping: %s\n", groupVersion)
122+
}
123+
return false
124+
}), gcResources)
125+
}
109126
gvrMap, err := discovery.GroupVersionResources(gcResources)
110127
if err != nil {
111128
return err
@@ -138,7 +155,7 @@ func (v *VerifyGCOptions) Run() error {
138155
fmt.Fprintf(v.Stderr, "fetching %v, %v\n", gvr.GroupVersion().String(), gvr.Resource)
139156
}
140157
pager.New(func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
141-
list, err := v.MetadataClient.Resource(gvr).List(ctx, opts)
158+
list, err := v.MetadataClient.Resource(gvr).Namespace(v.Namespace).List(ctx, opts)
142159
if err != nil {
143160
warningCount++
144161
fmt.Fprintf(v.Stderr, "warning: could not list %v: %v\n", gvr, err.Error())

0 commit comments

Comments
 (0)