...
1
16
17 package apiregistration
18
19 import (
20 "sort"
21
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 "k8s.io/apimachinery/pkg/version"
24 )
25
26
27
28
29
30 func SortedByGroupAndVersion(servers []*APIService) [][]*APIService {
31 serversByGroupPriorityMinimum := ByGroupPriorityMinimum(servers)
32 sort.Sort(serversByGroupPriorityMinimum)
33
34 ret := [][]*APIService{}
35 for _, curr := range serversByGroupPriorityMinimum {
36
37 existingIndex := -1
38 for j, groupInReturn := range ret {
39 if groupInReturn[0].Spec.Group == curr.Spec.Group {
40 existingIndex = j
41 break
42 }
43 }
44
45 if existingIndex >= 0 {
46 ret[existingIndex] = append(ret[existingIndex], curr)
47 sort.Sort(ByVersionPriority(ret[existingIndex]))
48 continue
49 }
50
51 ret = append(ret, []*APIService{curr})
52 }
53
54 return ret
55 }
56
57
58
59
60 type ByGroupPriorityMinimum []*APIService
61
62 func (s ByGroupPriorityMinimum) Len() int { return len(s) }
63 func (s ByGroupPriorityMinimum) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
64 func (s ByGroupPriorityMinimum) Less(i, j int) bool {
65 if s[i].Spec.GroupPriorityMinimum != s[j].Spec.GroupPriorityMinimum {
66 return s[i].Spec.GroupPriorityMinimum > s[j].Spec.GroupPriorityMinimum
67 }
68 return s[i].Name < s[j].Name
69 }
70
71
72
73
74 type ByVersionPriority []*APIService
75
76 func (s ByVersionPriority) Len() int { return len(s) }
77 func (s ByVersionPriority) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
78 func (s ByVersionPriority) Less(i, j int) bool {
79 if s[i].Spec.VersionPriority != s[j].Spec.VersionPriority {
80 return s[i].Spec.VersionPriority > s[j].Spec.VersionPriority
81 }
82 return version.CompareKubeAwareVersionStrings(s[i].Spec.Version, s[j].Spec.Version) > 0
83 }
84
85
86 func NewLocalAvailableAPIServiceCondition() APIServiceCondition {
87 return APIServiceCondition{
88 Type: Available,
89 Status: ConditionTrue,
90 LastTransitionTime: metav1.Now(),
91 Reason: "Local",
92 Message: "Local APIServices are always available",
93 }
94 }
95
96
97 func GetAPIServiceConditionByType(apiService *APIService, conditionType APIServiceConditionType) *APIServiceCondition {
98 for i := range apiService.Status.Conditions {
99 if apiService.Status.Conditions[i].Type == conditionType {
100 return &apiService.Status.Conditions[i]
101 }
102 }
103 return nil
104 }
105
106
107
108 func SetAPIServiceCondition(apiService *APIService, newCondition APIServiceCondition) {
109 existingCondition := GetAPIServiceConditionByType(apiService, newCondition.Type)
110 if existingCondition == nil {
111 apiService.Status.Conditions = append(apiService.Status.Conditions, newCondition)
112 return
113 }
114
115 if existingCondition.Status != newCondition.Status {
116 existingCondition.Status = newCondition.Status
117 existingCondition.LastTransitionTime = newCondition.LastTransitionTime
118 }
119
120 existingCondition.Reason = newCondition.Reason
121 existingCondition.Message = newCondition.Message
122 }
123
124
125 func IsAPIServiceConditionTrue(apiService *APIService, conditionType APIServiceConditionType) bool {
126 condition := GetAPIServiceConditionByType(apiService, conditionType)
127 return condition != nil && condition.Status == ConditionTrue
128 }
129
View as plain text