/
apply.go
107 lines (88 loc) · 3.05 KB
/
apply.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
package porter
import (
"context"
"errors"
"fmt"
"get.porter.sh/porter/pkg/encoding"
"get.porter.sh/porter/pkg/portercontext"
"get.porter.sh/porter/pkg/printer"
"get.porter.sh/porter/pkg/storage"
"get.porter.sh/porter/pkg/tracing"
"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap/zapcore"
)
type ApplyOptions struct {
Namespace string
File string
// Force the installation to be re-applied regardless of anything being changed or not
Force bool
// DryRun only checks if the changes would trigger a bundle run
DryRun bool
}
const ApplyDefaultFormat = printer.FormatPlaintext
var ApplyAllowedFormats = printer.Formats{printer.FormatPlaintext, printer.FormatYaml, printer.FormatJson}
func (o *ApplyOptions) Validate(cxt *portercontext.Context, args []string) error {
switch len(args) {
case 0:
return errors.New("a file argument is required")
case 1:
o.File = args[0]
default:
return errors.New("only one file argument may be specified")
}
info, err := cxt.FileSystem.Stat(o.File)
if err != nil {
return fmt.Errorf("invalid file argument %s: %w", o.File, err)
}
if info.IsDir() {
return fmt.Errorf("invalid file argument %s, must be a file not a directory", o.File)
}
return nil
}
func (p *Porter) InstallationApply(ctx context.Context, opts ApplyOptions) error {
ctx, log := tracing.StartSpan(ctx)
defer log.EndSpan()
log.Debugf("Reading input file %s", opts.File)
namespace, err := p.getNamespaceFromFile(opts)
if err != nil {
return log.Error(err)
}
if log.ShouldLog(zapcore.DebugLevel) {
// ignoring any error here, printing debug info isn't critical
contents, _ := p.FileSystem.ReadFile(opts.File)
log.Debug("read input file", attribute.String("contents", string(contents)))
}
var input DisplayInstallation
if err := encoding.UnmarshalFile(p.FileSystem, opts.File, &input); err != nil {
return log.Errorf("unable to parse %s as an installation document: %w", opts.File, err)
}
input.Namespace = namespace
inputInstallation, err := input.ConvertToInstallation()
if err != nil {
return log.Error(err)
}
installation, err := p.Installations.GetInstallation(ctx, inputInstallation.Namespace, inputInstallation.Name)
if err != nil {
if !errors.Is(err, storage.ErrNotFound{}) {
return log.Errorf("could not query for an existing installation document for %s: %w", inputInstallation, err)
}
// Create a new installation
installation = storage.NewInstallation(input.Namespace, input.Name)
log.Info("Creating a new installation", attribute.String("installation", installation.String()))
} else {
log.Infof("Updating %s installation\n", installation)
}
installation.Apply(inputInstallation.InstallationSpec)
checkStrategy := p.GetSchemaCheckStrategy(ctx)
if err := installation.Validate(ctx, checkStrategy); err != nil {
return log.Errorf("invalid installation: %w", err)
}
reconcileOpts := ReconcileOptions{
Namespace: input.Namespace,
Name: input.Name,
Installation: installation,
Force: opts.Force,
DryRun: opts.DryRun,
}
return p.ReconcileInstallation(ctx, reconcileOpts)
}