-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathinfra.go
More file actions
359 lines (319 loc) · 10.8 KB
/
infra.go
File metadata and controls
359 lines (319 loc) · 10.8 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
package sandboxcr
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/openkruise/agents/pkg/sandbox-manager/clients"
"github.com/openkruise/agents/pkg/sandbox-manager/config"
"github.com/openkruise/agents/pkg/sandbox-manager/consts"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
k8sinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
k8scache "k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"github.com/openkruise/agents/api/v1alpha1"
informers "github.com/openkruise/agents/client/informers/externalversions"
"github.com/openkruise/agents/pkg/proxy"
"github.com/openkruise/agents/pkg/sandbox-manager/infra"
"github.com/openkruise/agents/pkg/sandbox-manager/logs"
managerutils "github.com/openkruise/agents/pkg/utils/sandbox-manager"
stateutils "github.com/openkruise/agents/pkg/utils/sandboxutils"
)
type Infra struct {
Cache *Cache
Client *clients.ClientSet
Proxy *proxy.Server
// For claiming sandbox
pickCache sync.Map
// Currently, templates stores the mapping of sandboxset name -> number of namespaces. For example,
// if a sandboxset with the same name is created in two different namespaces, the corresponding value would be 2.
// In the future, this will be changed to integrate with Template CR.
templates sync.Map
reconcileRouteStopCh chan struct{}
}
func NewInfra(client *clients.ClientSet, k8sClient kubernetes.Interface, proxy *proxy.Server, opts config.SandboxManagerOptions) (*Infra, error) {
// Create informer factory for custom Sandbox resources
informerFactory := informers.NewSharedInformerFactory(client.SandboxClient, time.Minute*10)
sandboxInformer := informerFactory.Api().V1alpha1().Sandboxes().Informer()
sandboxSetInformer := informerFactory.Api().V1alpha1().SandboxSets().Informer()
// Create informer factory for native Kubernetes resources (PersistentVolume)
coreInformerFactory := k8sinformers.NewSharedInformerFactory(k8sClient, time.Minute*10)
persistentVolumeInformer := coreInformerFactory.Core().V1().PersistentVolumes().Informer()
// Create informer factory with specified namespace for native Kubernetes resources (Secret)
coreInformerFactorySpecifiedNs := k8sinformers.NewSharedInformerFactoryWithOptions(k8sClient, time.Minute*10, k8sinformers.WithNamespace(opts.SystemNamespace))
// to generate informers only for the specified namespace to avoid potential security privilege escalation risks.
secretInformer := coreInformerFactorySpecifiedNs.Core().V1().Secrets().Informer()
// Initialize cache with all required informers
cache, err := NewCache(informerFactory, sandboxInformer, sandboxSetInformer, coreInformerFactorySpecifiedNs, secretInformer, coreInformerFactory, persistentVolumeInformer)
if err != nil {
return nil, err
}
instance := &Infra{
Cache: cache,
Client: client,
Proxy: proxy,
reconcileRouteStopCh: make(chan struct{}),
}
cache.AddSandboxEventHandler(k8scache.ResourceEventHandlerFuncs{
AddFunc: instance.onSandboxAdd,
DeleteFunc: instance.onSandboxDelete,
UpdateFunc: instance.onSandboxUpdate,
})
cache.AddSandboxSetEventHandler(k8scache.ResourceEventHandlerFuncs{
AddFunc: instance.onSandboxSetCreate,
DeleteFunc: instance.onSandboxSetDelete,
})
// Start route reconciler to handle missed delete events
go instance.startRouteReconciler(RouteReconcileInterval)
return instance, nil
}
func (i *Infra) Run(ctx context.Context) error {
return i.Cache.Run(ctx)
}
func (i *Infra) Stop() {
close(i.reconcileRouteStopCh)
i.Cache.Stop()
}
func (i *Infra) ClaimSandbox(ctx context.Context, opts infra.ClaimSandboxOptions) (infra.Sandbox, infra.ClaimMetrics, error) {
log := klog.FromContext(ctx)
metrics := infra.ClaimMetrics{}
opts, err := ValidateAndInitClaimOptions(opts)
if err != nil {
log.Error(err, "invalid claim options")
return nil, metrics, err
}
claimCtx, cancel := context.WithTimeout(ctx, opts.ClaimTimeout)
defer cancel()
// Start claiming sandbox
log.V(consts.DebugLogLevel).Info("claim sandbox options", "options", opts)
metrics.Retries = -1 // starts from 0
var claimedSandbox infra.Sandbox
err = retry.OnError(wait.Backoff{
Steps: int(opts.ClaimTimeout / RetryInterval),
Duration: RetryInterval,
Factor: LockBackoffFactor,
Jitter: LockJitter,
}, func(err error) bool {
return errors.As(err, &retriableError{})
}, func() error {
metrics.Retries++
log.Info("try to claim sandbox", "retries", metrics.Retries)
claimed, tryMetrics, claimErr := TryClaimSandbox(claimCtx, opts, &i.pickCache, i.Cache, i.Client)
metrics.Total += tryMetrics.Total
metrics.Wait += tryMetrics.Wait
metrics.PickAndLock += tryMetrics.PickAndLock
metrics.WaitReady += tryMetrics.WaitReady
metrics.InitRuntime += tryMetrics.InitRuntime
metrics.CSIMount += tryMetrics.CSIMount
metrics.LockType = tryMetrics.LockType
if tryMetrics.LastError != nil {
metrics.LastError = tryMetrics.LastError
}
if claimErr == nil {
claimedSandbox = claimed
} else {
metrics.RetryCost += tryMetrics.Total
}
return claimErr
})
return claimedSandbox, metrics, buildClaimError(err, metrics.LastError)
}
func buildClaimError(err error, lastError error) error {
if err == nil {
return nil
}
return fmt.Errorf("%v, last error: %v", err, lastError)
}
func (i *Infra) GetCache() infra.CacheProvider {
return i.Cache
}
func (i *Infra) HasTemplate(name string) bool {
_, exists := i.templates.Load(name)
return exists
}
func (i *Infra) SelectSandboxes(user string, limit int, filter func(sandbox infra.Sandbox) bool) ([]infra.Sandbox, error) {
objects, err := i.Cache.ListSandboxWithUser(user)
if err != nil {
return nil, err
}
var sandboxes []infra.Sandbox
for _, obj := range objects {
if !managerutils.ResourceVersionExpectationSatisfied(obj) {
continue
}
sbx := AsSandbox(obj, i.Cache, i.Client.SandboxClient)
if filter == nil || filter(sbx) {
sandboxes = append(sandboxes, sbx)
}
if len(sandboxes) >= limit {
break
}
}
return sandboxes, nil
}
func (i *Infra) GetClaimedSandbox(ctx context.Context, sandboxID string) (infra.Sandbox, error) {
sandbox, err := i.Cache.GetClaimedSandbox(sandboxID)
if err != nil {
return nil, err
}
if !managerutils.ResourceVersionExpectationSatisfied(sandbox) {
klog.FromContext(ctx).Info("resource version expectation not satisfied, will request APIServer directly")
sandbox, err = i.Client.ApiV1alpha1().Sandboxes(sandbox.Namespace).Get(ctx, sandbox.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
}
return AsSandbox(sandbox, i.Cache, i.Client.SandboxClient), nil
}
func (i *Infra) onSandboxAdd(obj any) {
sbx, ok := obj.(*v1alpha1.Sandbox)
if !ok {
return
}
if !i.HasTemplate(GetTemplateFromSandbox(sbx)) {
return
}
route := AsSandbox(sbx, i.Cache, i.Client.SandboxClient).GetRoute()
i.Proxy.SetRoute(logs.NewContext(), route)
managerutils.ResourceVersionExpectationObserve(sbx)
}
func (i *Infra) onSandboxDelete(obj any) {
sbx, ok := obj.(*v1alpha1.Sandbox)
if !ok {
return
}
sandboxID := stateutils.GetSandboxID(sbx)
i.Proxy.DeleteRoute(sandboxID)
klog.InfoS("sandbox route deleted", "sandboxID", sandboxID)
managerutils.ResourceVersionExpectationDelete(sbx)
}
func (i *Infra) onSandboxUpdate(_, newObj any) {
newSbx, ok := newObj.(*v1alpha1.Sandbox)
if !ok {
return
}
if !i.HasTemplate(GetTemplateFromSandbox(newSbx)) {
return
}
i.refreshRoute(AsSandbox(newSbx, i.Cache, i.Client.SandboxClient))
managerutils.ResourceVersionExpectationObserve(newSbx)
}
func (i *Infra) refreshRoute(sbx infra.Sandbox) {
oldRoute, _ := i.Proxy.LoadRoute(sbx.GetName())
newRoute := sbx.GetRoute()
if newRoute.State != oldRoute.State || newRoute.IP != oldRoute.IP {
i.Proxy.SetRoute(logs.NewContext(), newRoute)
}
}
func (i *Infra) onSandboxSetCreate(newObj interface{}) {
newSbs, ok := newObj.(*v1alpha1.SandboxSet)
if !ok {
return
}
ctx := logs.NewContext("sandboxset", klog.KObj(newSbs))
log := klog.FromContext(ctx)
log.Info("sandboxset creation watched")
for {
got, loaded := i.templates.LoadOrStore(newSbs.Name, int32(1))
if !loaded { // stored, the first one
log.Info("template created")
return
}
old := got.(int32)
if i.templates.CompareAndSwap(newSbs.Name, old, old+1) {
log.Info("template count updated", "cnt", old+1)
break
}
}
}
func (i *Infra) onSandboxSetDelete(obj interface{}) {
sbs, ok := obj.(*v1alpha1.SandboxSet)
if !ok {
return
}
ctx := logs.NewContext("sandboxset", klog.KObj(sbs))
log := klog.FromContext(ctx)
log.Info("sandboxset deletion watched")
for {
got, loaded := i.templates.Load(sbs.Name)
if !loaded { // not exist
log.Info("template does not exist")
return
}
if old := got.(int32); old == 1 { // the last one is deleted
if i.templates.CompareAndDelete(sbs.Name, old) {
log.Info("template deleted")
break
}
} else { // more than one exist
if i.templates.CompareAndSwap(sbs.Name, old, old-1) {
log.Info("template count decreased", "cnt", old-1)
break
}
}
}
}
func GetTemplateFromSandbox(sbx metav1.Object) string {
tmpl := sbx.GetLabels()[v1alpha1.LabelSandboxTemplate]
if tmpl == "" {
tmpl = sbx.GetLabels()[v1alpha1.LabelSandboxPool]
}
return tmpl
}
const (
// RouteReconcileInterval is the interval for route reconciliation
RouteReconcileInterval = 5 * time.Minute
)
// startRouteReconciler periodically reconciles routes to clean up orphaned entries
// that might be left due to missed delete events from Kubernetes informer
func (i *Infra) startRouteReconciler(interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
i.reconcileRoutes()
case <-i.reconcileRouteStopCh:
klog.Info("route reconciler stopped")
return
}
}
}
// reconcileRoutes compares routes in Proxy with Sandboxes in Cache
// and deletes orphaned routes that no longer have corresponding Sandboxes
func (i *Infra) reconcileRoutes() {
ctx := logs.NewContext()
log := klog.FromContext(ctx)
log.Info("starting route reconciliation")
// Build set of existing sandbox IDs from cache
existingSandboxIDs := make(map[string]struct{})
sandboxList := i.Cache.sandboxInformer.GetStore().List()
for _, obj := range sandboxList {
sbx, ok := obj.(*v1alpha1.Sandbox)
if !ok {
continue
}
sandboxID := stateutils.GetSandboxID(sbx)
existingSandboxIDs[sandboxID] = struct{}{}
}
// Check all routes and delete orphaned ones
routes := i.Proxy.ListRoutes()
deletedCount := 0
for _, route := range routes {
if _, exists := existingSandboxIDs[route.ID]; !exists {
i.Proxy.DeleteRoute(route.ID)
deletedCount++
managerutils.ResourceVersionExpectationDelete(&metav1.ObjectMeta{
UID: route.UID,
})
log.Info("reconciler deleted orphaned route", "sandboxID", route.ID)
}
}
if deletedCount > 0 {
log.Info("route reconciliation completed", "orphanedRoutesDeleted", deletedCount, "totalRoutes", len(routes))
}
}