Skip to content

Commit

Permalink
fix: definition how to work with role bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
leonsteinhaeuser committed Nov 15, 2023
1 parent 14cbd28 commit f58b7cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions internal/controller/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ const (
//
// Example:
//
// rules, err := ParseRoleBindingSubjects("kind=ServiceAccount,name=foo,namespace=bar;kind=ServiceAccount,name=foo2,namespace=bar2")
// rules, err := ParseRoleBindingSubjects("kind=ServiceAccount;name=foo;namespace=bar,kind=ServiceAccount;name=foo2;namespace=bar2")
// if err != nil {
// // handle error
// }
// fmt.Println(rules) // [{Kind:ServiceAccount Name:foo Namespace:bar} {Kind:ServiceAccount Name:foo2 Namespace:bar2}]
func ParseRoleBindingSubjects(rulesStr string) ([]rbacv1.Subject, error) {
var subjects []rbacv1.Subject
rules := strc.Properties(rulesStr)
rules := strc.Array(rulesStr)
for roleIndex, rule := range rules {
subject := rbacv1.Subject{}
parseByComma := strc.Array(rule)
for keyIndex, item := range parseByComma {
properties := strc.Properties(rule)
for keyIndex, item := range properties {
key, value, err := strc.KeyValue(item)
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestParseRoleBindingSubjects(t *testing.T) {
{
name: "simple",
args: args{
rulesStr: "kind=ServiceAccount,name=foo,namespace=bar",
rulesStr: "kind=ServiceAccount;name=foo;namespace=bar",
},
want: []rbacv1.Subject{
{
Expand All @@ -34,7 +34,7 @@ func TestParseRoleBindingSubjects(t *testing.T) {
{
name: "simple with apiGroup",
args: args{
rulesStr: "kind=Role,name=foo,namespace=bar,apiGroup=rbac.authorization.k8s.io",
rulesStr: "kind=Role;name=foo;namespace=bar;apiGroup=rbac.authorization.k8s.io",
},
want: []rbacv1.Subject{
{
Expand All @@ -49,7 +49,7 @@ func TestParseRoleBindingSubjects(t *testing.T) {
{
name: "multiple",
args: args{
rulesStr: "kind=ServiceAccount,name=foo,namespace=bar;kind=ServiceAccount,name=foo2,namespace=bar2",
rulesStr: "kind=ServiceAccount;name=foo;namespace=bar,kind=ServiceAccount;name=foo2;namespace=bar2",
},
want: []rbacv1.Subject{
{
Expand All @@ -68,7 +68,7 @@ func TestParseRoleBindingSubjects(t *testing.T) {
{
name: "multiple with apiGroup",
args: args{
rulesStr: "kind=Role,apiGroup=rbac.authorization.k8s.io,name=foo,namespace=bar;kind=ServiceAccount,name=foo2,namespace=bar2",
rulesStr: "kind=Role;apiGroup=rbac.authorization.k8s.io;name=foo;namespace=bar,kind=ServiceAccount;name=foo2;namespace=bar2",
},
want: []rbacv1.Subject{
{
Expand All @@ -88,15 +88,15 @@ func TestParseRoleBindingSubjects(t *testing.T) {
{
name: "invalid key",
args: args{
rulesStr: "kind=ServiceAccount,name=foo,namespace=bar;kind=ServiceAccount,name=foo2,namespace=bar2;kind=ServiceAccount,name=foo3,namespace=bar3,invalid=foo",
rulesStr: "kind=ServiceAccount;name=foo;namespace=bar,kind=ServiceAccount;name=foo2;namespace=bar2,kind=ServiceAccount;name=foo3;namespace=bar3;invalid=foo",
},
want: nil,
wantErr: true,
},
{
name: "invalid amount of entries",
args: args{
rulesStr: "kind=ServiceAccount=ServiceAccount,name=foo,namespace=bar;kind=ServiceAccount,name=foo2,namespace=bar2;kind=ServiceAccount,name=foo3,namespace=bar3,invalid=foo",
rulesStr: "kind=ServiceAccount=ServiceAccount;name=foo;namespace=bar,kind=ServiceAccount;name=foo2;namespace=bar2,kind=ServiceAccount;name=foo3;namespace=bar3;invalid=foo",
},
want: nil,
wantErr: true,
Expand Down

0 comments on commit f58b7cb

Please sign in to comment.