Skip to content

Commit 4cd544c

Browse files
committed
fix: do not fail if missing permissions
to some CRDs Continue and display the tree with the objects that we can access
1 parent 35cdeef commit 4cd544c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

cmd/kubectl-tree/query.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
// getAllResources finds all API objects in specified API resources in all namespaces (or non-namespaced).
18-
func getAllResources(client dynamic.Interface, apis []apiResource, allNs bool) ([]unstructured.Unstructured, error) {
18+
func getAllResources(client dynamic.Interface, apis []apiResource, allNs bool, ignoreErrors bool) ([]unstructured.Unstructured, error) {
1919
var mu sync.Mutex
2020
var wg sync.WaitGroup
2121
var out []unstructured.Unstructured
@@ -38,7 +38,9 @@ func getAllResources(client dynamic.Interface, apis []apiResource, allNs bool) (
3838
if errors.IsForbidden(err) {
3939
// should not fail the overall process, but print an info message indicating the permission issue
4040
klog.V(4).Infof("[query api] skipping forbidden resource: %s", a.GroupVersionResource())
41-
klog.Infof("cannot query %s (forbidden), omitting from the tree", a.GroupVersionResource().GroupResource())
41+
if !ignoreErrors {
42+
klog.Infof("cannot query %s (forbidden), omitting from the tree", a.GroupVersionResource().GroupResource())
43+
}
4244
} else {
4345
klog.V(4).Infof("[query api] error querying: %s, error=%v", a.GroupVersionResource(), err)
4446
errResult = stderrors.Join(errResult, fmt.Errorf("failed to query the %s resources: %w", a.GroupVersionResource(), err))
@@ -56,6 +58,10 @@ func getAllResources(client dynamic.Interface, apis []apiResource, allNs bool) (
5658
wg.Wait()
5759
klog.V(2).Infof("all goroutines have returned in %v", time.Since(start))
5860
klog.V(2).Infof("query result: error=%v, objects=%d", errResult, len(out))
61+
if ignoreErrors {
62+
klog.V(2).Infof("ignoring errors, returning all objects")
63+
return out, nil
64+
}
5965
return out, errResult
6066
}
6167

cmd/kubectl-tree/rootcmd.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
const (
3838
allNamespacesFlag = "all-namespaces"
3939
colorFlag = "color"
40+
ignoreErrorsFlag = "ignore-errors"
4041
)
4142

4243
var cf *genericclioptions.ConfigFlags
@@ -72,6 +73,11 @@ func run(command *cobra.Command, args []string) error {
7273
allNs = false
7374
}
7475

76+
ignoreErrors, err := command.Flags().GetBool(ignoreErrorsFlag)
77+
if err != nil {
78+
ignoreErrors = false
79+
}
80+
7581
colorArg, err := command.Flags().GetString(colorFlag)
7682
if err != nil {
7783
return err
@@ -149,9 +155,9 @@ func run(command *cobra.Command, args []string) error {
149155
klog.V(5).Infof("target parent object: %#v", obj)
150156

151157
klog.V(2).Infof("querying all api objects")
152-
apiObjects, err := getAllResources(dyn, apis.resources(), allNs)
158+
apiObjects, err := getAllResources(dyn, apis.resources(), allNs, ignoreErrors)
153159
if err != nil {
154-
return fmt.Errorf("error while querying api objects: %w", err)
160+
return fmt.Errorf("error while querying api objects, use --ignore-errors to continue: %w", err)
155161
}
156162
klog.V(2).Infof("found total %d api objects", len(apiObjects))
157163

@@ -180,6 +186,7 @@ func init() {
180186

181187
rootCmd.Flags().BoolP(allNamespacesFlag, "A", false, "query all objects in all API groups, both namespaced and non-namespaced")
182188
rootCmd.Flags().StringP(colorFlag, "c", "auto", "Enable or disable color output. This can be 'always', 'never', or 'auto' (default = use color only if using tty). The flag is overridden by the NO_COLOR env variable if set.")
189+
rootCmd.Flags().Bool(ignoreErrorsFlag, false, "continue on errors while querying API resources")
183190

184191
cf.AddFlags(rootCmd.Flags())
185192
if err := flag.Set("logtostderr", "true"); err != nil {

0 commit comments

Comments
 (0)