forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bypriority.go
54 lines (43 loc) · 1.25 KB
/
bypriority.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
package admission
import (
kapi "k8s.io/kubernetes/pkg/api"
)
// ByRestrictions is a helper to sort SCCs based on priority. If priorities are equal
// a string compare of the name is used.
type ByPriority []*kapi.SecurityContextConstraints
func (s ByPriority) Len() int {
return len(s)
}
func (s ByPriority) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByPriority) Less(i, j int) bool {
iSCC := s[i]
jSCC := s[j]
iSCCPriority := getPriority(iSCC)
jSCCPriority := getPriority(jSCC)
// a higher priority is considered "less" so that it moves to the front of the line
if iSCCPriority > jSCCPriority {
return true
}
if iSCCPriority < jSCCPriority {
return false
}
// priorities are equal, let's try point values
iRestrictionScore := pointValue(iSCC)
jRestrictionScore := pointValue(jSCC)
// a lower restriction score is considered "less" so that it moves to the front of the line
// (the greater the score, the more lax the SCC is)
if iRestrictionScore < jRestrictionScore {
return true
}
if iRestrictionScore > jRestrictionScore {
return false
}
// they are still equal, sort by name
return iSCC.Name < jSCC.Name
}
func getPriority(scc *kapi.SecurityContextConstraints) int {
if scc.Priority == nil {
return 0
}
return *scc.Priority
}