From 09bd3f8b3c4d019b52b3aa1572535a1c9d4f6041 Mon Sep 17 00:00:00 2001 From: Justin Toh Date: Thu, 7 Oct 2021 23:54:35 +0800 Subject: [PATCH] feat: Add --scopes/-S flag to specify additional ns to find relationships Signed-off-by: Justin Toh --- pkg/cmd/lineage/flags.go | 8 ++++++++ pkg/cmd/lineage/lineage.go | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/pkg/cmd/lineage/flags.go b/pkg/cmd/lineage/flags.go index f670c7b..b18e7ac 100644 --- a/pkg/cmd/lineage/flags.go +++ b/pkg/cmd/lineage/flags.go @@ -7,11 +7,14 @@ import ( const ( flagAllNamespaces = "all-namespaces" flagAllNamespacesShorthand = "A" + flagScopes = "scopes" + flagScopesShorthand = "S" ) // Flags composes common configuration flag structs used in the command. type Flags struct { AllNamespaces *bool + Scopes *[]string } // Copy returns a copy of Flags for mutation. @@ -26,14 +29,19 @@ func (f *Flags) AddFlags(flags *pflag.FlagSet) { if f.AllNamespaces != nil { flags.BoolVarP(f.AllNamespaces, flagAllNamespaces, flagAllNamespacesShorthand, *f.AllNamespaces, "If present, list object relationships across all namespaces.") } + if f.Scopes != nil { + flags.StringSliceVarP(f.Scopes, flagScopes, flagScopesShorthand, *f.Scopes, "Accepts a comma separated list of additional namespaces to find relationships. You can also use multiple flag options like -S namespace1 -S namespace2...") + } } // NewConfigFlags returns flags associated with command configuration, // with default values set. func NewFlags() *Flags { allNamespaces := false + scopes := []string{} return &Flags{ AllNamespaces: &allNamespaces, + Scopes: &scopes, } } diff --git a/pkg/cmd/lineage/lineage.go b/pkg/cmd/lineage/lineage.go index 95d8ff3..81f6d45 100644 --- a/pkg/cmd/lineage/lineage.go +++ b/pkg/cmd/lineage/lineage.go @@ -172,6 +172,7 @@ func (o *CmdOptions) Validate() error { klog.V(4).Infof("Namespace: %s", o.Namespace) klog.V(4).Infof("RequestObject: %v", o.RequestObject) klog.V(4).Infof("Flags.AllNamespaces: %t", *o.Flags.AllNamespaces) + klog.V(4).Infof("Flags.Scopes: %v", *o.Flags.Scopes) klog.V(4).Infof("ClientFlags.Context: %s", *o.ClientFlags.Context) klog.V(4).Infof("ClientFlags.Namespace: %s", *o.ClientFlags.Namespace) klog.V(4).Infof("PrintFlags.OutputFormat: %s", *o.PrintFlags.OutputFormat) @@ -200,6 +201,9 @@ func (o *CmdOptions) Run() error { if o.Flags.AllNamespaces != nil && *o.Flags.AllNamespaces { namespaces = append(namespaces, "") } + if o.Flags.Scopes != nil { + namespaces = append(namespaces, *o.Flags.Scopes...) + } // Fetch all resources in the cluster objects, err := o.KubeClient.List(ctx, client.ListOptions{Namespaces: namespaces})