forked from kubernetes-sigs/kueue
/
import.go
217 lines (192 loc) · 6.37 KB
/
import.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
/*
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 pod
import (
"context"
"errors"
"fmt"
"maps"
"time"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
"sigs.k8s.io/kueue/cmd/importer/util"
"sigs.k8s.io/kueue/pkg/constants"
controllerconstants "sigs.k8s.io/kueue/pkg/controller/constants"
"sigs.k8s.io/kueue/pkg/controller/jobs/pod"
"sigs.k8s.io/kueue/pkg/workload"
)
func Import(ctx context.Context, c client.Client, cache *util.ImportCache, jobs uint) error {
ch := make(chan corev1.Pod)
go func() {
err := util.PushPods(ctx, c, cache.Namespaces, ch)
if err != nil {
ctrl.LoggerFrom(ctx).Error(err, "Listing pods")
}
}()
summary := util.ConcurrentProcessPod(ch, jobs, func(p *corev1.Pod) (bool, error) {
log := ctrl.LoggerFrom(ctx).WithValues("pod", klog.KObj(p))
log.V(3).Info("Importing")
lq, skip, err := cache.LocalQueue(p)
if skip || err != nil {
return skip, err
}
oldLq, found := p.Labels[controllerconstants.QueueLabel]
if !found {
if err := addLabels(ctx, c, p, lq.Name, cache.AddLabels); err != nil {
return false, fmt.Errorf("cannot add queue label: %w", err)
}
} else if oldLq != lq.Name {
return false, fmt.Errorf("another local queue name is set %q expecting %q", oldLq, lq.Name)
}
kp := pod.FromObject(p)
// Note: the recorder is not used for single pods, we can just pass nil for now.
wl, err := kp.ConstructComposableWorkload(ctx, c, nil)
if err != nil {
return false, fmt.Errorf("construct workload: %w", err)
}
maps.Copy(wl.Labels, cache.AddLabels)
if pc, found := cache.PriorityClasses[p.Spec.PriorityClassName]; found {
wl.Spec.PriorityClassName = pc.Name
wl.Spec.Priority = &pc.Value
wl.Spec.PriorityClassSource = constants.PodPriorityClassSource
}
if err := createWorkload(ctx, c, wl); err != nil {
return false, fmt.Errorf("creating workload: %w", err)
}
//make its admission and update its status
info := workload.NewInfo(wl)
cq := cache.ClusterQueues[string(lq.Spec.ClusterQueue)]
admission := kueue.Admission{
ClusterQueue: kueue.ClusterQueueReference(cq.Name),
PodSetAssignments: []kueue.PodSetAssignment{
{
Name: info.TotalRequests[0].Name,
Flavors: make(map[corev1.ResourceName]kueue.ResourceFlavorReference),
ResourceUsage: info.TotalRequests[0].Requests.ToResourceList(),
Count: ptr.To[int32](1),
},
},
}
flv := cq.Spec.ResourceGroups[0].Flavors[0].Name
for r := range info.TotalRequests[0].Requests {
admission.PodSetAssignments[0].Flavors[r] = flv
}
wl.Status.Admission = &admission
reservedCond := metav1.Condition{
Type: kueue.WorkloadQuotaReserved,
Status: metav1.ConditionTrue,
Reason: "Imported",
Message: fmt.Sprintf("Imported into ClusterQueue %s", cq.Name),
}
apimeta.SetStatusCondition(&wl.Status.Conditions, reservedCond)
admittedCond := metav1.Condition{
Type: kueue.WorkloadAdmitted,
Status: metav1.ConditionTrue,
Reason: "Imported",
Message: fmt.Sprintf("Imported into ClusterQueue %s", cq.Name),
}
apimeta.SetStatusCondition(&wl.Status.Conditions, admittedCond)
if err := admitWorkload(ctx, c, wl); err != nil {
return false, err
}
log.V(2).Info("Successfully imported", "pod", klog.KObj(p), "workload", klog.KObj(wl))
return false, nil
})
log := ctrl.LoggerFrom(ctx)
log.Info("Import done", "checked", summary.TotalPods, "skipped", summary.SkippedPods, "failed", summary.FailedPods)
for e, pods := range summary.ErrorsForPods {
log.Info("Import failed for Pods", "err", e, "occurrences", len(pods), "obsevedFirstIn", pods[0])
}
return errors.Join(summary.Errors...)
}
func checkError(err error) (retry, reload bool, timeout time.Duration) {
retry_seconds, retry := apierrors.SuggestsClientDelay(err)
if retry {
return true, false, time.Duration(retry_seconds) * time.Second
}
if apierrors.IsConflict(err) {
return true, true, 0
}
return false, false, 0
}
func addLabels(ctx context.Context, c client.Client, p *corev1.Pod, queue string, addLabels map[string]string) error {
p.Labels[controllerconstants.QueueLabel] = queue
p.Labels[pod.ManagedLabelKey] = pod.ManagedLabelValue
maps.Copy(p.Labels, addLabels)
err := c.Update(ctx, p)
retry, reload, timeout := checkError(err)
for retry {
if timeout >= 0 {
select {
case <-ctx.Done():
return errors.New("context canceled")
case <-time.After(timeout):
}
}
if reload {
err = c.Get(ctx, client.ObjectKeyFromObject(p), p)
if err != nil {
retry, reload, timeout = checkError(err)
continue
}
p.Labels[controllerconstants.QueueLabel] = queue
p.Labels[pod.ManagedLabelKey] = pod.ManagedLabelValue
maps.Copy(p.Labels, addLabels)
}
err = c.Update(ctx, p)
retry, reload, timeout = checkError(err)
}
return err
}
func createWorkload(ctx context.Context, c client.Client, wl *kueue.Workload) error {
err := c.Create(ctx, wl)
if apierrors.IsAlreadyExists(err) {
return nil
}
retry, _, timeout := checkError(err)
for retry {
if timeout >= 0 {
select {
case <-ctx.Done():
return errors.New("context canceled")
case <-time.After(timeout):
}
}
err = c.Create(ctx, wl)
retry, _, timeout = checkError(err)
}
return err
}
func admitWorkload(ctx context.Context, c client.Client, wl *kueue.Workload) error {
err := workload.ApplyAdmissionStatus(ctx, c, wl, false)
retry, _, timeout := checkError(err)
for retry {
if timeout >= 0 {
select {
case <-ctx.Done():
return errors.New("context canceled")
case <-time.After(timeout):
}
}
err = workload.ApplyAdmissionStatus(ctx, c, wl, false)
retry, _, timeout = checkError(err)
}
return err
}