Skip to content

Commit

Permalink
Merge pull request #46 from mkgrei/add-cluster-only-flag
Browse files Browse the repository at this point in the history
Explicitly ignore namespaced Roles
  • Loading branch information
Ladicle authored Apr 18, 2024
2 parents 20357e6 + bfeaca2 commit 59f2455
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Options:
-n, --namespace string Change the namespace scope for this CLI request
-k, --subject-kind string Set SubjectKind to summarize (default: ServiceAccount)
-o, --options List of all options for this command
-c, --cluster-only Ingore namespaced Roles and show only ClusterRoles
--version Show version for this command

Use "kubectl rolesum --options" for a list of all options (applies to this command).
Expand Down
10 changes: 5 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Option struct {
SubjectKind string
SubjectName string
ShowOptionFlag bool
ClusterOnly bool

f cmdutil.Factory
}
Expand Down Expand Up @@ -77,6 +78,7 @@ func NewRolesumCmd() *cobra.Command {
opt.f = cmdutil.NewFactory(matchVersionFlags)

cmd.Flags().StringVarP(&opt.SubjectKind, "subject-kind", "k", subject.KindSA, "Set SubjectKind to summarize")
cmd.Flags().BoolVarP(&opt.ClusterOnly, "cluster-only", "c", false, "Ignore namespaced Roles")
cmd.Flags().BoolVarP(&opt.ShowOptionFlag, "options", "o", false, "List of all options for this command")

return cmd
Expand Down Expand Up @@ -110,15 +112,13 @@ func (o *Option) Run() error {
Name: o.SubjectName,
Kind: o.SubjectKind,
}
namespaced := false
if sub.Kind == subject.KindSA {
if !o.ClusterOnly {
k8sCfg := o.f.ToRawKubeConfigLoader()
ns, _, err := k8sCfg.Namespace()
if err != nil {
return err
}
sub.Namespace = ns
namespaced = true
}

client, err := o.f.KubernetesClientSet()
Expand All @@ -128,7 +128,7 @@ func (o *Option) Run() error {

exp := explorer.NewPolicyExplorer(client)
var nsp []*explorer.SubjectRole
if namespaced {
if !o.ClusterOnly {
nsp, err = exp.NamespacedSbjRoles(ctx, sub)
if err != nil {
return err
Expand Down Expand Up @@ -158,7 +158,7 @@ func (o *Option) Run() error {

pp.BlankLine()
pp.PrintHeader("Policies")
if namespaced {
if !o.ClusterOnly {
pp.PrintPolicies(nsp)
pp.BlankLine()
}
Expand Down
1 change: 1 addition & 0 deletions pkg/util/cmd/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "he
-n, --namespace string Change the namespace scope for this CLI request
-k, --subject-kind string Set SubjectKind to summarize (default: ServiceAccount)
-o, --options List of all options for this command
-c, --cluster-only Ingore namespaced Roles and show only ClusterRoles
--version Show version for this command
Use "kubectl rolesum --options" for a list of all options (applies to this command).
Expand Down
3 changes: 3 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,7 @@ echo; echo "Test..."
echo; echo "Test[Group]..."
./_output/kubectl-rolesum -k Group developer

echo; echo "Test[Group w/ ClusterOnly]..."
./_output/kubectl-rolesum -k Group developer -c

./clean.sh

0 comments on commit 59f2455

Please sign in to comment.