-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
87 lines (70 loc) · 2.04 KB
/
helpers.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package api
import (
"fmt"
"sort"
"strings"
kutil "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
func ExpandResources(rawResources kutil.StringSet) kutil.StringSet {
ret := kutil.StringSet{}
toVisit := rawResources.List()
visited := kutil.StringSet{}
for i := 0; i < len(toVisit); i++ {
currResource := toVisit[i]
if visited.Has(currResource) {
continue
}
visited.Insert(currResource)
if strings.Index(currResource, ResourceGroupPrefix+":") != 0 {
ret.Insert(strings.ToLower(currResource))
continue
}
if resourceTypes, exists := GroupsToResources[currResource]; exists {
toVisit = append(toVisit, resourceTypes...)
}
}
return ret
}
func (r PolicyRule) String() string {
return fmt.Sprintf("PolicyRule{Verbs:%v, Resources:%v, ResourceNames:%v, Restrictions:%v}", r.Verbs.List(), r.Resources.List(), r.ResourceNames.List(), r.AttributeRestrictions)
}
func getRoleBindingValues(roleBindingMap map[string]RoleBinding) []RoleBinding {
ret := []RoleBinding{}
for _, currBinding := range roleBindingMap {
ret = append(ret, currBinding)
}
return ret
}
func SortRoleBindings(roleBindingMap map[string]RoleBinding, reverse bool) []RoleBinding {
roleBindings := getRoleBindingValues(roleBindingMap)
if reverse {
sort.Sort(sort.Reverse(RoleBindingSorter(roleBindings)))
} else {
sort.Sort(RoleBindingSorter(roleBindings))
}
return roleBindings
}
type PolicyBindingSorter []PolicyBinding
func (s PolicyBindingSorter) Len() int {
return len(s)
}
func (s PolicyBindingSorter) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
func (s PolicyBindingSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
type RoleBindingSorter []RoleBinding
func (s RoleBindingSorter) Len() int {
return len(s)
}
func (s RoleBindingSorter) Less(i, j int) bool {
return s[i].Name < s[j].Name
}
func (s RoleBindingSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func GetPolicyBindingName(policyRefNamespace string) string {
return fmt.Sprintf("%s:%s", policyRefNamespace, PolicyName)
}
var ClusterPolicyBindingName = GetPolicyBindingName("")