@@ -16,11 +16,14 @@ limitations under the License.
1616package main
1717
1818import (
19+ "flag"
1920 "fmt"
2021 "github.com/spf13/cobra"
22+ "github.com/spf13/pflag"
2123 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2224 "k8s.io/cli-runtime/pkg/genericclioptions"
2325 "k8s.io/client-go/dynamic"
26+ "k8s.io/klog"
2427 "os"
2528 "strings"
2629
@@ -55,13 +58,17 @@ func run(_ *cobra.Command, args []string) error {
5558 if err != nil {
5659 return err
5760 }
58- apis , err := buildAPILookup (dc )
61+
62+ apis , err := findAPIs (dc )
5963 if err != nil {
6064 return err
6165 }
66+ klog .V (3 ).Info ("completed querying APIs list" )
6267
6368 kind , name := args [0 ], args [1 ]
69+ klog .V (3 ).Infof ("parsed kind=%v name=%v" , kind , name )
6470 apiRes := apis .lookup (kind )
71+ klog .V (5 ).Infof ("kind matches=%v" , apiRes )
6572 if len (apiRes ) == 0 {
6673 return fmt .Errorf ("could not find api kind %q" , kind )
6774 } else if len (apiRes ) > 1 {
@@ -82,25 +89,47 @@ func run(_ *cobra.Command, args []string) error {
8289 return fmt .Errorf ("failed to get %s/%s: %w" , kind , name , err )
8390 }
8491
92+ klog .V (5 ).Infof ("target parent object: %#v" , obj )
93+
94+ klog .V (2 ).Infof ("querying all api objects" )
8595 apiObjects , err := getAllResources (dyn , apis .resources ())
8696 if err != nil {
8797 return fmt .Errorf ("error while querying api objects: %w" , err )
8898 }
99+ klog .V (2 ).Infof ("found total %d api objects" , len (apiObjects ))
89100
90101 objs := newObjectDirectory (apiObjects )
91102 if len (objs .ownership [obj .GetUID ()]) == 0 {
92103 fmt .Println ("No resources are owned by the specified object through ownerReferences." )
93104 return nil
94105 }
95106 treeView (os .Stderr , objs , * obj )
107+ klog .V (2 ).Infof ("done printing tree view" )
96108 return nil
97109}
98110
99- func main () {
111+ func init () {
112+ klog .InitFlags (nil )
113+ pflag .CommandLine .AddGoFlagSet (flag .CommandLine )
114+
115+ // hide all glog flags except for -v
116+ flag .CommandLine .VisitAll (func (f * flag.Flag ) {
117+ if f .Name != "v" {
118+ pflag .Lookup (f .Name ).Hidden = true
119+ }
120+ })
121+
100122 cf = genericclioptions .NewConfigFlags (true )
101123 cf .AddFlags (rootCmd .Flags ())
102- if err := rootCmd .Execute (); err != nil {
124+ if err := flag .Set ("logtostderr" , "true" ); err != nil {
125+ fmt .Fprintf (os .Stderr , "failed to set logtostderr flag: %v\n " , err )
103126 os .Exit (1 )
104127 }
105128}
106129
130+ func main () {
131+ defer klog .Flush ()
132+ if err := rootCmd .Execute (); err != nil {
133+ os .Exit (1 )
134+ }
135+ }
0 commit comments