@@ -30,6 +30,10 @@ import (
3030 "k8s.io/klog"
3131)
3232
33+ const (
34+ allNamespacesFlag = "all-namespaces"
35+ )
36+
3337var cf * genericclioptions.ConfigFlags
3438
3539// This variable is populated by goreleaser
@@ -57,7 +61,13 @@ func versionString() string {
5761 return "v" + version
5862}
5963
60- func run (_ * cobra.Command , args []string ) error {
64+ func run (command * cobra.Command , args []string ) error {
65+
66+ allNs , err := command .Flags ().GetBool (allNamespacesFlag )
67+ if err != nil {
68+ allNs = false
69+ }
70+
6171 restConfig , err := cf .ToRESTConfig ()
6272 if err != nil {
6373 return err
@@ -102,15 +112,8 @@ func run(_ *cobra.Command, args []string) error {
102112 api = apiResults [0 ]
103113 }
104114
105- ns := * cf .Namespace
106- if ns == "" {
107- clientConfig := cf .ToRawKubeConfigLoader ()
108- defaultNamespace , _ , err := clientConfig .Namespace ()
109- if err != nil {
110- defaultNamespace = "default"
111- }
112- ns = defaultNamespace
113- }
115+ ns := getNamespace ()
116+ klog .V (2 ).Infof ("namespace=%s allNamespaces=%v" , ns , allNs )
114117
115118 obj , err := dyn .Resource (api .GroupVersionResource ()).Namespace (ns ).Get (name , metav1.GetOptions {})
116119 if err != nil {
@@ -120,7 +123,7 @@ func run(_ *cobra.Command, args []string) error {
120123 klog .V (5 ).Infof ("target parent object: %#v" , obj )
121124
122125 klog .V (2 ).Infof ("querying all api objects" )
123- apiObjects , err := getAllResources (dyn , apis .resources ())
126+ apiObjects , err := getAllResources (dyn , apis .resources (), allNs )
124127 if err != nil {
125128 return fmt .Errorf ("error while querying api objects: %w" , err )
126129 }
@@ -148,6 +151,9 @@ func init() {
148151 })
149152
150153 cf = genericclioptions .NewConfigFlags (true )
154+
155+ rootCmd .Flags ().BoolP (allNamespacesFlag , "A" , false , "query all objects in all API groups, both namespaced and non-namespaced" )
156+
151157 cf .AddFlags (rootCmd .Flags ())
152158 if err := flag .Set ("logtostderr" , "true" ); err != nil {
153159 fmt .Fprintf (os .Stderr , "failed to set logtostderr flag: %v\n " , err )
0 commit comments