-
Notifications
You must be signed in to change notification settings - Fork 46
/
plan.go
149 lines (117 loc) · 4.07 KB
/
plan.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package job
import (
"fmt"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad-pack/internal/pkg/errors"
"github.com/hashicorp/nomad-pack/internal/runner"
"github.com/hashicorp/nomad-pack/terminal"
)
// PlanDeployment satisfies the PlanDeployment function of the runner.Runner
// interface.
func (r *Runner) PlanDeployment(ui terminal.UI, errCtx *errors.UIErrorContext) (int, []*errors.WrappedUIContext) {
var (
exitCode int
outputErrors []*errors.WrappedUIContext
)
if len(r.parsedTemplates) < 1 {
outputErrors = append(outputErrors, newNoParsedTemplatesError("failed to check for conflicts", errCtx))
return runner.PlanCodeError, outputErrors
}
for tplName, parsedJob := range r.parsedTemplates {
// tplErrorContext forms the basis for error output context as is
// appended to when new information becomes available.
tplErrorContext := errCtx.Copy()
tplErrorContext.Add(errors.UIContextPrefixTemplateName, tplName)
tplErrorContext.Add(errors.UIContextPrefixJobName, parsedJob.GetName())
// Set up the options.
planOpts := &api.PlanOptions{
Diff: r.cfg.PlanConfig.Diff,
PolicyOverride: r.cfg.PlanConfig.PolicyOverride,
}
if parsedJob.Job().IsMultiregion() {
return r.multiRegionPlan(planOpts, parsedJob.Job(), ui, tplErrorContext)
}
// Submit the job
planResponse, _, err := r.client.Jobs().PlanOpts(parsedJob.Job(), planOpts, r.newWriteOptsFromJob(parsedJob))
if err != nil {
outputErrors = append(outputErrors, &errors.WrappedUIContext{
Err: err,
Subject: "failed to perform plan",
Context: tplErrorContext,
})
exitCode = runner.HigherPlanCode(exitCode, runner.PlanCodeError)
continue
}
exitCode = runner.HigherPlanCode(exitCode, r.outputPlannedJob(ui, parsedJob.Job(), planResponse))
}
if outputErrors != nil || len(outputErrors) > 0 {
return exitCode, outputErrors
}
return exitCode, nil
}
func (r *Runner) multiRegionPlan(
opts *api.PlanOptions,
job *api.Job,
ui terminal.UI,
errCtx *errors.UIErrorContext) (int, []*errors.WrappedUIContext) {
// Setup our return objects along with a map to store all the plans.
var (
exitCode int
outputErrors []*errors.WrappedUIContext
)
plans := map[string]*api.JobPlanResponse{}
// collect all the plans first so that we can report all errors
for _, region := range job.Multiregion.Regions {
job.Region = ®ion.Name
regionCtx := errCtx.Copy()
regionCtx.Add(errors.UIContextPrefixRegion, region.Name)
// Submit the job for this region
result, _, err := r.client.Jobs().PlanOpts(job, opts, r.newWriteOptsFromClientJob(job))
if err != nil {
outputErrors = append(outputErrors, &errors.WrappedUIContext{
Err: err,
Subject: "failed to perform regional plan",
Context: regionCtx,
})
exitCode = runner.HigherPlanCode(exitCode, runner.PlanCodeError)
continue
}
plans[region.Name] = result
}
if outputErrors != nil || len(outputErrors) > 0 {
return exitCode, outputErrors
}
for regionName, resp := range plans {
ui.Info(fmt.Sprintf("Region: %q", regionName))
exitCode = runner.HigherPlanCode(exitCode, r.outputPlannedJob(ui, job, resp))
}
return exitCode, outputErrors
}
func (r *Runner) outputPlannedJob(ui terminal.UI, job *api.Job, resp *api.JobPlanResponse) int {
// Print the diff if not disabled
if r.cfg.PlanConfig.Diff {
formatJobDiff(*resp.Diff, r.cfg.PlanConfig.Verbose, ui)
}
// Print the scheduler dry-run output
ui.Header("Scheduler dry-run:")
formatDryRun(resp, job, ui)
// Print any warnings if there are any
if resp.Warnings != "" {
ui.Warning(fmt.Sprintf("\nJob Warnings:\n%s", resp.Warnings))
}
// Print preemptions if there are any
if resp.Annotations != nil && resp.Annotations.PreemptedAllocs != nil {
formatPreemptions(ui, resp)
}
return getExitCode(resp)
}
func getExitCode(resp *api.JobPlanResponse) int {
for _, d := range resp.Annotations.DesiredTGUpdates {
if d.Stop+d.Place+d.Migrate+d.DestructiveUpdate+d.Canary > 0 {
return runner.PlanCodeUpdates
}
}
return runner.PlanCodeNoUpdates
}