Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly ignore namespaced Roles #46

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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