forked from openshift/origin
/
bypriority.go
67 lines (55 loc) · 1.85 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
55
56
57
58
59
60
61
62
63
64
65
66
67
package sort
import (
securityv1 "github.com/openshift/api/security/v1"
securityapi "github.com/openshift/origin/pkg/security/apis/security"
securityapiv1 "github.com/openshift/origin/pkg/security/apis/security/v1"
)
// ByPriority is a helper to sort SCCs based on priority. If priorities are equal
// a string compare of the name is used.
type ByPriority []*securityv1.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 *securityv1.SecurityContextConstraints) int {
if scc.Priority == nil {
return 0
}
return int(*scc.Priority)
}
func ByPriorityConvert(toConvert []*securityapi.SecurityContextConstraints) (ByPriority, error) {
converted := []*securityv1.SecurityContextConstraints{}
for _, internal := range toConvert {
external := &securityv1.SecurityContextConstraints{}
if err := securityapiv1.Convert_security_SecurityContextConstraints_To_v1_SecurityContextConstraints(internal, external, nil); err != nil {
return nil, err
}
}
return converted, nil
}