forked from operator-framework/operator-lifecycle-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.go
574 lines (491 loc) · 17.3 KB
/
resolver.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package resolver
import (
"context"
"encoding/json"
"fmt"
"sort"
"github.com/sirupsen/logrus"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
v1alpha1listers "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/solver"
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
)
type OperatorResolver interface {
SolveOperators(csvs []*v1alpha1.ClusterServiceVersion, subs []*v1alpha1.Subscription, add map[OperatorSourceInfo]struct{}) (OperatorSet, error)
}
type SatResolver struct {
cache OperatorCacheProvider
log logrus.FieldLogger
}
func NewDefaultSatResolver(rcp RegistryClientProvider, catsrcLister v1alpha1listers.CatalogSourceLister, log logrus.FieldLogger) *SatResolver {
return &SatResolver{
cache: NewOperatorCache(rcp, log, catsrcLister),
log: log,
}
}
type debugWriter struct {
logrus.FieldLogger
}
func (w *debugWriter) Write(b []byte) (int, error) {
n := len(b)
w.Debug(string(b))
return n, nil
}
func (r *SatResolver) SolveOperators(namespaces []string, csvs []*v1alpha1.ClusterServiceVersion, subs []*v1alpha1.Subscription) (OperatorSet, error) {
var errs []error
installables := make(map[solver.Identifier]solver.Installable, 0)
visited := make(map[OperatorSurface]*BundleInstallable, 0)
// TODO: better abstraction
startingCSVs := make(map[string]struct{})
// build a virtual catalog of all currently installed CSVs
existingSnapshot, existingInstallables, err := r.newSnapshotForNamespace(namespaces[0], subs, csvs)
if err != nil {
return nil, err
}
namespacedCache := r.cache.Namespaced(namespaces...).WithExistingOperators(existingSnapshot)
for _, i := range existingInstallables {
installables[i.Identifier()] = i
}
// build constraints for each Subscription
for _, sub := range subs {
pkg := sub.Spec.Package
catalog := registry.CatalogKey{
Name: sub.Spec.CatalogSource,
Namespace: sub.Spec.CatalogSourceNamespace,
}
predicates := []OperatorPredicate{WithPackage(pkg)}
if sub.Spec.Channel != "" {
predicates = append(predicates, WithChannel(sub.Spec.Channel))
}
// find the currently installed operator (if it exists)
var current *Operator
for _, csv := range csvs {
if csv.Name == sub.Status.InstalledCSV {
op, err := NewOperatorFromV1Alpha1CSV(csv)
if err != nil {
return nil, err
}
current = op
break
}
}
channelFilter := []OperatorPredicate{}
// if we found an existing installed operator, we should filter the channel by operators that can replace it
if current != nil {
channelFilter = append(channelFilter, Or(SkipRangeIncludes(*current.Version()), Replaces(current.Identifier())))
}
// if no operator is installed and we have a startingCSV, filter for it
if current == nil && len(sub.Spec.StartingCSV) > 0 {
channelFilter = append(channelFilter, WithCSVName(sub.Spec.StartingCSV))
startingCSVs[sub.Spec.StartingCSV] = struct{}{}
}
// find operators, in channel order, that can skip from the current version or list the current in "replaces"
subInstallables, err := r.getSubscriptionInstallables(pkg, current, catalog, predicates, channelFilter, namespacedCache, visited)
if err != nil {
errs = append(errs, err)
continue
}
for _, i := range subInstallables {
installables[i.Identifier()] = i
}
}
r.addInvariants(namespacedCache, installables)
input := make([]solver.Installable, 0)
for _, i := range installables {
input = append(input, i)
}
if len(errs) > 0 {
return nil, utilerrors.NewAggregate(errs)
}
s, err := solver.New(solver.WithInput(input), solver.WithTracer(solver.LoggingTracer{&debugWriter{r.log}}))
if err != nil {
return nil, err
}
solvedInstallables, err := s.Solve(context.TODO())
if err != nil {
return nil, err
}
// get the set of bundle installables from the result solved installables
operatorInstallables := make([]BundleInstallable, 0)
for _, installable := range solvedInstallables {
if bundleInstallable, ok := installable.(*BundleInstallable); ok {
operatorInstallables = append(operatorInstallables, *bundleInstallable)
}
}
operators := make(map[string]OperatorSurface, 0)
for _, installableOperator := range operatorInstallables {
csvName, channel, catalog, err := installableOperator.BundleSourceInfo()
if err != nil {
errs = append(errs, err)
continue
}
op, err := ExactlyOne(namespacedCache.Catalog(catalog).Find(WithCSVName(csvName), WithChannel(channel)))
if err != nil {
errs = append(errs, err)
continue
}
if len(installableOperator.Replaces) > 0 {
op.replaces = installableOperator.Replaces
}
// lookup if this installable came from a starting CSV
if _, ok := startingCSVs[csvName]; ok {
op.sourceInfo.StartingCSV = csvName
}
operators[csvName] = op
}
if len(errs) > 0 {
return nil, utilerrors.NewAggregate(errs)
}
return operators, nil
}
func (r *SatResolver) getSubscriptionInstallables(pkg string, current *Operator, catalog registry.CatalogKey, cachePredicates []OperatorPredicate, channelPredicates []OperatorPredicate, namespacedCache MultiCatalogOperatorFinder, visited map[OperatorSurface]*BundleInstallable) (map[solver.Identifier]solver.Installable, error) {
installables := make(map[solver.Identifier]solver.Installable, 0)
candidates := make([]*BundleInstallable, 0)
subInstallable := NewSubscriptionInstallable(pkg)
installables[subInstallable.Identifier()] = &subInstallable
bundles := namespacedCache.Catalog(catalog).Find(cachePredicates...)
// there are no options for this package, return early
if len(bundles) == 0 {
// should this condition fail resolution altogether?
return installables, nil
}
// bundles in the default channel appear first, then lexicographically order by channel name
sort.SliceStable(bundles, func(i, j int) bool {
var idef bool
if isrc := bundles[i].SourceInfo(); isrc != nil {
idef = isrc.DefaultChannel
}
var jdef bool
if jsrc := bundles[j].SourceInfo(); jsrc != nil {
jdef = jsrc.DefaultChannel
}
if idef == jdef {
return bundles[i].bundle.ChannelName < bundles[j].bundle.ChannelName
}
return idef
})
var sortedBundles []*Operator
lastChannel, lastIndex := "", 0
for i := 0; i <= len(bundles); i++ {
if i != len(bundles) && bundles[i].bundle.ChannelName == lastChannel {
continue
}
channel, err := r.sortChannel(bundles[lastIndex:i])
if err != nil {
return nil, err
}
sortedBundles = append(sortedBundles, channel...)
if i != len(bundles) {
lastChannel = bundles[i].bundle.ChannelName
lastIndex = i
}
}
for _, o := range Filter(sortedBundles, channelPredicates...) {
predicates := append(cachePredicates, WithCSVName(o.Identifier()))
id, installable, err := r.getBundleInstallables(catalog, predicates, namespacedCache, visited)
if err != nil {
return nil, err
}
if len(id) < 1 {
return nil, fmt.Errorf("could not find any potential bundles for subscription: %s", pkg)
}
for _, i := range installable {
if _, ok := id[i.Identifier()]; ok {
candidates = append(candidates, i)
}
installables[i.Identifier()] = i
}
}
depIds := make([]solver.Identifier, 0)
for _, c := range candidates {
// track which operator this is replacing, so that it can be realized when creating the resources on cluster
if current != nil {
c.Replaces = current.Identifier()
}
depIds = append(depIds, c.Identifier())
}
// all candidates added as options for this constraint
subInstallable.AddDependency(depIds)
return installables, nil
}
func (r *SatResolver) getBundleInstallables(catalog registry.CatalogKey, predicates []OperatorPredicate, namespacedCache MultiCatalogOperatorFinder, visited map[OperatorSurface]*BundleInstallable) (map[solver.Identifier]struct{}, map[solver.Identifier]*BundleInstallable, error) {
errs := make([]error, 0)
installables := make(map[solver.Identifier]*BundleInstallable, 0) // all installables, including dependencies
var finder OperatorFinder = namespacedCache
if !catalog.Empty() {
finder = namespacedCache.Catalog(catalog)
}
bundleStack := finder.Find(predicates...)
// track the first layer of installable ids
var initial = make(map[*Operator]struct{})
for _, o := range bundleStack {
initial[o] = struct{}{}
}
for {
if len(bundleStack) == 0 {
break
}
// pop from the stack
bundle := bundleStack[len(bundleStack)-1]
bundleStack = bundleStack[:len(bundleStack)-1]
bundleSource := bundle.SourceInfo()
if bundleSource == nil {
err := fmt.Errorf("unable to resolve the source of bundle %s, invalid cache", bundle.Identifier())
errs = append(errs, err)
continue
}
if b, ok := visited[bundle]; ok {
installables[b.identifier] = b
continue
}
bundleInstallable := NewBundleInstallable(bundle.Identifier(), bundle.Channel(), bundleSource.Catalog)
visited[bundle] = &bundleInstallable
dependencyPredicates, err := bundle.DependencyPredicates()
if err != nil {
errs = append(errs, err)
continue
}
for _, d := range dependencyPredicates {
// errors ignored; this will build an empty/unsatisfiable dependency if no candidates are found
candidateBundles, _ := AtLeast(1, namespacedCache.FindPreferred(&bundle.sourceInfo.Catalog, d))
sortedBundles, err := r.sortBundles(candidateBundles)
if err != nil {
errs = append(errs, err)
continue
}
bundleDependencies := make([]solver.Identifier, 0)
for _, dep := range sortedBundles {
found := namespacedCache.Catalog(dep.SourceInfo().Catalog).Find(WithCSVName(dep.Identifier()))
if len(found) == 0 {
err := fmt.Errorf("couldn't find %s in %s", bundle.Identifier(), dep.sourceInfo.Catalog)
errs = append(errs, err)
r.log.Warnf("cache consistency error: %s not found in %s", bundle.Identifier(), dep.sourceInfo.Catalog)
continue
}
b := found[0]
src := b.SourceInfo()
if src == nil {
err := fmt.Errorf("unable to resolve the source of bundle %s, invalid cache", bundle.Identifier())
errs = append(errs, err)
continue
}
i := NewBundleInstallable(b.Identifier(), b.Channel(), src.Catalog)
installables[i.Identifier()] = &i
bundleDependencies = append(bundleDependencies, i.Identifier())
bundleStack = append(bundleStack, b)
}
bundleInstallable.AddDependency(bundleDependencies)
}
installables[bundleInstallable.Identifier()] = &bundleInstallable
}
if len(errs) > 0 {
return nil, nil, utilerrors.NewAggregate(errs)
}
ids := make(map[solver.Identifier]struct{}, 0) // immediate installables found via predicates
for o := range initial {
ids[visited[o].Identifier()] = struct{}{}
}
return ids, installables, nil
}
func (r *SatResolver) newSnapshotForNamespace(namespace string, subs []*v1alpha1.Subscription, csvs []*v1alpha1.ClusterServiceVersion) (*CatalogSnapshot, []solver.Installable, error) {
installables := make([]solver.Installable, 0)
existingOperatorCatalog := registry.NewVirtualCatalogKey(namespace)
// build a catalog snapshot of CSVs without subscriptions
csvsWithSubscriptions := make(map[*v1alpha1.ClusterServiceVersion]struct{})
for _, sub := range subs {
for _, csv := range csvs {
if csv.Name == sub.Status.InstalledCSV {
csvsWithSubscriptions[csv] = struct{}{}
break
}
}
}
standaloneOperators := make([]*Operator, 0)
for _, csv := range csvs {
if _, ok := csvsWithSubscriptions[csv]; ok {
continue
}
op, err := NewOperatorFromV1Alpha1CSV(csv)
if err != nil {
return nil, nil, err
}
op.sourceInfo = &OperatorSourceInfo{
Catalog: existingOperatorCatalog,
}
standaloneOperators = append(standaloneOperators, op)
// all standalone operators are mandatory
i := NewBundleInstallable(op.Identifier(), "", existingOperatorCatalog, solver.Mandatory())
installables = append(installables, &i)
}
return NewRunningOperatorSnapshot(r.log, existingOperatorCatalog, standaloneOperators), installables, nil
}
func (r *SatResolver) addInvariants(namespacedCache MultiCatalogOperatorFinder, installables map[solver.Identifier]solver.Installable) {
// no two operators may provide the same GVK or Package in a namespace
conflictToInstallable := make(map[string][]solver.Installable)
for _, installable := range installables {
bundleInstallable, ok := installable.(*BundleInstallable)
if !ok {
continue
}
csvName, channel, catalog, err := bundleInstallable.BundleSourceInfo()
if err != nil {
continue
}
op, err := ExactlyOne(namespacedCache.Catalog(catalog).Find(WithCSVName(csvName), WithChannel(channel)))
if err != nil {
continue
}
// cannot provide the same GVK
for _, p := range op.Properties() {
if p.Type != opregistry.GVKType {
continue
}
var prop opregistry.GVKProperty
err := json.Unmarshal([]byte(p.Value), &prop)
if err != nil {
continue
}
key := fmt.Sprintf("gvkunique/%s/%s/%s", prop.Group, prop.Version, prop.Kind)
_, ok := conflictToInstallable[key]
if !ok {
conflictToInstallable[key] = make([]solver.Installable, 0)
}
conflictToInstallable[key] = append(conflictToInstallable[key], installable)
}
// cannot have the same package
for _, p := range op.Properties() {
if p.Type != opregistry.PackageType {
continue
}
var prop opregistry.PackageProperty
err := json.Unmarshal([]byte(p.Value), &prop)
if err != nil {
continue
}
key := fmt.Sprintf("pkgunique/%s", prop.PackageName)
_, ok := conflictToInstallable[key]
if !ok {
conflictToInstallable[key] = make([]solver.Installable, 0)
}
conflictToInstallable[key] = append(conflictToInstallable[key], installable)
}
}
for key, is := range conflictToInstallable {
if len(is) <= 1 {
continue
}
ids := make([]solver.Identifier, 0)
for _, d := range is {
ids = append(ids, d.Identifier())
}
s := NewSubscriptionInstallable(key)
s.constraints = append(s.constraints, solver.AtMost(1, ids...))
installables[s.Identifier()] = &s
}
}
func (r *SatResolver) sortBundles(bundles []*Operator) ([]*Operator, error) {
// assume bundles have been passed in sorted by catalog already
catalogOrder := make([]registry.CatalogKey, 0)
type PackageChannel struct {
Package, Channel string
DefaultChannel bool
}
// TODO: for now channels will be sorted lexicographically
channelOrder := make(map[registry.CatalogKey][]PackageChannel)
// partition by catalog -> channel -> bundle
partitionedBundles := map[registry.CatalogKey]map[PackageChannel][]*Operator{}
for _, b := range bundles {
pc := PackageChannel{
Package: b.Package(),
Channel: b.Channel(),
DefaultChannel: b.SourceInfo().DefaultChannel,
}
if _, ok := partitionedBundles[b.sourceInfo.Catalog]; !ok {
catalogOrder = append(catalogOrder, b.sourceInfo.Catalog)
partitionedBundles[b.sourceInfo.Catalog] = make(map[PackageChannel][]*Operator)
}
if _, ok := partitionedBundles[b.sourceInfo.Catalog][pc]; !ok {
channelOrder[b.sourceInfo.Catalog] = append(channelOrder[b.sourceInfo.Catalog], pc)
partitionedBundles[b.sourceInfo.Catalog][pc] = make([]*Operator, 0)
}
partitionedBundles[b.sourceInfo.Catalog][pc] = append(partitionedBundles[b.sourceInfo.Catalog][pc], b)
}
for catalog := range partitionedBundles {
sort.SliceStable(channelOrder[catalog], func(i, j int) bool {
pi, pj := channelOrder[catalog][i], channelOrder[catalog][j]
if pi.DefaultChannel != pj.DefaultChannel {
return pi.DefaultChannel
}
if pi.Package != pj.Package {
return pi.Package < pj.Package
}
return pi.Channel < pj.Channel
})
for channel := range partitionedBundles[catalog] {
sorted, err := r.sortChannel(partitionedBundles[catalog][channel])
if err != nil {
return nil, err
}
partitionedBundles[catalog][channel] = sorted
}
}
all := make([]*Operator, 0)
for _, catalog := range catalogOrder {
for _, channel := range channelOrder[catalog] {
all = append(all, partitionedBundles[catalog][channel]...)
}
}
return all, nil
}
// sorts bundle in a channel by replaces
func (r *SatResolver) sortChannel(bundles []*Operator) ([]*Operator, error) {
if len(bundles) <= 1 {
return bundles, nil
}
channel := []*Operator{}
bundleLookup := map[string]*Operator{}
// if a replaces b, then replacedBy[b] = a
replacedBy := map[*Operator]*Operator{}
replaces := map[*Operator]*Operator{}
for _, b := range bundles {
bundleLookup[b.Identifier()] = b
}
for _, b := range bundles {
if b.replaces != "" {
if r, ok := bundleLookup[b.replaces]; ok {
replacedBy[r] = b
replaces[b] = r
}
}
for _, skip := range b.skips {
if r, ok := bundleLookup[skip]; ok {
replacedBy[r] = b
}
}
}
// a bundle without a replacement is a channel head, but if we find more than one of those something is weird
headCandidates := []*Operator{}
for _, b := range bundles {
if _, ok := replacedBy[b]; !ok {
headCandidates = append(headCandidates, b)
}
}
if len(headCandidates) != 1 {
// TODO: more context in error
return nil, fmt.Errorf("found more than one head for channel")
}
head := headCandidates[0]
current := head
for {
channel = append(channel, current)
next, ok := replaces[current]
if !ok {
break
}
current = next
}
// TODO: do we care if the channel doesn't include every bundle in the input?
return channel, nil
}