-
Notifications
You must be signed in to change notification settings - Fork 1
/
workflow.go
282 lines (231 loc) · 6.93 KB
/
workflow.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
package kanvas
import (
"bytes"
"fmt"
"path/filepath"
"strings"
"dario.cat/mergo"
)
type Workflow struct {
Plan [][]string
WorkflowJobs map[string]*WorkflowJob
Dir string
Options Options
deps map[string][]string
}
type WorkflowJob struct {
// Skipped becomes a non-nil map of outputs when the job is skipped
// This isn't a bool because we want to force the user to specify
// the outputs for the skipped job,
// otherwise the subsequent job will fail because it can't find the outputs.
Skipped map[string]string
Dir string
Needs []string
Driver *Driver
}
func NewWorkflow(config Component, opts Options) (*Workflow, error) {
wf := &Workflow{
WorkflowJobs: map[string]*WorkflowJob{},
Dir: config.Dir,
deps: make(map[string][]string),
Options: opts,
}
if err := wf.Load("", config.Dir, config); err != nil {
return nil, err
}
return wf, nil
}
func (wf *Workflow) Load(path, baseDir string, config Component) error {
components, err := wf.loadEnvironment(config)
if err != nil {
return err
}
if len(components) == 0 {
return fmt.Errorf("no components found")
}
if err := wf.load(path, baseDir, components); err != nil {
return fmt.Errorf("loading %q %q: %w", path, baseDir, err)
}
plan, err := topologicalSort(wf.deps)
if err != nil {
return err
}
if len(components) > 0 && len(plan) == 0 {
return fmt.Errorf("BUG: Unable to produce a valid plan even though there was no error")
}
// Remove top-level components from the plan.
// They are for logically grouping sub-components so
// not needed to be executed.
var midLevels []string
for _, level := range plan[0] {
if strings.Count(level, "/") < 2 {
continue
}
midLevels = append(midLevels, level)
}
// Replace the top-level components with the mid-level ones, if any.
// The mid-level components are the ones that actually contain
// "run" fields to be executed.
// Top-level components are just for grouping hence they don't
// contain "run" fields.
//
// For the test/e2e/testdata/kanvas.yaml example, the plan looks like the below:
//
// Before: plan = [][]string len: 4, cap: 4, [["/product1/appimage","product1"],["/product1/base"],["/product1/argocd"],["/product1/argocd_resources"]]
// After : plan = [][]string len: 4, cap: 4, [["/product1/appimage"],["/product1/base"],["/product1/argocd"],["/product1/argocd_resources"]]
//
// Notice that the top-level component "product1" is removed.
if len(midLevels) > 0 {
plan[0] = midLevels
}
wf.Plan = plan
return nil
}
func (wf *Workflow) loadEnvironment(config Component) (map[string]Component, error) {
var env Environment
if config.Environments != nil && wf.Options.Env != "" {
var ok bool
env, ok = config.Environments[wf.Options.Env]
if !ok {
return nil, fmt.Errorf("environment %q not found", wf.Options.Env)
}
}
r := map[string]Component{}
usedEnvs := map[string]struct{}{}
overrodeEnvs := map[string]struct{}{}
for name, c := range config.Components {
replacement, replaced := env.Uses[name]
if replaced {
if err := replacement.Validate(); err != nil {
return nil, fmt.Errorf("environment %q: override for component %q: %w", wf.Options.Env, name, err)
}
c = replacement
usedEnvs[name] = struct{}{}
}
defaults, err := DeepCopyComponent(env.Defaults)
if err != nil {
return nil, err
}
if err := mergo.Merge(defaults, c, mergo.WithOverride); err != nil {
return nil, err
}
overrides, overrode := env.Overrides[name]
if overrode {
overrodeEnvs[name] = struct{}{}
if err := mergo.Merge(defaults, overrides, mergo.WithOverride); err != nil {
return nil, fmt.Errorf("unable to override component %q: %w", name, err)
}
}
if replaced && overrode {
return nil, fmt.Errorf("component %q is both used and overridden. You can only use or override a component", name)
}
r[name] = c
}
for name := range env.Uses {
if _, ok := usedEnvs[name]; !ok {
return nil, fmt.Errorf("environment %q uses %q but it is not defined", wf.Options.Env, name)
}
}
for name := range env.Overrides {
if _, ok := overrodeEnvs[name]; !ok {
return nil, fmt.Errorf("environment %q overrides %q but it is not defined", wf.Options.Env, name)
}
}
return r, nil
}
func (wf *Workflow) load(path, baseDir string, components map[string]Component) error {
const gitJob = "git"
// "git" job is a special job that is always added to the workflow
if _, ok := wf.WorkflowJobs[gitJob]; !ok {
dir := baseDir
driver := &Driver{
Output: kanvasOutputCommandForID(gitJob),
OutputFunc: func(r *Runtime, op Op, o map[string]string) error {
var tag bytes.Buffer
if err := r.Exec(dir, []string{"git", "tag", "--points-at", "HEAD"}, ExecStdout(&tag)); err != nil {
return fmt.Errorf("unable to get current git tag: %w", err)
}
o["tag"] = strings.TrimSpace(tag.String())
var sha bytes.Buffer
if err := r.Exec(dir, []string{"git", "rev-parse", "HEAD"}, ExecStdout(&sha)); err != nil {
return fmt.Errorf("unable to get current git sha: %w", err)
}
o["sha"] = strings.TrimSpace(sha.String())
return nil
},
}
wf.WorkflowJobs[gitJob] = &WorkflowJob{
Dir: dir,
Driver: driver,
}
}
for name, c := range components {
subPath := ID(path, name)
j := &WorkflowJob{}
//
// We can override the component's skipped flag via options
//
var outs map[string]map[string]string
if wf.Options.SkippedJobsOutputs != nil {
outs = wf.Options.SkippedJobsOutputs
} else {
outs = map[string]map[string]string{}
}
if len(outs) != len(wf.Options.Skip) {
return fmt.Errorf("the number of skipped jobs (%d) doesn't match the number of skipped jobs outputs (%d)", len(wf.Options.Skip), len(outs))
}
var skipped bool
for _, s := range wf.Options.Skip {
if s == subPath {
var m map[string]string
if o, ok := outs[subPath]; ok {
m = o
} else {
m = map[string]string{}
}
j.Skipped = m
skipped = true
break
}
}
var needs []string
if !skipped {
for _, n := range c.Needs {
needs = append(needs, ID(path, n))
if n == gitJob {
// This is to ensure that the git job is managed by the topological sorter.
//
// And we do this only when any of the components needs the git job.
// Otherwise, we end up initializing (and possibly failing) the git component
// even when no other component needs it.
wf.deps[gitJob] = []string{}
}
}
}
dir := c.Dir
if dir == "" {
dir = baseDir
} else {
if dir[0] == '/' {
dir = filepath.Join(wf.Dir, dir)
} else {
dir = filepath.Join(baseDir, dir)
}
}
driver, err := newDriver(subPath, dir, c, wf.Options)
if err != nil {
return fmt.Errorf("component %q: %w", name, err)
}
j.Dir = dir
j.Needs = needs
j.Driver = driver
wf.WorkflowJobs[subPath] = j
if len(c.Components) > 0 {
if err := wf.load(subPath, dir, c.Components); err != nil {
return err
}
}
wf.deps[subPath] = needs
}
return nil
}