@@ -30,10 +30,10 @@ import (
3030 "k8s.io/cli-runtime/pkg/resource"
3131 "k8s.io/client-go/discovery"
3232 "k8s.io/client-go/dynamic"
33- "k8s.io/client-go/kubernetes/scheme"
3433 _ "k8s.io/client-go/plugin/pkg/client/auth" // combined authprovider import
3534 "k8s.io/client-go/rest"
3635 "k8s.io/klog"
36+ "k8s.io/utils/ptr"
3737)
3838
3939const (
@@ -124,17 +124,27 @@ func run(command *cobra.Command, args []string) error {
124124 klog .V (3 ).Info ("completed querying APIs list" )
125125
126126 // Use resource.Builder to resolve resource kind and name (kubectl-compatible)
127- builder := resource . NewBuilder ( cf )
128- namespace := ""
129- if cf . Namespace != nil {
130- namespace = * cf . Namespace
127+ clientCfg := cf . ToRawKubeConfigLoader ( )
128+ kubeconfigNamespace , _ , err := clientCfg . Namespace ()
129+ if err != nil {
130+ return fmt . Errorf ( "failed to determine namespace from kubeconfig: %w" , err )
131131 }
132132
133- result := builder .
134- WithScheme (scheme .Scheme , scheme .Scheme .PrioritizedVersionsAllGroups ()... ).
135- NamespaceParam (namespace ).
136- DefaultNamespace ().
133+ rb := resource .NewBuilder (cf )
134+
135+ namespace := ptr .Deref (cf .Namespace , "" )
136+ if namespace != "" {
137+ rb = rb .NamespaceParam (namespace )
138+ } else if kubeconfigNamespace != "" {
139+ rb = rb .NamespaceParam (kubeconfigNamespace )
140+ }
141+ result := rb .
142+ Unstructured ().
143+ AllNamespaces (allNs ).
137144 ResourceTypeOrNameArgs (true , args ... ).
145+ Latest ().
146+ Flatten ().
147+ ContinueOnError ().
138148 Do ()
139149
140150 infos , err := result .Infos ()
@@ -147,7 +157,6 @@ func run(command *cobra.Command, args []string) error {
147157 if len (infos ) > 1 {
148158 return fmt .Errorf ("multiple resources found, specify a single resource" )
149159 }
150-
151160 info := infos [0 ]
152161 gvr := info .Mapping .Resource
153162 name := info .Name
@@ -192,7 +201,7 @@ func run(command *cobra.Command, args []string) error {
192201 fmt .Println ("No resources are owned by this object through ownerReferences." )
193202 return nil
194203 }
195- treeView (os . Stderr , objs , * obj , conditionTypes )
204+ treeView (color . Output , objs , * obj , conditionTypes )
196205 klog .V (2 ).Infof ("done printing tree view" )
197206 return nil
198207}
0 commit comments