Skip to content

Commit 63edf56

Browse files
committed
Remove empty role bindings when removing subjects
1 parent a208486 commit 63edf56

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pkg/oc/admin/policy/modify_roles.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,11 @@ func (o *RoleModificationOptions) RemoveRole() error {
551551
for _, roleBinding := range roleBindings {
552552
roleBinding.Subjects = removeSubjects(roleBinding.Subjects, subjectsToRemove)
553553

554-
err := o.RoleBindingAccessor.UpdateRoleBinding(roleBinding)
554+
if len(roleBinding.Subjects) > 0 {
555+
err = o.RoleBindingAccessor.UpdateRoleBinding(roleBinding)
556+
} else {
557+
err = o.RoleBindingAccessor.DeleteRoleBinding(roleBinding.Name)
558+
}
555559
if err != nil {
556560
return err
557561
}

pkg/oc/admin/policy/policy.go

+9
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ type RoleBindingAccessor interface {
121121
GetRoleBinding(name string) (*authorizationapi.RoleBinding, error)
122122
UpdateRoleBinding(binding *authorizationapi.RoleBinding) error
123123
CreateRoleBinding(binding *authorizationapi.RoleBinding) error
124+
DeleteRoleBinding(name string) error
124125
}
125126

126127
// LocalRoleBindingAccessor operates against role bindings in namespace
@@ -181,6 +182,10 @@ func (a LocalRoleBindingAccessor) CreateRoleBinding(binding *authorizationapi.Ro
181182
return err
182183
}
183184

185+
func (a LocalRoleBindingAccessor) DeleteRoleBinding(name string) error {
186+
return a.Client.RoleBindings(a.BindingNamespace).Delete(name, &metav1.DeleteOptions{})
187+
}
188+
184189
// ClusterRoleBindingAccessor operates against cluster scoped role bindings
185190
type ClusterRoleBindingAccessor struct {
186191
Client authorizationtypedclient.ClusterRoleBindingsGetter
@@ -249,3 +254,7 @@ func (a ClusterRoleBindingAccessor) CreateRoleBinding(binding *authorizationapi.
249254
_, err := a.Client.ClusterRoleBindings().Create(clusterBinding)
250255
return err
251256
}
257+
258+
func (a ClusterRoleBindingAccessor) DeleteRoleBinding(name string) error {
259+
return a.Client.ClusterRoleBindings().Delete(name, &metav1.DeleteOptions{})
260+
}

0 commit comments

Comments
 (0)