Skip to content

Commit e73396c

Browse files
authored
feat(rbac): user rolebinding support (#91)
### Motivation support user type rolebinding.
1 parent 2dbae5c commit e73396c

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

cloud/data_source_rolebinding.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,19 @@ func dataSourceRoleBinding() *schema.Resource {
5757
Type: schema.TypeString,
5858
},
5959
},
60-
"cel": {
61-
Type: schema.TypeString,
60+
"user_names": {
61+
Type: schema.TypeList,
6262
Computed: true,
63-
Description: descriptions["rolebinding_cel"],
63+
Description: descriptions["rolebinding_user_names"],
6464
Elem: &schema.Schema{
6565
Type: schema.TypeString,
6666
},
6767
},
68+
"cel": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
Description: descriptions["rolebinding_cel"],
72+
},
6873
},
6974
}
7075
}
@@ -98,16 +103,24 @@ func DataSourceRoleBindingRead(ctx context.Context, d *schema.ResourceData, meta
98103
}
99104

100105
var serviceAccountNames []string
106+
var userNames []string
101107
for _, subject := range roleBinding.Spec.Subjects {
102108
if subject.Kind == "ServiceAccount" {
103109
serviceAccountNames = append(serviceAccountNames, subject.Name)
110+
} else if subject.Kind == "User" {
111+
userNames = append(userNames, subject.Name)
104112
}
105113
}
106114
if serviceAccountNames != nil {
107115
if err = d.Set("service_account_names", serviceAccountNames); err != nil {
108116
return diag.FromErr(fmt.Errorf("ERROR_SET_SERVICE_ACCOUNT_NAMES: %w", err))
109117
}
110118
}
119+
if userNames != nil {
120+
if err = d.Set("user_names", userNames); err != nil {
121+
return diag.FromErr(fmt.Errorf("ERROR_SET_USER_NAMES: %w", err))
122+
}
123+
}
111124

112125
if roleBinding.Spec.CEL != nil {
113126
if err = d.Set("cel", roleBinding.Spec.CEL); err != nil {

cloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func init() {
173173
"rolebinding_service_account_names": "The list of service accounts that are role binding names ",
174174
"dns": "The DNS ID and name. Must specify together",
175175
"rolebinding_cel": "The CEL(Common Expression Langauge) for conditional role binding",
176+
"rolebinding_user_names": "The list of users that are role binding names ",
176177
}
177178
}
178179

cloud/resource_rolebinding.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,19 @@ func resourceRoleBinding() *schema.Resource {
8383
Type: schema.TypeString,
8484
},
8585
},
86-
"cel": {
87-
Type: schema.TypeString,
86+
"user_names": {
87+
Type: schema.TypeList,
8888
Optional: true,
89-
Description: descriptions["rolebinding_cel"],
89+
Description: descriptions["rolebinding_user_names"],
9090
Elem: &schema.Schema{
9191
Type: schema.TypeString,
9292
},
9393
},
94+
"cel": {
95+
Type: schema.TypeString,
96+
Optional: true,
97+
Description: descriptions["rolebinding_cel"],
98+
},
9499
},
95100
}
96101
}
@@ -101,6 +106,7 @@ func resourceRoleBindingCreate(ctx context.Context, d *schema.ResourceData, m in
101106

102107
predefinedRoleName := d.Get("cluster_role_name").(string)
103108
serviceAccountNames := d.Get("service_account_names").([]interface{})
109+
userNames := d.Get("user_names").([]interface{})
104110
cel := d.Get("cel").(string)
105111

106112
clientSet, err := getClientSet(getFactoryFromMeta(m))
@@ -137,6 +143,17 @@ func resourceRoleBindingCreate(ctx context.Context, d *schema.ResourceData, m in
137143
})
138144
}
139145
}
146+
147+
if userNames != nil {
148+
for _, userName := range userNames {
149+
rb.Spec.Subjects = append(rb.Spec.Subjects, v1alpha1.Subject{
150+
APIGroup: "cloud.streamnative.io",
151+
Name: userName.(string),
152+
Kind: "User",
153+
})
154+
}
155+
}
156+
140157
if cel != "" {
141158
rb.Spec.CEL = pointer.String(cel)
142159
}

0 commit comments

Comments
 (0)