-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
apply_controller.go
230 lines (199 loc) · 5.99 KB
/
apply_controller.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
// Package apply implements the apply controller.
package apply
import (
"context"
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/bmatcuk/doublestar/v4"
"github.com/ghodss/yaml"
"github.com/peter-evans/kdef/cli/ctl/apply/docparse"
"github.com/peter-evans/kdef/cli/log"
"github.com/peter-evans/kdef/core/client"
"github.com/peter-evans/kdef/core/model/def"
"github.com/peter-evans/kdef/core/model/opt"
"github.com/peter-evans/kdef/core/model/res"
"github.com/peter-evans/kdef/core/operators/acl"
"github.com/peter-evans/kdef/core/operators/broker"
"github.com/peter-evans/kdef/core/operators/brokers"
"github.com/peter-evans/kdef/core/operators/topic"
)
const cannotContinueOnError = "cannot continue on error"
type applier interface {
Execute(ctx context.Context) *res.ApplyResult
}
// ControllerOptions represents options to configure an apply controller.
type ControllerOptions struct {
// Applier options.
DefinitionFormat opt.DefinitionFormat
PropertyOverrides []string
DryRun bool
ReassAwaitTimeout int
// Apply controller specific options.
ContinueOnError bool
ExitCode bool
JSONOutput bool
}
// NewApplyController creates a new apply controller.
func NewApplyController(
cl *client.Client,
args []string,
opts ControllerOptions,
) *applyController { //revive:disable-line:unexported-return
return &applyController{
cl: cl,
args: args,
opts: opts,
}
}
type applyController struct {
cl *client.Client
args []string
opts ControllerOptions
}
// Execute implements the execution of the apply controller.
func (a *applyController) Execute(ctx context.Context) error {
results := res.ApplyResults{}
var ctlErrors bool
if a.args[0] == "-" {
// Apply definitions from stdin.
res, err := a.applyDefsFromStdin(ctx)
results = append(results, res...)
if err != nil {
log.Error(err)
ctlErrors = true
}
} else {
// Apply definitions from file.
for _, arg := range a.args {
basepath, pattern := doublestar.SplitPattern(arg)
fsys := os.DirFS(basepath)
err := doublestar.GlobWalk(fsys, pattern, func(p string, d fs.DirEntry) error {
if d.IsDir() {
return nil
}
res, err := a.applyDefsFromFile(ctx, filepath.Join(basepath, p))
results = append(results, res...)
if err != nil {
log.Error(err)
ctlErrors = true
}
if (err != nil || res.ContainsErr()) && !a.opts.ContinueOnError {
return fmt.Errorf(cannotContinueOnError)
}
return nil
})
if err != nil {
if err.Error() != cannotContinueOnError {
log.Error(err)
ctlErrors = true
}
if !a.opts.ContinueOnError {
break
}
}
}
}
if a.opts.JSONOutput {
out, err := results.JSON()
if err != nil {
return err
}
fmt.Printf("%s\n", out)
}
if len(results) == 0 {
log.Error(fmt.Errorf("no valid resource definitions found"))
ctlErrors = true
}
if ctlErrors || results.ContainsErr() {
return fmt.Errorf("apply completed with errors")
}
if a.opts.ExitCode && results.ContainsUnappliedChanges() {
return fmt.Errorf("unapplied changes exist")
}
return nil
}
func (a *applyController) applyDefsFromStdin(ctx context.Context) (res.ApplyResults, error) {
log.Infof("Reading definition(s) from stdin")
defDocs, err := docparse.FromStdin(docparse.Format(a.opts.DefinitionFormat))
if err != nil {
return nil, fmt.Errorf("failed to read definition(s): %v", err)
}
return a.applyDefinitions(ctx, defDocs)
}
func (a *applyController) applyDefsFromFile(ctx context.Context, filepath string) (res.ApplyResults, error) {
log.Infof("Reading definition(s) from file %q", filepath)
defDocs, err := docparse.FromFile(filepath, docparse.Format(a.opts.DefinitionFormat))
if err != nil {
return nil, fmt.Errorf("failed to read definition(s): %v", err)
}
return a.applyDefinitions(ctx, defDocs)
}
func (a *applyController) applyDefinitions(ctx context.Context, defDocs []string) (res.ApplyResults, error) {
resourceDefs, err := getResourceDefinitions(defDocs, a.opts.DefinitionFormat)
if err != nil {
return nil, fmt.Errorf("invalid resource definition: %v", err)
}
var results res.ApplyResults
for i, resourceDef := range resourceDefs {
var applier applier
switch resourceDef.Kind {
case def.KindACL:
applier = acl.NewApplier(a.cl, defDocs[i], acl.ApplierOptions{
DefinitionFormat: a.opts.DefinitionFormat,
PropertyOverrides: a.opts.PropertyOverrides,
DryRun: a.opts.DryRun,
})
case def.KindBroker:
applier = broker.NewApplier(a.cl, defDocs[i], broker.ApplierOptions{
DefinitionFormat: a.opts.DefinitionFormat,
PropertyOverrides: a.opts.PropertyOverrides,
DryRun: a.opts.DryRun,
})
case def.KindBrokers:
applier = brokers.NewApplier(a.cl, defDocs[i], brokers.ApplierOptions{
DefinitionFormat: a.opts.DefinitionFormat,
PropertyOverrides: a.opts.PropertyOverrides,
DryRun: a.opts.DryRun,
})
case def.KindTopic:
applier = topic.NewApplier(a.cl, defDocs[i], topic.ApplierOptions{
DefinitionFormat: a.opts.DefinitionFormat,
PropertyOverrides: a.opts.PropertyOverrides,
DryRun: a.opts.DryRun,
ReassAwaitTimeout: a.opts.ReassAwaitTimeout,
})
}
res := applier.Execute(ctx)
results = append(results, res)
if res.GetErr() != nil && !a.opts.ContinueOnError {
return results, nil
}
}
return results, nil
}
func getResourceDefinitions(defDocs []string, format opt.DefinitionFormat) ([]def.ResourceDefinition, error) {
kinds := make([]def.ResourceDefinition, len(defDocs))
for i, defDoc := range defDocs {
var resourceDef def.ResourceDefinition
switch format {
case opt.YAMLFormat:
if err := yaml.Unmarshal([]byte(defDoc), &resourceDef); err != nil {
return nil, err
}
case opt.JSONFormat:
if err := json.Unmarshal([]byte(defDoc), &resourceDef); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported format")
}
if err := resourceDef.ValidateResource(); err != nil {
return nil, err
}
kinds[i] = resourceDef
}
return kinds, nil
}