/
dependencies.go
458 lines (391 loc) · 14 KB
/
dependencies.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
package porter
import (
"context"
"errors"
"fmt"
"strings"
"get.porter.sh/porter/pkg/cnab"
cnabprovider "get.porter.sh/porter/pkg/cnab/provider"
"get.porter.sh/porter/pkg/config"
"get.porter.sh/porter/pkg/manifest"
"get.porter.sh/porter/pkg/runtime"
"get.porter.sh/porter/pkg/storage"
"get.porter.sh/porter/pkg/tracing"
"github.com/hashicorp/go-multierror"
)
type dependencyExecutioner struct {
*config.Config
porter *Porter
Resolver BundleResolver
CNAB cnabprovider.CNABProvider
Installations storage.InstallationProvider
parentInstallation storage.Installation
parentAction BundleAction
parentOpts *BundleExecutionOptions
// These are populated by Prepare, call it or perish in inevitable errors
parentArgs cnabprovider.ActionArguments
deps []*queuedDependency
// this should maybe go somewhere else
depArgs cnabprovider.ActionArguments
}
func newDependencyExecutioner(p *Porter, installation storage.Installation, action BundleAction) *dependencyExecutioner {
resolver := BundleResolver{
Cache: p.Cache,
Registry: p.Registry,
}
return &dependencyExecutioner{
porter: p,
parentInstallation: installation,
parentAction: action,
parentOpts: action.GetOptions(),
Config: p.Config,
Resolver: resolver,
CNAB: p.CNAB,
Installations: p.Installations,
}
}
type queuedDependency struct {
cnab.DependencyLock
BundleReference cnab.BundleReference
Parameters map[string]string
// cache of the CNAB file contents
cnabFileContents []byte
}
func (e *dependencyExecutioner) Prepare(ctx context.Context) error {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
parentActionArgs, err := e.porter.BuildActionArgs(ctx, e.parentInstallation, e.parentAction)
if err != nil {
return err
}
e.parentArgs = parentActionArgs
err = e.identifyDependencies(ctx)
if err != nil {
return err
}
for _, dep := range e.deps {
err := e.prepareDependency(ctx, dep)
if err != nil {
return err
}
}
return nil
}
func (e *dependencyExecutioner) Execute(ctx context.Context) error {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
if e.deps == nil {
return span.Error(errors.New("Prepare must be called before Execute"))
}
// executeDependency the requested action against all the dependencies
for _, dep := range e.deps {
if !e.sharedActionResolver(ctx, dep) {
return nil
}
err := e.executeDependency(ctx, dep)
if err != nil {
return err
}
}
return nil
}
// PrepareRootActionArguments uses information about the dependencies of a bundle to prepare
// the execution of the root operation.
func (e *dependencyExecutioner) PrepareRootActionArguments(ctx context.Context) (cnabprovider.ActionArguments, error) {
args, err := e.porter.BuildActionArgs(ctx, e.parentInstallation, e.parentAction)
if err != nil {
return cnabprovider.ActionArguments{}, err
}
if args.Files == nil {
args.Files = make(map[string]string, 2*len(e.deps))
}
// Define files necessary for dependencies that need to be copied into the bundle
// args.Files is a map of target path to file contents
// This creates what goes in /cnab/app/dependencies/DEP.NAME
for _, dep := range e.deps {
// Copy the dependency bundle.json
e.checkSharedOutputs(ctx, dep)
target := runtime.GetDependencyDefinitionPath(dep.DependencyLock.Alias)
args.Files[target] = string(dep.cnabFileContents)
}
return args, nil
}
func (e *dependencyExecutioner) checkSharedOutputs(ctx context.Context, dep *queuedDependency) {
if !e.sharedActionResolver(ctx, dep) && e.parentAction.GetAction() == "install" {
e.getActionArgs(ctx, dep)
}
}
// sharedActionResolver tries to localize if v2, and shared deps
// then what actions should we take based off labels/action type/state
// true means continue, false means stop
func (e *dependencyExecutioner) sharedActionResolver(ctx context.Context, dep *queuedDependency) bool {
depInstallation, err := e.Installations.GetInstallation(ctx, e.parentOpts.Namespace, dep.Alias)
if err != nil {
if errors.Is(err, storage.ErrNotFound{}) {
return true
}
}
e.depArgs.Installation = depInstallation
//We're real, let's check if this is in the installation the parent
// is referencing
if dep.SharingGroup == depInstallation.Labels["sh.porter.SharingGroup"] {
if e.parentAction.GetAction() == "install" {
return false
}
if e.parentAction.GetAction() == "upgrade" {
return true
}
if e.parentAction.GetAction() == "uninstall" {
return false
}
}
return true
}
func (e *dependencyExecutioner) identifyDependencies(ctx context.Context) error {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
// Load parent CNAB bundle definition
var bun cnab.ExtendedBundle
if e.parentOpts.CNABFile != "" {
bundle, err := e.CNAB.LoadBundle(e.parentOpts.CNABFile)
if err != nil {
return span.Error(err)
}
bun = bundle
} else if e.parentOpts.Reference != "" {
cachedBundle, err := e.Resolver.Resolve(ctx, e.parentOpts.BundlePullOptions)
if err != nil {
return span.Error(fmt.Errorf("could not resolve bundle: %w", err))
}
bun = cachedBundle.Definition
} else if e.parentOpts.Name != "" {
c, err := e.Installations.GetLastRun(ctx, e.parentOpts.Namespace, e.parentOpts.Name)
if err != nil {
return err
}
bun = cnab.NewBundle(c.Bundle)
} else {
// If we hit here, there is a bug somewhere
return span.Error(errors.New("identifyDependencies failed to load the bundle because no bundle was specified. Please report this bug to https://github.com/getporter/porter/issues/new/choose"))
}
locks, err := bun.ResolveDependencies(bun)
if err != nil {
return span.Error(err)
}
e.deps = make([]*queuedDependency, len(locks))
for i, lock := range locks {
span.Debugf("Resolved dependency %s to %s", lock.Alias, lock.Reference)
e.deps[i] = &queuedDependency{
DependencyLock: lock,
}
}
return nil
}
func (e *dependencyExecutioner) prepareDependency(ctx context.Context, dep *queuedDependency) error {
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
// Pull the dependency
var err error
pullOpts := BundlePullOptions{
Reference: dep.Reference,
InsecureRegistry: e.parentOpts.InsecureRegistry,
Force: e.parentOpts.Force,
}
if err := pullOpts.Validate(); err != nil {
return span.Error(fmt.Errorf("error preparing dependency %s: %w", dep.Alias, err))
}
cachedDep, err := e.Resolver.Resolve(ctx, pullOpts)
if err != nil {
return span.Error(fmt.Errorf("error pulling dependency %s: %w", dep.Alias, err))
}
dep.BundleReference = cachedDep.BundleReference
strategy := e.GetSchemaCheckStrategy(ctx)
err = cachedDep.Definition.Validate(e.Context, strategy)
if err != nil {
return span.Error(fmt.Errorf("invalid bundle %s: %w", dep.Alias, err))
}
// Cache the bundle.json for later
dep.cnabFileContents, err = e.FileSystem.ReadFile(cachedDep.BundlePath)
if err != nil {
return span.Error(fmt.Errorf("error reading %s: %w", cachedDep.BundlePath, err))
}
// Make a lookup of which parameters are defined in the dependent bundle
depParams := map[string]struct{}{}
for paramName := range cachedDep.Definition.Parameters {
depParams[paramName] = struct{}{}
}
// Handle any parameter overrides for the dependency defined in porter.yaml
// dependencies:
// requires:
// - name: DEP
// parameters:
// PARAM: VALUE
// TODO: When we redo dependencies, we need to remove this dependency on the bundle being a porter bundle with a manifest
// Yes, right now the way this works means this feature is Porter only
m := &manifest.Manifest{}
if e.parentOpts.File != "" {
var err error
m, err = manifest.LoadManifestFrom(ctx, e.Config, e.parentOpts.File)
if err != nil {
return err
}
}
for _, manifestDep := range m.Dependencies.Requires {
if manifestDep.Name == dep.Alias {
for paramName, value := range manifestDep.Parameters {
// Make sure the parameter is defined in the bundle
if _, ok := depParams[paramName]; !ok {
return fmt.Errorf("invalid dependencies.%s.parameters entry, %s is not a parameter defined in that bundle", dep.Alias, paramName)
}
if dep.Parameters == nil {
dep.Parameters = make(map[string]string, 1)
}
dep.Parameters[paramName] = value
}
}
}
// Handle any parameter overrides for the dependency defined on the command line
// --param DEP#PARAM=VALUE
for key, value := range e.parentOpts.depParams {
parts := strings.Split(key, "#")
if len(parts) > 1 && parts[0] == dep.Alias {
paramName := parts[1]
// Make sure the parameter is defined in the bundle
if _, ok := depParams[paramName]; !ok {
return fmt.Errorf("invalid --param %s, %s is not a parameter defined in the bundle %s", key, paramName, dep.Alias)
}
if dep.Parameters == nil {
dep.Parameters = make(map[string]string, 1)
}
dep.Parameters[paramName] = value
delete(e.parentArgs.Params, key)
}
}
return nil
}
func (e *dependencyExecutioner) executeDependency(ctx context.Context, dep *queuedDependency) error {
// TODO(carolynvs): We should really switch up how the deperator works so that
// even the root bundle uses the execution engine here. This would set up how
// we want dependencies and mixins as bundles to work in the future.
ctx, span := tracing.StartSpan(ctx)
defer span.EndSpan()
if dep.SharingMode {
err := e.runDependencyv2(ctx, dep)
return err
}
eb := cnab.ExtendedBundle{}
//this expects depv1 style dependency to be installed as parentName+depName
depName := eb.BuildPrerequisiteInstallationName(e.parentOpts.Name, dep.Alias)
depInstallation, err := e.Installations.GetInstallation(ctx, e.parentOpts.Namespace, depName)
if err != nil {
if errors.Is(err, storage.ErrNotFound{}) {
depInstallation = storage.NewInstallation(e.parentOpts.Namespace, depName)
depInstallation.SetLabel("sh.porter.parentInstallation", e.parentArgs.Installation.String())
// For now, assume it's okay to give the dependency the same credentials as the parent
depInstallation.CredentialSets = e.parentInstallation.CredentialSets
if err = e.Installations.InsertInstallation(ctx, depInstallation); err != nil {
return err
}
} else {
return err
}
}
e.depArgs.Installation = depInstallation
if err = e.getActionArgs(ctx, dep); err != nil {
return err
}
if err = e.finalizeExecute(ctx, dep); err != nil {
return err
}
return nil
}
// runDependencyv2 will see if the child dependency is already installed
// and if so, use sharingmode && group to resolve what to do
func (e *dependencyExecutioner) runDependencyv2(ctx context.Context, dep *queuedDependency) error {
depInstallation, err := e.Installations.GetInstallation(ctx, e.parentOpts.Namespace, dep.Alias)
if err != nil {
if errors.Is(err, storage.ErrNotFound{}) {
depInstallation = storage.NewInstallation(e.parentOpts.Namespace, dep.Alias)
depInstallation.SetLabel("sh.porter.parentInstallation", e.parentArgs.Installation.String())
depInstallation.SetLabel("sh.porter.SharingGroup", dep.SharingGroup)
// For now, assume it's okay to give the dependency the same credentials as the parent
depInstallation.CredentialSets = e.parentInstallation.CredentialSets
if err = e.Installations.InsertInstallation(ctx, depInstallation); err != nil {
return err
}
return err
}
}
//We save the installation
e.depArgs.Installation = depInstallation
// Installed: Return
// Uninstalled: Error (delete or else)
// Upgrade: Unsupported
// Invoke: At your own risk
//todo(schristoff): this is kind of icky, can be it less so?
if dep.SharingGroup == depInstallation.Labels["sh.porter.SharingGroup"] {
if depInstallation.IsInstalled() {
action := e.parentAction.GetAction()
if action == "upgrade" || action == "uninstall" {
return nil
}
}
if depInstallation.Uninstalled {
return fmt.Errorf("error executing dependency, dependency must be in installed status or deleted, %s is in status %s", dep.Alias, depInstallation.Status)
}
}
if err = e.getActionArgs(ctx, dep); err != nil {
return err
}
if err = e.finalizeExecute(ctx, dep); err != nil {
return err
}
return nil
}
func (e *dependencyExecutioner) getActionArgs(ctx context.Context,
dep *queuedDependency) error {
finalParams, err := e.porter.finalizeParameters(ctx, e.depArgs.Installation, dep.BundleReference.Definition, e.parentArgs.Action, dep.Parameters)
if err != nil {
return fmt.Errorf("error resolving parameters for dependency %s: %w", dep.Alias, err)
}
e.depArgs = cnabprovider.ActionArguments{
BundleReference: dep.BundleReference,
Action: e.parentArgs.Action,
Installation: e.depArgs.Installation,
Driver: e.parentArgs.Driver,
AllowDockerHostAccess: e.parentOpts.AllowDockerHostAccess,
Params: finalParams,
PersistLogs: e.parentArgs.PersistLogs,
}
return nil
}
// finalizeExecute handles some Uninstall logic that is carried out
// right before calling CNAB execute.
func (e *dependencyExecutioner) finalizeExecute(ctx context.Context, dep *queuedDependency) error {
ctx, span := tracing.StartSpan(ctx)
// Determine if we're working with UninstallOptions, to inform deletion and
// error handling, etc.
var uninstallOpts UninstallOptions
if opts, ok := e.parentAction.(UninstallOptions); ok {
uninstallOpts = opts
}
var executeErrs error
span.Infof("Executing dependency %s...", dep.Alias)
err := e.CNAB.Execute(ctx, e.depArgs)
if err != nil {
executeErrs = multierror.Append(executeErrs, fmt.Errorf("error executing dependency %s: %w", dep.Alias, err))
// Handle errors when/if the action is uninstall
// If uninstallOpts is an empty struct, executeErrs will pass through
executeErrs = uninstallOpts.handleUninstallErrs(e.Err, executeErrs)
if executeErrs != nil {
return span.Error(executeErrs)
}
}
// If uninstallOpts is an empty struct (i.e., action not Uninstall), this
// will resolve to false and thus be a no-op
if uninstallOpts.shouldDelete() {
span.Infof(installationDeleteTmpl, e.depArgs.Installation)
return e.Installations.RemoveInstallation(ctx, e.depArgs.Installation.Namespace, e.depArgs.Installation.Name)
}
return nil
}