forked from kubernetes-sigs/kueue
/
util.go
226 lines (197 loc) · 6.17 KB
/
util.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
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"context"
"errors"
"fmt"
"maps"
"slices"
"sync"
corev1 "k8s.io/api/core/v1"
schedulingv1 "k8s.io/api/scheduling/v1"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
utilslices "sigs.k8s.io/kueue/pkg/util/slices"
)
const (
ListLength = 100
)
var (
ErrNoMappingKey = errors.New("no mapping key found")
ErrNoMapping = errors.New("no mapping found")
ErrLQNotFound = errors.New("localqueue not found")
ErrCQNotFound = errors.New("clusterqueue not found")
ErrCQInvalid = errors.New("clusterqueue invalid")
)
func listOptions(namespace, queueLabel, continueToken string) []client.ListOption {
opts := []client.ListOption{
client.InNamespace(namespace),
client.HasLabels{queueLabel},
client.Limit(ListLength),
client.Continue(continueToken),
}
return opts
}
type ImportCache struct {
Namespaces []string
QueueLabel string
Mapping map[string]string
LocalQueues map[string]map[string]*kueue.LocalQueue
ClusterQueues map[string]*kueue.ClusterQueue
ResourceFalvors map[string]*kueue.ResourceFlavor
PriorityClasses map[string]*schedulingv1.PriorityClass
AddLabels map[string]string
}
func LoadImportCache(ctx context.Context, c client.Client, namespaces []string, queueLabel string, mapping, addLabels map[string]string) (*ImportCache, error) {
ret := ImportCache{
Namespaces: slices.Clone(namespaces),
QueueLabel: queueLabel,
Mapping: maps.Clone(mapping),
LocalQueues: make(map[string]map[string]*kueue.LocalQueue),
AddLabels: addLabels,
}
// get the cluster queues
cqList := &kueue.ClusterQueueList{}
if err := c.List(ctx, cqList); err != nil {
return nil, fmt.Errorf("loading cluster queues: %w", err)
}
ret.ClusterQueues = utilslices.ToRefMap(cqList.Items, func(cq *kueue.ClusterQueue) string { return cq.Name })
// get the local queues
for _, ns := range namespaces {
lqList := &kueue.LocalQueueList{}
if err := c.List(ctx, lqList); err != nil {
return nil, fmt.Errorf("loading local queues in namespace %s: %w", ns, err)
}
ret.LocalQueues[ns] = utilslices.ToRefMap(lqList.Items, func(lq *kueue.LocalQueue) string { return lq.Name })
}
// ResourceFlavors
rfList := &kueue.ResourceFlavorList{}
if err := c.List(ctx, rfList); err != nil {
return nil, fmt.Errorf("loading resource flavors: %w", err)
}
ret.ResourceFalvors = utilslices.ToRefMap(rfList.Items, func(rf *kueue.ResourceFlavor) string { return rf.Name })
// PriorityClasses
pcList := &schedulingv1.PriorityClassList{}
if err := c.List(ctx, pcList); err != nil {
return nil, fmt.Errorf("loading resource flavors: %w", err)
}
ret.PriorityClasses = utilslices.ToRefMap(pcList.Items, func(pc *schedulingv1.PriorityClass) string { return pc.Name })
return &ret, nil
}
func (mappingCache *ImportCache) LocalQueue(p *corev1.Pod) (*kueue.LocalQueue, error) {
mappingKey, found := p.Labels[mappingCache.QueueLabel]
if !found {
return nil, ErrNoMappingKey
}
queueName, found := mappingCache.Mapping[mappingKey]
if !found {
return nil, fmt.Errorf("%s: %w", mappingKey, ErrNoMapping)
}
nqQueues, found := mappingCache.LocalQueues[p.Namespace]
if !found {
return nil, fmt.Errorf("%s: %w", queueName, ErrLQNotFound)
}
lq, found := nqQueues[queueName]
if !found {
return nil, fmt.Errorf("%s: %w", queueName, ErrLQNotFound)
}
return lq, nil
}
func (mappingCache *ImportCache) ClusterQueue(p *corev1.Pod) (*kueue.ClusterQueue, error) {
lq, err := mappingCache.LocalQueue(p)
if err != nil {
return nil, err
}
queueName := string(lq.Spec.ClusterQueue)
cq, found := mappingCache.ClusterQueues[queueName]
if !found {
return nil, fmt.Errorf("cluster queue: %s: %w", queueName, ErrCQNotFound)
}
return cq, nil
}
func PushPods(ctx context.Context, c client.Client, namespaces []string, queueLabel string, ch chan<- corev1.Pod) error {
defer close(ch)
for _, ns := range namespaces {
lst := &corev1.PodList{}
log := ctrl.LoggerFrom(ctx).WithValues("namespace", ns)
log.V(3).Info("Begin pods list")
defer log.V(3).Info("End pods list")
page := 0
for {
err := c.List(ctx, lst, listOptions(ns, queueLabel, lst.Continue)...)
if err != nil {
log.Error(err, "list")
return fmt.Errorf("listing pods in %s, page %d: %w", ns, page, err)
}
for _, p := range lst.Items {
if p.Status.Phase == corev1.PodFailed || p.Status.Phase == corev1.PodSucceeded {
log.V(2).Info("Skip pod", "pod", klog.KObj(&p), "phase", p.Status.Phase)
} else {
ch <- p
}
}
page++
if lst.Continue == "" {
log.V(2).Info("No more pods", "pages", page)
break
}
}
}
return nil
}
type Result struct {
Pod string
Err error
}
type ProcessSummary struct {
TotalPods int
FailedPods int
ErrorsForPods map[string][]string
Errors []error
}
func ConcurrentProcessPod(ch <-chan corev1.Pod, jobs uint, f func(p *corev1.Pod) error) ProcessSummary {
wg := sync.WaitGroup{}
resultCh := make(chan Result)
wg.Add(int(jobs))
for i := 0; i < int(jobs); i++ {
go func() {
defer wg.Done()
for pod := range ch {
err := f(&pod)
resultCh <- Result{Pod: client.ObjectKeyFromObject(&pod).String(), Err: err}
}
}()
}
go func() {
wg.Wait()
close(resultCh)
}()
ps := ProcessSummary{
ErrorsForPods: make(map[string][]string),
}
for result := range resultCh {
ps.TotalPods++
if result.Err != nil {
ps.FailedPods++
estr := result.Err.Error()
if _, found := ps.ErrorsForPods[estr]; !found {
ps.Errors = append(ps.Errors, result.Err)
}
ps.ErrorsForPods[estr] = append(ps.ErrorsForPods[estr], result.Pod)
}
}
return ps
}