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