Skip to content

Commit

Permalink
feat(rbac): user rolebinding support (#91)
Browse files Browse the repository at this point in the history
### Motivation


support user type rolebinding.
  • Loading branch information
mattisonchao authored Dec 17, 2024
1 parent 2dbae5c commit e73396c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
19 changes: 16 additions & 3 deletions cloud/data_source_rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,19 @@ func dataSourceRoleBinding() *schema.Resource {
Type: schema.TypeString,
},
},
"cel": {
Type: schema.TypeString,
"user_names": {
Type: schema.TypeList,
Computed: true,
Description: descriptions["rolebinding_cel"],
Description: descriptions["rolebinding_user_names"],
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"cel": {
Type: schema.TypeString,
Computed: true,
Description: descriptions["rolebinding_cel"],
},
},
}
}
Expand Down Expand Up @@ -98,16 +103,24 @@ func DataSourceRoleBindingRead(ctx context.Context, d *schema.ResourceData, meta
}

var serviceAccountNames []string
var userNames []string
for _, subject := range roleBinding.Spec.Subjects {
if subject.Kind == "ServiceAccount" {
serviceAccountNames = append(serviceAccountNames, subject.Name)
} else if subject.Kind == "User" {
userNames = append(userNames, subject.Name)
}
}
if serviceAccountNames != nil {
if err = d.Set("service_account_names", serviceAccountNames); err != nil {
return diag.FromErr(fmt.Errorf("ERROR_SET_SERVICE_ACCOUNT_NAMES: %w", err))
}
}
if userNames != nil {
if err = d.Set("user_names", userNames); err != nil {
return diag.FromErr(fmt.Errorf("ERROR_SET_USER_NAMES: %w", err))
}
}

if roleBinding.Spec.CEL != nil {
if err = d.Set("cel", roleBinding.Spec.CEL); err != nil {
Expand Down
1 change: 1 addition & 0 deletions cloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func init() {
"rolebinding_service_account_names": "The list of service accounts that are role binding names ",
"dns": "The DNS ID and name. Must specify together",
"rolebinding_cel": "The CEL(Common Expression Langauge) for conditional role binding",
"rolebinding_user_names": "The list of users that are role binding names ",
}
}

Expand Down
23 changes: 20 additions & 3 deletions cloud/resource_rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,19 @@ func resourceRoleBinding() *schema.Resource {
Type: schema.TypeString,
},
},
"cel": {
Type: schema.TypeString,
"user_names": {
Type: schema.TypeList,
Optional: true,
Description: descriptions["rolebinding_cel"],
Description: descriptions["rolebinding_user_names"],
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"cel": {
Type: schema.TypeString,
Optional: true,
Description: descriptions["rolebinding_cel"],
},
},
}
}
Expand All @@ -101,6 +106,7 @@ func resourceRoleBindingCreate(ctx context.Context, d *schema.ResourceData, m in

predefinedRoleName := d.Get("cluster_role_name").(string)
serviceAccountNames := d.Get("service_account_names").([]interface{})
userNames := d.Get("user_names").([]interface{})
cel := d.Get("cel").(string)

clientSet, err := getClientSet(getFactoryFromMeta(m))
Expand Down Expand Up @@ -137,6 +143,17 @@ func resourceRoleBindingCreate(ctx context.Context, d *schema.ResourceData, m in
})
}
}

if userNames != nil {
for _, userName := range userNames {
rb.Spec.Subjects = append(rb.Spec.Subjects, v1alpha1.Subject{
APIGroup: "cloud.streamnative.io",
Name: userName.(string),
Kind: "User",
})
}
}

if cel != "" {
rb.Spec.CEL = pointer.String(cel)
}
Expand Down

0 comments on commit e73396c

Please sign in to comment.