forked from openkruise/agents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandboxset_controller.go
More file actions
402 lines (371 loc) · 15.2 KB
/
sandboxset_controller.go
File metadata and controls
402 lines (371 loc) · 15.2 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
Copyright 2025.
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 sandboxset
import (
"context"
"errors"
"flag"
"fmt"
"math"
"reflect"
"time"
"github.com/google/uuid"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
intstrutil "k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
agentsv1alpha1 "github.com/openkruise/agents/api/v1alpha1"
"github.com/openkruise/agents/pkg/discovery"
"github.com/openkruise/agents/pkg/features"
"github.com/openkruise/agents/pkg/sandbox-manager/consts"
"github.com/openkruise/agents/pkg/utils"
"github.com/openkruise/agents/pkg/utils/expectations"
utilfeature "github.com/openkruise/agents/pkg/utils/feature"
"github.com/openkruise/agents/pkg/utils/fieldindex"
managerutils "github.com/openkruise/agents/pkg/utils/sandbox-manager"
stateutils "github.com/openkruise/agents/pkg/utils/sandboxutils"
)
func init() {
flag.IntVar(&concurrentReconciles, "sandboxset-workers", concurrentReconciles, "Max concurrent workers for SandboxSet controller.")
flag.IntVar(&initialBatchSize, "sandboxset-initial-batch-size", initialBatchSize, "The initial batch size to use for the api-server operation")
}
var (
concurrentReconciles = 3
initialBatchSize = 16
controllerKind = agentsv1alpha1.GroupVersion.WithKind("SandboxSet")
)
func Add(mgr manager.Manager) error {
if !utilfeature.DefaultFeatureGate.Enabled(features.SandboxSetGate) || !discovery.DiscoverGVK(controllerKind) {
return nil
}
err := (&Reconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr)
if err != nil {
return err
}
klog.Infof("Started SandboxSetReconciler successfully")
return nil
}
// Reconciler reconciles a Sandbox object
type Reconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
Codec runtime.Codec
}
const (
EventSandboxCreated = "SandboxCreated"
EventCreateSandboxFailed = "CreateSandboxFailed"
EventSandboxScaledDown = "SandboxScaledDown"
EventFailedSandboxDeleted = "FailedSandboxDeleted"
)
// +kubebuilder:rbac:groups=agents.kruise.io,resources=sandboxsets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=agents.kruise.io,resources=sandboxsets/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=agents.kruise.io,resources=sandboxsets/finalizers,verbs=update
// +kubebuilder:rbac:groups=core,resources=persistentvolumeclaims,verbs=get;list;watch
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
totalStart := time.Now()
log := logf.FromContext(ctx).WithValues("sandboxset", req.NamespacedName)
ctx = logf.IntoContext(ctx, log)
sbs := &agentsv1alpha1.SandboxSet{}
if err := r.Get(ctx, req.NamespacedName, sbs); err != nil {
if apierrors.IsNotFound(err) {
scaleUpExpectation.DeleteExpectations(req.String())
scaleDownExpectation.DeleteExpectations(req.String())
// Remove metrics when sandboxset is deleted
SandboxSetReplicas.DeleteLabelValues(req.Namespace, req.Name)
SandboxSetAvailableReplicas.DeleteLabelValues(req.Namespace, req.Name)
SandboxSetDesiredReplicas.DeleteLabelValues(req.Namespace, req.Name)
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
// Preparation
newStatus, err := r.initNewStatus(sbs)
if err != nil {
log.Error(err, "failed to init new status")
return ctrl.Result{}, err
}
controllerKey := GetControllerKey(sbs)
groups, err := r.groupAllSandboxes(ctx, sbs)
if err != nil {
log.Error(err, "failed to group sandboxes")
return ctrl.Result{}, err
}
var requeueAfter time.Duration
scaleUpSatisfied, dirtyScaleUp, scaleUpTimeoutAfter := scaleExpectationSatisfied(ctx, scaleUpExpectation, controllerKey)
scaleDownSatisfied, _, scaleDownTimeoutAfter := scaleExpectationSatisfied(ctx, scaleDownExpectation, controllerKey)
requeueAfter = min(scaleUpTimeoutAfter, scaleDownTimeoutAfter)
calculateSandboxSetStatusFromGroup(ctx, newStatus, groups, dirtyScaleUp)
// Set selector in status for scale subresource
if newStatus.Selector == "" {
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: map[string]string{
agentsv1alpha1.LabelSandboxPool: sbs.Name,
agentsv1alpha1.LabelSandboxIsClaimed: "false",
},
})
if err != nil {
log.Error(err, "failed to generate selector")
} else {
newStatus.Selector = selector.String()
}
}
var allErrors error
// Step 1: perform scale
start := time.Now()
delta := calculateScaleDelta(sbs, newStatus)
log.Info("performing scale", "expect", sbs.Spec.Replicas, "actual", newStatus.Replicas,
"available", newStatus.AvailableReplicas, "delta", delta)
if delta > 0 {
err = r.scaleUp(ctx, delta, sbs, newStatus.UpdateRevision)
} else if delta < 0 {
if !scaleUpSatisfied || !scaleDownSatisfied {
log.Info("skip scale down for scaleUpExpectation or scaleDownExpectation is not satisfied")
} else {
err = r.scaleDown(ctx, -delta, sbs, groups)
}
}
if err != nil {
log.Error(err, "failed to perform scale", "cost", time.Since(start))
allErrors = errors.Join(allErrors, err)
} else {
log.Info("scale finished", "cost", time.Since(start))
}
// Step 2: delete dead sandboxes
start = time.Now()
if err = r.deleteDeadSandboxes(ctx, groups.Dead); err != nil {
log.Error(err, "failed to perform garbage collection")
allErrors = errors.Join(allErrors, err)
} else {
log.Info("all dead sandboxes deleted", "cost", time.Since(start))
}
log.Info("reconcile done", "totalCost", time.Since(totalStart))
if err = r.updateSandboxSetStatus(ctx, *newStatus, sbs); err != nil {
log.Error(err, "failed to update sandboxset status")
allErrors = errors.Join(allErrors, err)
}
return ctrl.Result{RequeueAfter: requeueAfter}, allErrors
}
// scaleUp is allowed when scaleUpExpectation is satisfied
func (r *Reconciler) scaleUp(ctx context.Context, count int, sbs *agentsv1alpha1.SandboxSet, revision string) error {
log := logf.FromContext(ctx)
log.Info("scale up", "count", count)
successes, err := utils.DoItSlowly(count, initialBatchSize, func() error {
created, err := r.createSandbox(ctx, sbs, revision)
if err != nil {
log.Error(err, "failed to create sandbox")
return err
}
log.V(consts.DebugLogLevel).Info("sandbox created", "sandbox", klog.KObj(created))
return nil
})
log.Info("scale up finished", "successes", successes, "fails", count-successes)
return err
}
// scaleDown is allowed when both scaleUpExpectation and scaleDownExpectation are satisfied
func (r *Reconciler) scaleDown(ctx context.Context, count int, sbs *agentsv1alpha1.SandboxSet, groups GroupedSandboxes) error {
log := logf.FromContext(ctx)
controllerKey := GetControllerKey(sbs)
lock := uuid.New().String()
log.Info("scale down", "count", count)
var toDelete []client.ObjectKey
for _, snapshot := range append(groups.Creating, groups.Available...) {
if count <= 0 {
break
}
toDelete = append(toDelete, client.ObjectKeyFromObject(snapshot))
count--
}
successes, err := utils.DoItSlowlyWithInputs(toDelete, initialBatchSize, func(key client.ObjectKey) error {
scaleDownExpectation.ExpectScale(controllerKey, expectations.Delete, key.Name)
err := r.scaleDownSandbox(ctx, key, lock)
if err != nil {
log.Error(err, "failed to scale down sandbox")
scaleDownExpectation.ObserveScale(controllerKey, expectations.Delete, key.Name)
}
return err
})
log.Info("scale down finished", "success", successes, "fails", len(toDelete)-successes)
return err
}
// calculateScaleDelta calculates the delta for scaling, considering MaxUnavailable limit.
// Returns positive value for scale up, negative for scale down, 0 for no scaling needed.
func calculateScaleDelta(sbs *agentsv1alpha1.SandboxSet, newStatus *agentsv1alpha1.SandboxSetStatus) int {
delta := int(sbs.Spec.Replicas - newStatus.Replicas)
// scale down
if delta <= 0 {
return delta
}
// apply maxUnavailable limit only for scale up
scaleMaxUnavailable := math.MaxInt
if sbs.Spec.ScaleStrategy.MaxUnavailable != nil {
scaleMaxUnavailable, _ = intstrutil.GetScaledValueFromIntOrPercent(
intstrutil.ValueOrDefault(sbs.Spec.ScaleStrategy.MaxUnavailable, intstrutil.FromInt32(math.MaxInt32)),
int(sbs.Spec.Replicas),
true)
// subtract sandboxes that are currently being creating
scaleMaxUnavailable -= int(newStatus.Replicas - newStatus.AvailableReplicas)
}
// ignore negative values
if scaleMaxUnavailable < 0 {
scaleMaxUnavailable = 0
}
// delta cannot exceed scaleMaxUnavailable
if delta > scaleMaxUnavailable {
delta = scaleMaxUnavailable
}
return delta
}
func (r *Reconciler) createSandbox(ctx context.Context, sbs *agentsv1alpha1.SandboxSet, revision string) (*agentsv1alpha1.Sandbox, error) {
sbx := NewSandboxFromSandboxSet(sbs)
sbx.Labels[agentsv1alpha1.LabelTemplateHash] = revision
if err := ctrl.SetControllerReference(sbs, sbx, r.Scheme); err != nil {
return nil, err
}
if err := r.Create(ctx, sbx); err != nil {
r.Recorder.Eventf(sbs, corev1.EventTypeWarning, EventCreateSandboxFailed, "Failed to create sandbox: %s", err)
return nil, err
}
scaleUpExpectation.ExpectScale(GetControllerKey(sbs), expectations.Create, sbx.Name)
r.Recorder.Eventf(sbs, corev1.EventTypeNormal, EventSandboxCreated, "Sandbox %s created", klog.KObj(sbx))
return sbx, nil
}
func (r *Reconciler) scaleDownSandbox(ctx context.Context, key client.ObjectKey, lock string) (err error) {
log := logf.FromContext(ctx).WithValues("sandbox", key).V(consts.DebugLogLevel)
sbx := &agentsv1alpha1.Sandbox{}
log.Info("try to scale down sandbox")
if err = r.Get(ctx, key, sbx); err != nil {
return err
}
if sbx.Annotations[agentsv1alpha1.AnnotationLock] != "" && sbx.Annotations[agentsv1alpha1.AnnotationOwner] != consts.OwnerManagerScaleDown {
log.Info("sandbox to be scaled down claimed before performed, skip")
return errors.New("sandbox to be scaled down claimed before performed, skip")
}
managerutils.LockSandbox(sbx, lock, consts.OwnerManagerScaleDown)
if err = r.Update(ctx, sbx); err != nil {
return fmt.Errorf("failed to lock sandbox when scaling down: %s", err)
}
if err = r.Delete(ctx, sbx); err != nil {
log.Error(err, "failed to delete sandbox")
return err
}
log.Info("sandbox locked and deleted")
r.Recorder.Eventf(sbx, corev1.EventTypeNormal, EventSandboxScaledDown, "Sandbox %s locked and deleted", klog.KObj(sbx))
return nil
}
// deleteDeadSandboxes does not need to use ScaleExpectation, because this is a garbage collection logic that does not
// require maintaining replica counts (or rather, only needs to maintain the dead group's replica count at 0), so just
// delete all dead sandboxes.
func (r *Reconciler) deleteDeadSandboxes(ctx context.Context, dead []*agentsv1alpha1.Sandbox) error {
log := logf.FromContext(ctx).V(consts.DebugLogLevel)
failNum := 0
for _, sbx := range dead {
if sbx.DeletionTimestamp != nil {
continue
}
if err := r.Delete(ctx, sbx); err != nil {
log.Error(err, "failed to delete sandbox")
failNum++
}
log.Info("sandbox deleted", "sandbox", klog.KObj(sbx))
r.Recorder.Eventf(sbx, corev1.EventTypeNormal, EventFailedSandboxDeleted, "Sandbox %s deleted", klog.KObj(sbx))
}
if failNum > 0 {
return fmt.Errorf("failed to delete %d sandboxes", failNum)
}
return nil
}
func (r *Reconciler) updateSandboxSetStatus(ctx context.Context, newStatus agentsv1alpha1.SandboxSetStatus, sbs *agentsv1alpha1.SandboxSet) error {
log := logf.FromContext(ctx).V(consts.DebugLogLevel)
clone := sbs.DeepCopy()
if err := r.Get(ctx, client.ObjectKey{Namespace: sbs.Namespace, Name: sbs.Name}, clone); err != nil {
log.Error(err, "failed to get updated sandboxset from client")
return client.IgnoreNotFound(err)
}
if reflect.DeepEqual(clone.Status, newStatus) {
return nil
}
clone.Status = newStatus
err := r.Status().Update(ctx, clone)
if err == nil {
log.Info("update sandboxset status success", "status", utils.DumpJson(newStatus))
// Update metrics for availableReplicas and replicas
SandboxSetReplicas.WithLabelValues(sbs.Namespace, sbs.Name).Set(float64(newStatus.Replicas))
SandboxSetAvailableReplicas.WithLabelValues(sbs.Namespace, sbs.Name).Set(float64(newStatus.AvailableReplicas))
SandboxSetDesiredReplicas.WithLabelValues(sbs.Namespace, sbs.Name).Set(float64(sbs.Spec.Replicas))
} else {
log.Error(err, "update sandboxset status failed")
}
return err
}
func (r *Reconciler) groupAllSandboxes(ctx context.Context, sbs *agentsv1alpha1.SandboxSet) (GroupedSandboxes, error) {
log := logf.FromContext(ctx)
sandboxList := &agentsv1alpha1.SandboxList{}
if err := r.List(ctx, sandboxList,
client.InNamespace(sbs.Namespace),
client.MatchingFields{fieldindex.IndexNameForOwnerRefUID: string(sbs.UID)},
client.UnsafeDisableDeepCopy,
); err != nil {
return GroupedSandboxes{}, err
}
groups := GroupedSandboxes{}
for i := range sandboxList.Items {
sbx := &sandboxList.Items[i]
scaleUpExpectation.ObserveScale(GetControllerKey(sbs), expectations.Create, sbx.Name)
debugLog := log.V(consts.DebugLogLevel).WithValues("sandbox", sbx.Name)
state, reason := stateutils.GetSandboxState(sbx)
switch state {
case agentsv1alpha1.SandboxStateCreating:
groups.Creating = append(groups.Creating, sbx)
case agentsv1alpha1.SandboxStateAvailable:
groups.Available = append(groups.Available, sbx)
case agentsv1alpha1.SandboxStateRunning:
fallthrough
case agentsv1alpha1.SandboxStatePaused:
groups.Used = append(groups.Used, sbx)
case agentsv1alpha1.SandboxStateDead:
groups.Dead = append(groups.Dead, sbx)
default: // unknown, impossible, just in case
return GroupedSandboxes{}, fmt.Errorf("cannot find state for sandbox %s", sbx.Name)
}
debugLog.Info("sandbox is grouped", "state", state, "reason", reason)
}
log.Info("sandbox group done", "total", len(sandboxList.Items), "creating", len(groups.Creating),
"available", len(groups.Available), "used", len(groups.Used), "failed", len(groups.Dead))
return groups, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
controllerName := "sandboxset-controller"
r.Recorder = mgr.GetEventRecorderFor(controllerName)
r.Codec = serializer.NewCodecFactory(mgr.GetScheme()).LegacyCodec(agentsv1alpha1.SchemeGroupVersion)
return ctrl.NewControllerManagedBy(mgr).
Named(controllerName).
WithOptions(controller.Options{MaxConcurrentReconciles: concurrentReconciles}).
Watches(&agentsv1alpha1.SandboxSet{}, &handler.EnqueueRequestForObject{}).
Watches(&agentsv1alpha1.Sandbox{}, &SandboxEventHandler{}).
Complete(r)
}