-
Notifications
You must be signed in to change notification settings - Fork 4
/
policy_internal_representation.go
56 lines (48 loc) · 1.26 KB
/
policy_internal_representation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package helpers
import (
v1 "k8s.io/api/rbac/v1"
)
type expandedPolicyKey struct {
APIGroup string
Resource string
ResourceNames string
NonResourceURLs string
}
type policyValue []string
// PolicyListIR is an internal representation of a list of PolicyRules which is easier to patch and manipulate
type policyListIR map[expandedPolicyKey]policyValue
func policyListToIR(input []v1.PolicyRule) policyListIR {
outputMap := make(policyListIR)
for _, rule := range input {
currentPolicyKey := expandedPolicyKey{
APIGroup: rule.APIGroups[0],
Resource: rule.Resources[0],
}
if _, ok := outputMap[currentPolicyKey]; ok {
outputMap[currentPolicyKey] = appendSet(outputMap[currentPolicyKey], rule.Verbs...)
} else {
outputMap[currentPolicyKey] = rule.Verbs
}
}
return outputMap
}
func irToPolicyList(input policyListIR) []v1.PolicyRule {
output := make([]v1.PolicyRule, 0, 100)
for key, verbs := range input {
output = append(output, v1.PolicyRule{
APIGroups: []string{key.APIGroup},
Resources: []string{key.Resource},
Verbs: verbs,
})
}
return output
}
func unionIRs(m1, m2 policyListIR) policyListIR {
for ia, va := range m1 {
if it, ok := m2[ia]; ok {
va = appendSet(va, it...)
}
m2[ia] = va
}
return m2
}