Skip to content

Commit

Permalink
feat: Add --scopes/-S flag to specify additional ns to find relations…
Browse files Browse the repository at this point in the history
…hips

Signed-off-by: Justin Toh <tohjustin@hotmail.com>
  • Loading branch information
tohjustin committed Oct 7, 2021
1 parent dbb5006 commit 09bd3f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/cmd/lineage/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
}
}
4 changes: 4 additions & 0 deletions pkg/cmd/lineage/lineage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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})
Expand Down

0 comments on commit 09bd3f8

Please sign in to comment.