Skip to content

Commit

Permalink
Simplify the RBAC subject patch by removing old subjects and then add…
Browse files Browse the repository at this point in the history
…ing new ones
  • Loading branch information
jingairpi committed Jan 3, 2025
1 parent 8aa8b4d commit 597c0e6
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 deletions kubernetes/structures_rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,36 +159,19 @@ func flattenClusterRoleAggregationRule(in *api.AggregationRule) []interface{} {

// Patch Ops
func patchRbacSubject(d *schema.ResourceData) PatchOperations {
o, n := d.GetChange("subject")
oldsubjects := expandRBACSubjects(o.([]interface{}))
newsubjects := expandRBACSubjects(n.([]interface{}))
ops := make([]PatchOperation, 0, len(newsubjects)+len(oldsubjects))
_, n := d.GetChange("subject")
newSubjects := expandRBACSubjects(n.([]interface{}))

common := len(newsubjects)
if common > len(oldsubjects) {
common = len(oldsubjects)
}
if len(oldsubjects) > len(newsubjects) {
for i := len(newsubjects); i < len(oldsubjects); i++ {
ops = append(ops, &RemoveOperation{
Path: "/subjects/" + strconv.Itoa(len(oldsubjects)-i),
})
}
}
for i, v := range newsubjects[:common] {
ops = append(ops, &ReplaceOperation{
Path: "/subjects/" + strconv.Itoa(i),
Value: v,
})
}
if len(newsubjects) > len(oldsubjects) {
for i, v := range newsubjects[common:] {
ops = append(ops, &AddOperation{
Path: "/subjects/" + strconv.Itoa(common+i),
Value: v,
})
}
ops := PatchOperations{
&RemoveOperation{
Path: "/subjects",
},
&AddOperation{
Path: "/subjects",
Value: newSubjects,
},
}

return ops
}

Expand Down

0 comments on commit 597c0e6

Please sign in to comment.