forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mason.go
488 lines (442 loc) · 13.6 KB
/
mason.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
Copyright 2017 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 mason
import (
"context"
"fmt"
"sync"
"time"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/boskos/common"
"k8s.io/test-infra/boskos/crds"
)
const (
// LeasedResources is a common.UserData entry
LeasedResources = "leasedResources"
)
// Masonable should be implemented by all configurations
type Masonable interface {
Construct(context.Context, common.Resource, common.TypeToResources) (*common.UserData, error)
}
// ConfigConverter converts a string into a Masonable
type ConfigConverter func(string) (Masonable, error)
type storageAccess interface {
// GetDynamicResourceLifeCycle gets an existing dynamic resource life cycle, errors otherwise
GetDynamicResourceLifeCycle(name string) (*crds.DRLCObject, error)
// GetDynamicResourceLifeCycles list all dynamic resource life cycle
GetDynamicResourceLifeCycles() (*crds.DRLCObjectList, error)
}
type boskosClient interface {
Acquire(rtype, state, dest string) (*common.Resource, error)
AcquireByState(state, dest string, names []string) ([]common.Resource, error)
ReleaseOne(name, dest string) error
UpdateOne(name, state string, userData *common.UserData) error
SyncAll() error
UpdateAll(dest string) error
ReleaseAll(dest string) error
}
// Mason uses config to convert dirty resources to usable one
type Mason struct {
client boskosClient
cleanerCount int
storage storageAccess
pending, fulfilled, cleaned chan requirements
boskosWaitPeriod, boskosSyncPeriod time.Duration
wg sync.WaitGroup
configConverters map[string]ConfigConverter
cancel context.CancelFunc
}
// requirements for a given resource
type requirements struct {
resource common.Resource
needs common.ResourceNeeds
fulfillment common.TypeToResources
}
func (r requirements) isFulFilled() bool {
for rType, count := range r.needs {
resources, ok := r.fulfillment[rType]
if !ok {
return false
}
if len(resources) != count {
return false
}
}
return true
}
// NewMason creates and initialized a new Mason object
// In: rtypes - A list of resource types to act on
// cleanerCount - Number of cleaning threads
// client - boskos client
// waitPeriod - time to wait before a new boskos operation (acquire mostly)
// syncPeriod - time to wait before syncing resource information to boskos
// Out: A Pointer to a Mason Object
func NewMason(cleanerCount int, client boskosClient, waitPeriod, syncPeriod time.Duration, s storageAccess) *Mason {
return &Mason{
client: client,
cleanerCount: cleanerCount,
storage: s,
pending: make(chan requirements),
cleaned: make(chan requirements, cleanerCount+1),
fulfilled: make(chan requirements, cleanerCount+1),
boskosWaitPeriod: waitPeriod,
boskosSyncPeriod: syncPeriod,
configConverters: map[string]ConfigConverter{},
}
}
// CheckUserData helps with extracting leased resource data from the resource.
func CheckUserData(res common.Resource) (common.LeasedResources, error) {
var leasedResources common.LeasedResources
if res.UserData == nil {
err := fmt.Errorf("user data is empty")
return nil, err
}
if err := res.UserData.Extract(LeasedResources, &leasedResources); err != nil {
return nil, err
}
return leasedResources, nil
}
// RegisterConfigConverter is used to register a new Masonable interface
// In: name - identifier for Masonable implementation
// fn - function that will parse the configuration string and return a Masonable interface
//
// Out: nil on success, error otherwise
func (m *Mason) RegisterConfigConverter(name string, fn ConfigConverter) error {
_, ok := m.configConverters[name]
if ok {
return fmt.Errorf("a converter for %s already exists", name)
}
m.configConverters[name] = fn
return nil
}
func (m *Mason) convertConfig(configEntry *common.DynamicResourceLifeCycle) (Masonable, error) {
fn, ok := m.configConverters[configEntry.Config.Type]
if !ok {
return nil, fmt.Errorf("config type %s is not supported", configEntry.Type)
}
return fn(configEntry.Config.Content)
}
func (m *Mason) garbageCollect(req requirements) {
names := []string{req.resource.Name}
for _, resources := range req.fulfillment {
for _, r := range resources {
names = append(names, r.Name)
}
}
for _, name := range names {
if err := m.client.ReleaseOne(name, common.Dirty); err != nil {
logrus.WithError(err).Errorf("Unable to release leased resource %s", name)
}
}
}
func (m *Mason) cleanAll(ctx context.Context) {
defer func() {
logrus.Info("Exiting cleanAll Thread")
m.wg.Done()
}()
for {
select {
case <-ctx.Done():
return
case req := <-m.fulfilled:
if err := m.cleanOne(ctx, &req.resource, req.fulfillment); err != nil {
logrus.WithError(err).Errorf("unable to clean resource %s", req.resource.Name)
m.garbageCollect(req)
} else {
m.cleaned <- req
}
}
}
}
func (m *Mason) cleanOne(ctx context.Context, res *common.Resource, leasedResources common.TypeToResources) error {
configEntry, err := m.storage.GetDynamicResourceLifeCycle(res.Type)
if err != nil {
logrus.WithError(err).Errorf("failed to get config for resource %s", res.Type)
return err
}
config, err := m.convertConfig(drlcPtr(configEntry.ToDynamicResourceLifeCycle()))
if err != nil {
logrus.WithError(err).Errorf("failed to convert config type %s - \n%s", configEntry.Spec.Config.Type, configEntry.Spec.Config.Content)
return err
}
errChan := make(chan error)
var userData *common.UserData
go func() {
var err error
userData, err = config.Construct(ctx, *res, leasedResources.Copy())
errChan <- err
}()
select {
case err = <-errChan:
if err != nil {
logrus.WithError(err).Errorf("failed to construct resource %s", res.Name)
return err
}
case <-ctx.Done():
return ctx.Err()
}
if err := m.client.UpdateOne(res.Name, res.State, userData); err != nil {
logrus.WithError(err).Error("unable to update user data")
return err
}
if res.UserData == nil {
res.UserData = userData
} else {
res.UserData.Update(userData)
}
logrus.Infof("Resource %s is cleaned", res.Name)
return nil
}
func (m *Mason) freeAll(ctx context.Context) {
defer func() {
logrus.Info("Exiting freeAll Thread")
m.wg.Done()
}()
for {
select {
case <-ctx.Done():
return
case req := <-m.cleaned:
if err := m.freeOne(&req.resource); err != nil {
logrus.WithError(err).Errorf("failed to free up resource %s", req.resource.Name)
m.garbageCollect(req)
}
}
}
}
func (m *Mason) freeOne(res *common.Resource) error {
leasedResources, err := CheckUserData(*res)
if err != nil {
return err
}
// TODO: Implement a ReleaseMultiple in a transaction to prevent orphans
// Finally return the resource as free
if err := m.client.ReleaseOne(res.Name, common.Free); err != nil {
logrus.WithError(err).Errorf("failed to release resource %s", res.Name)
return err
}
// And release leased resources as res.Name state
for _, name := range leasedResources {
if err := m.client.ReleaseOne(name, res.Name); err != nil {
logrus.WithError(err).Errorf("unable to release %s to state %s", name, res.Name)
return err
}
}
logrus.Infof("Resource %s has been freed", res.Name)
return nil
}
func (m *Mason) recycleAll(ctx context.Context) {
defer func() {
logrus.Info("Exiting recycleAll Thread")
m.wg.Done()
}()
tick := time.NewTicker(m.boskosWaitPeriod).C
for {
select {
case <-ctx.Done():
return
case <-tick:
configs, err := m.storage.GetDynamicResourceLifeCycles()
if err != nil {
logrus.WithError(err).Error("unable to get configuration")
continue
}
var configTypes []string
for _, c := range configs.Items {
_, isRegistered := m.configConverters[c.Spec.Config.Type]
if isRegistered {
configTypes = append(configTypes, c.GetName())
}
}
for _, r := range configTypes {
if res, err := m.client.Acquire(r, common.Dirty, common.Cleaning); err != nil {
logrus.WithError(err).Debug("boskos acquire failed!")
} else {
if req, err := m.recycleOne(res); err != nil {
logrus.WithError(err).Errorf("unable to recycle resource %s", res.Name)
if err := m.client.ReleaseOne(res.Name, common.Dirty); err != nil {
logrus.WithError(err).Errorf("Unable to release resources %s", res.Name)
}
} else {
m.pending <- *req
}
}
}
}
}
}
func (m *Mason) recycleOne(res *common.Resource) (*requirements, error) {
logrus.Infof("Resource %s is being recycled", res.Name)
configEntry, err := m.storage.GetDynamicResourceLifeCycle(res.Type)
if err != nil {
logrus.WithError(err).Errorf("could not get config for resource type %s", res.Type)
return nil, err
}
leasedResources, err := CheckUserData(*res)
if err != nil {
logrus.WithError(err).Warningf("failed to extract %s from resource user data", LeasedResources)
}
if leasedResources != nil {
resources, err := m.client.AcquireByState(res.Name, common.Leased, leasedResources)
if err != nil {
logrus.WithError(err).Warningf("could not acquire any leased resources for %s", res.Name)
}
for _, r := range resources {
if err := m.client.ReleaseOne(r.Name, common.Dirty); err != nil {
logrus.WithError(err).Warningf("could not release resource %s", r.Name)
}
}
// Deleting Leased Resources
res.UserData.Delete(LeasedResources)
if err := m.client.UpdateOne(res.Name, res.State, common.UserDataFromMap(map[string]string{LeasedResources: ""})); err != nil {
logrus.WithError(err).Errorf("could not update resource %s with freed leased resources", res.Name)
}
}
return &requirements{
fulfillment: common.TypeToResources{},
needs: configEntry.Spec.Needs,
resource: *res,
}, nil
}
func (m *Mason) syncAll(ctx context.Context) {
defer func() {
logrus.Info("Exiting UpdateAll Thread")
m.wg.Done()
}()
tick := time.NewTicker(m.boskosSyncPeriod).C
for {
select {
case <-ctx.Done():
return
case <-tick:
if err := m.client.SyncAll(); err != nil {
logrus.WithError(err).Errorf("failed to sync resources")
}
}
}
}
func (m *Mason) fulfillAll(ctx context.Context) {
defer func() {
logrus.Info("Exiting fulfillAll Thread")
m.wg.Done()
}()
for {
select {
case <-ctx.Done():
return
case req := <-m.pending:
if err := m.fulfillOne(ctx, &req); err != nil {
m.garbageCollect(req)
} else {
m.fulfilled <- req
}
}
}
}
func (m *Mason) fulfillOne(ctx context.Context, req *requirements) error {
// Making a copy
needs := common.ResourceNeeds{}
for k, v := range req.needs {
needs[k] = v
}
tick := time.NewTicker(m.boskosWaitPeriod).C
for rType := range needs {
for needs[rType] > 0 {
select {
case <-ctx.Done():
return ctx.Err()
case <-tick:
m.updateResources(req)
if res, err := m.client.Acquire(rType, common.Free, common.Leased); err != nil {
logrus.WithError(err).Debug("boskos acquire failed!")
} else {
req.fulfillment[rType] = append(req.fulfillment[rType], *res)
needs[rType]--
}
}
}
}
if req.isFulFilled() {
var leasedResources common.LeasedResources
for _, lr := range req.fulfillment {
for _, r := range lr {
leasedResources = append(leasedResources, r.Name)
}
}
userData := &common.UserData{}
if err := userData.Set(LeasedResources, &leasedResources); err != nil {
logrus.WithError(err).Errorf("failed to add %s user data", LeasedResources)
return err
}
if err := m.client.UpdateOne(req.resource.Name, req.resource.State, userData); err != nil {
logrus.WithError(err).Errorf("Unable to update resource %s", req.resource.Name)
return err
}
if req.resource.UserData == nil {
req.resource.UserData = userData
} else {
req.resource.UserData.Update(userData)
}
logrus.Infof("requirements for release %s is fulfilled", req.resource.Name)
return nil
}
return nil
}
func (m *Mason) updateResources(req *requirements) {
var resources []common.Resource
resources = append(resources, req.resource)
for _, leasedResources := range req.fulfillment {
resources = append(resources, leasedResources...)
}
for _, r := range resources {
if err := m.client.UpdateOne(r.Name, r.State, nil); err != nil {
logrus.WithError(err).Warningf("failed to update resource %s", r.Name)
}
}
}
func (m *Mason) start(ctx context.Context, fn func(context.Context)) {
go func() {
fn(ctx)
}()
m.wg.Add(1)
}
// Start Mason
func (m *Mason) Start() {
ctx, cancel := context.WithCancel(context.Background())
m.cancel = cancel
m.start(ctx, m.syncAll)
m.start(ctx, m.recycleAll)
m.start(ctx, m.fulfillAll)
for i := 0; i < m.cleanerCount; i++ {
m.start(ctx, m.cleanAll)
}
m.start(ctx, m.freeAll)
logrus.Info("Mason started")
}
// Stop Mason
func (m *Mason) Stop() {
logrus.Info("Stopping Mason")
m.cancel()
m.wg.Wait()
close(m.pending)
close(m.cleaned)
close(m.fulfilled)
m.client.ReleaseAll(common.Dirty)
logrus.Info("Mason stopped")
}
func resourcePtr(r common.Resource) *common.Resource {
return &r
}
func drlcPtr(drlc common.DynamicResourceLifeCycle) *common.DynamicResourceLifeCycle {
return &drlc
}