Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cmd/kubectl-tree/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

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

Expand Down
11 changes: 9 additions & 2 deletions cmd/kubectl-tree/rootcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
const (
allNamespacesFlag = "all-namespaces"
colorFlag = "color"
ignoreErrorsFlag = "ignore-errors"
)

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

ignoreErrors, err := command.Flags().GetBool(ignoreErrorsFlag)
if err != nil {
ignoreErrors = false
}

colorArg, err := command.Flags().GetString(colorFlag)
if err != nil {
return err
Expand Down Expand Up @@ -149,9 +155,9 @@ func run(command *cobra.Command, args []string) error {
klog.V(5).Infof("target parent object: %#v", obj)

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

Expand Down Expand Up @@ -180,6 +186,7 @@ func init() {

rootCmd.Flags().BoolP(allNamespacesFlag, "A", false, "query all objects in all API groups, both namespaced and non-namespaced")
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.")
rootCmd.Flags().Bool(ignoreErrorsFlag, false, "continue on errors while querying API resources")

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