-
Notifications
You must be signed in to change notification settings - Fork 75
/
helpers.go
274 lines (229 loc) · 9.26 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package v1beta1
import (
"fmt"
"sort"
"strconv"
"sync"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
)
type PlacementDecisionGetter interface {
List(selector labels.Selector, namespace string) (ret []*PlacementDecision, err error)
}
// +k8s:deepcopy-gen=false
type PlacementDecisionClustersTracker struct {
placement *Placement
placementDecisionGetter PlacementDecisionGetter
existingScheduledClusterGroups ClusterGroupsMap
clusterGroupsIndexToName map[int32]string
clusterGroupsNameToIndex map[string][]int32
lock sync.RWMutex
}
// +k8s:deepcopy-gen=false
type GroupKey struct {
GroupName string `json:"groupName,omitempty"`
GroupIndex int32 `json:"groupIndex,omitempty"`
}
// NewPlacementDecisionClustersTracker initializes a PlacementDecisionClustersTracker
// using existing clusters. Clusters are added to the default cluster group with index 0.
// Set existingScheduledClusters to nil if there are no existing clusters.
func NewPlacementDecisionClustersTracker(placement *Placement, pdl PlacementDecisionGetter, existingScheduledClusters sets.Set[string]) *PlacementDecisionClustersTracker {
pdct := &PlacementDecisionClustersTracker{
placement: placement,
placementDecisionGetter: pdl,
existingScheduledClusterGroups: ClusterGroupsMap{{GroupIndex: 0}: existingScheduledClusters},
}
// Generate group name indices for the tracker.
pdct.generateGroupsNameIndex()
return pdct
}
// NewPlacementDecisionClustersTrackerWithGroups initializes a PlacementDecisionClustersTracker
// using existing cluster groups. Set existingScheduledClusterGroups to nil if no groups exist.
func NewPlacementDecisionClustersTrackerWithGroups(placement *Placement, pdl PlacementDecisionGetter, existingScheduledClusterGroups ClusterGroupsMap) *PlacementDecisionClustersTracker {
pdct := &PlacementDecisionClustersTracker{
placement: placement,
placementDecisionGetter: pdl,
existingScheduledClusterGroups: existingScheduledClusterGroups,
}
// Generate group name indices for the tracker.
pdct.generateGroupsNameIndex()
return pdct
}
// Refresh refreshes the tracker's decisionClusters.
func (pdct *PlacementDecisionClustersTracker) Refresh() error {
pdct.lock.Lock()
defer pdct.lock.Unlock()
if pdct.placement == nil || pdct.placementDecisionGetter == nil {
return nil
}
// Get the generated PlacementDecisions
decisionSelector := labels.SelectorFromSet(labels.Set{
PlacementLabel: pdct.placement.Name,
})
decisions, err := pdct.placementDecisionGetter.List(decisionSelector, pdct.placement.Namespace)
if err != nil {
return fmt.Errorf("failed to list PlacementDecisions: %w", err)
}
// Get the decision cluster names and groups
newScheduledClusterGroups := map[GroupKey]sets.Set[string]{}
for _, d := range decisions {
groupKey, err := parseGroupKeyFromDecision(d)
if err != nil {
return err
}
if _, exist := newScheduledClusterGroups[groupKey]; !exist {
newScheduledClusterGroups[groupKey] = sets.New[string]()
}
for _, sd := range d.Status.Decisions {
newScheduledClusterGroups[groupKey].Insert(sd.ClusterName)
}
}
// Update the existing decision cluster groups
pdct.existingScheduledClusterGroups = newScheduledClusterGroups
pdct.generateGroupsNameIndex()
return nil
}
// GetClusterChanges updates the tracker's decisionClusters and returns added and deleted cluster names.
func (pdct *PlacementDecisionClustersTracker) GetClusterChanges() (sets.Set[string], sets.Set[string], error) {
// Get existing clusters
existingScheduledClusters := pdct.existingScheduledClusterGroups.GetClusters()
// Refresh clusters
err := pdct.Refresh()
if err != nil {
return nil, nil, err
}
newScheduledClusters := pdct.existingScheduledClusterGroups.GetClusters()
// Compare the difference
added := newScheduledClusters.Difference(existingScheduledClusters)
deleted := existingScheduledClusters.Difference(newScheduledClusters)
return added, deleted, nil
}
func (pdct *PlacementDecisionClustersTracker) generateGroupsNameIndex() {
pdct.clusterGroupsIndexToName = map[int32]string{}
pdct.clusterGroupsNameToIndex = map[string][]int32{}
for groupkey := range pdct.existingScheduledClusterGroups {
// index to name
pdct.clusterGroupsIndexToName[groupkey.GroupIndex] = groupkey.GroupName
// name to index
if index, exist := pdct.clusterGroupsNameToIndex[groupkey.GroupName]; exist {
pdct.clusterGroupsNameToIndex[groupkey.GroupName] = append(index, groupkey.GroupIndex)
} else {
pdct.clusterGroupsNameToIndex[groupkey.GroupName] = []int32{groupkey.GroupIndex}
}
}
// sort index order
for _, index := range pdct.clusterGroupsNameToIndex {
sort.Slice(index, func(i, j int) bool {
return index[i] < index[j]
})
}
}
// ExistingClusterGroups returns the tracker's existing decision cluster groups for groups listed in groupKeys.
// Return empty set when groupKeys is empty.
func (pdct *PlacementDecisionClustersTracker) ExistingClusterGroups(groupKeys ...GroupKey) ClusterGroupsMap {
pdct.lock.RLock()
defer pdct.lock.RUnlock()
resultClusterGroups := make(map[GroupKey]sets.Set[string])
includeGroupKeys := pdct.fulfillGroupKeys(groupKeys)
for _, groupKey := range includeGroupKeys {
if clusters, found := pdct.existingScheduledClusterGroups[groupKey]; found {
resultClusterGroups[groupKey] = clusters
}
}
return resultClusterGroups
}
// ExistingClusterGroupsBesides returns the tracker's existing decision cluster groups except cluster groups listed in groupKeys.
// Return all the clusters when groupKeys is empty.
func (pdct *PlacementDecisionClustersTracker) ExistingClusterGroupsBesides(groupKeys ...GroupKey) ClusterGroupsMap {
pdct.lock.RLock()
defer pdct.lock.RUnlock()
resultClusterGroups := make(map[GroupKey]sets.Set[string])
excludeGroupKeys := pdct.fulfillGroupKeys(groupKeys)
includeGroupKeys := pdct.getGroupKeysBesides(excludeGroupKeys)
for _, groupKey := range includeGroupKeys {
if clusters, found := pdct.existingScheduledClusterGroups[groupKey]; found {
resultClusterGroups[groupKey] = clusters
}
}
return resultClusterGroups
}
// Fulfill the expect groupkeys with group name or group index, the returned groupkeys are ordered by input group name then group index.
// For example, the input is []GroupKey{{GroupName: "group1"}, {GroupIndex: 2}},
// the returned is []GroupKey{{GroupName: "group1", GroupIndex: 0}, {GroupName: "group1", GroupIndex: 1}, {GroupName: "group2", GroupIndex: 2}}
func (pdct *PlacementDecisionClustersTracker) fulfillGroupKeys(groupKeys []GroupKey) []GroupKey {
fulfilledGroupKeys := []GroupKey{}
for _, gk := range groupKeys {
if gk.GroupName != "" {
if indexes, exist := pdct.clusterGroupsNameToIndex[gk.GroupName]; exist {
for _, groupIndex := range indexes {
fulfilledGroupKeys = append(fulfilledGroupKeys, GroupKey{GroupName: gk.GroupName, GroupIndex: groupIndex})
}
}
} else {
if groupName, exist := pdct.clusterGroupsIndexToName[gk.GroupIndex]; exist {
fulfilledGroupKeys = append(fulfilledGroupKeys, GroupKey{GroupName: groupName, GroupIndex: gk.GroupIndex})
}
}
}
return fulfilledGroupKeys
}
func (pdct *PlacementDecisionClustersTracker) getGroupKeysBesides(groupKeyToExclude []GroupKey) []GroupKey {
groupKey := []GroupKey{}
for i := 0; i < len(pdct.clusterGroupsIndexToName); i++ {
gKey := GroupKey{GroupName: pdct.clusterGroupsIndexToName[int32(i)], GroupIndex: int32(i)}
if !containsGroupKey(groupKeyToExclude, gKey) {
groupKey = append(groupKey, gKey)
}
}
return groupKey
}
// ClusterGroupsMap is a custom type representing a map of group keys to sets of cluster names.
type ClusterGroupsMap map[GroupKey]sets.Set[string]
// GetOrderedGroupKeys returns an ordered slice of GroupKeys, sorted by group index.
func (g ClusterGroupsMap) GetOrderedGroupKeys() []GroupKey {
groupKeys := []GroupKey{}
for groupKey := range g {
groupKeys = append(groupKeys, groupKey)
}
// sort by group index index
sort.Slice(groupKeys, func(i, j int) bool {
return groupKeys[i].GroupIndex < groupKeys[j].GroupIndex
})
return groupKeys
}
// GetClusters returns a set containing all clusters from all group sets.
func (g ClusterGroupsMap) GetClusters() sets.Set[string] {
clusterSet := sets.New[string]()
for _, clusterGroup := range g {
clusterSet = clusterSet.Union(clusterGroup)
}
return clusterSet
}
// ClusterToGroupKey returns a mapping of cluster names to their respective group keys.
func (g ClusterGroupsMap) ClusterToGroupKey() map[string]GroupKey {
clusterToGroupKey := map[string]GroupKey{}
for groupKey, clusterGroup := range g {
for c := range clusterGroup {
clusterToGroupKey[c] = groupKey
}
}
return clusterToGroupKey
}
// Helper function to check if a groupKey is present in the groupKeys slice.
func containsGroupKey(groupKeys []GroupKey, groupKey GroupKey) bool {
for _, gk := range groupKeys {
if gk == groupKey {
return true
}
}
return false
}
func parseGroupKeyFromDecision(d *PlacementDecision) (GroupKey, error) {
groupName := d.Labels[DecisionGroupNameLabel]
groupIndex := d.Labels[DecisionGroupIndexLabel]
groupIndexNum, err := strconv.Atoi(groupIndex)
if err != nil {
return GroupKey{}, fmt.Errorf("incorrect group index: %w", err)
}
return GroupKey{GroupName: groupName, GroupIndex: int32(groupIndexNum)}, nil
}