This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
/
release_cmd.go
262 lines (228 loc) · 7.94 KB
/
release_cmd.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
package main
import (
"context"
"fmt"
"io"
"time"
"github.com/spf13/cobra"
"github.com/weaveworks/flux"
"github.com/weaveworks/flux/api/v11"
"github.com/weaveworks/flux/api/v6"
"github.com/weaveworks/flux/cluster"
"github.com/weaveworks/flux/job"
"github.com/weaveworks/flux/update"
)
type workloadReleaseOpts struct {
*rootOpts
namespace string
workloads []string
allWorkloads bool
image string
allImages bool
exclude []string
dryRun bool
interactive bool
force bool
watch bool
outputOpts
cause update.Cause
// Deprecated
controllers []string
}
func newWorkloadRelease(parent *rootOpts) *workloadReleaseOpts {
return &workloadReleaseOpts{rootOpts: parent}
}
func (opts *workloadReleaseOpts) Command() *cobra.Command {
cmd := &cobra.Command{
Use: "release",
Short: "Release a new version of a workload.",
Example: makeExample(
"fluxctl release -n default --workload=deployment/foo --update-image=library/hello:v2",
"fluxctl release --all --update-image=library/hello:v2",
"fluxctl release --workload=default:deployment/foo --update-all-images",
),
RunE: opts.RunE,
}
AddOutputFlags(cmd, &opts.outputOpts)
AddCauseFlags(cmd, &opts.cause)
cmd.Flags().StringVarP(&opts.namespace, "namespace", "n", "default", "Workload namespace")
// Note: we cannot define a shorthand for --workload since it clashes with the shorthand of --watch
cmd.Flags().StringSliceVarP(&opts.workloads, "workload", "", []string{}, "List of workloads to release <namespace>:<kind>/<name>")
cmd.Flags().BoolVar(&opts.allWorkloads, "all", false, "Release all workloads")
cmd.Flags().StringVarP(&opts.image, "update-image", "i", "", "Update a specific image")
cmd.Flags().BoolVar(&opts.allImages, "update-all-images", false, "Update all images to latest versions")
cmd.Flags().StringSliceVar(&opts.exclude, "exclude", []string{}, "List of workloads to exclude")
cmd.Flags().BoolVar(&opts.dryRun, "dry-run", false, "Do not release anything; just report back what would have been done")
cmd.Flags().BoolVar(&opts.interactive, "interactive", false, "Select interactively which containers to update")
cmd.Flags().BoolVarP(&opts.force, "force", "f", false, "Disregard locks and container image filters (has no effect when used with --all or --update-all-images)")
cmd.Flags().BoolVarP(&opts.watch, "watch", "w", false, "Watch rollout progress during release")
// Deprecated
cmd.Flags().StringSliceVarP(&opts.controllers, "controller", "c", []string{}, "List of controllers to release <namespace>:<kind>/<name>")
cmd.Flags().MarkDeprecated("controller", "changed to --workload, use that instead")
return cmd
}
func (opts *workloadReleaseOpts) RunE(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errorWantedNoArgs
}
if err := checkExactlyOne("--update-image=<image> or --update-all-images", opts.image != "", opts.allImages); err != nil {
return err
}
// Backwards compatibility with --controller until we remove it
opts.workloads = append(opts.workloads, opts.controllers...)
switch {
case len(opts.workloads) <= 0 && !opts.allWorkloads:
return newUsageError("please supply either --all, or at least one --workload=<workload>")
case opts.watch && opts.dryRun:
return newUsageError("cannot use --watch with --dry-run")
case opts.force && opts.allWorkloads && opts.allImages:
return newUsageError("--force has no effect when used with --all and --update-all-images")
case opts.force && opts.allWorkloads:
fmt.Fprintf(cmd.OutOrStderr(), "Warning: --force will not ignore locked workloads when used with --all\n")
case opts.force && opts.allImages:
fmt.Fprintf(cmd.OutOrStderr(), "Warning: --force will not ignore container image tags when used with --update-all-images\n")
}
var workloads []update.ResourceSpec
if opts.allWorkloads {
workloads = []update.ResourceSpec{update.ResourceSpecAll}
} else {
for _, workload := range opts.workloads {
id, err := flux.ParseResourceIDOptionalNamespace(opts.namespace, workload)
if err != nil {
return err
}
workloads = append(workloads, update.MakeResourceSpec(id))
}
}
var (
image update.ImageSpec
err error
)
switch {
case opts.image != "":
image, err = update.ParseImageSpec(opts.image)
if err != nil {
return err
}
case opts.allImages:
image = update.ImageSpecLatest
}
var kind update.ReleaseKind = update.ReleaseKindExecute
if opts.dryRun || opts.interactive {
kind = update.ReleaseKindPlan
}
var excludes []flux.ResourceID
for _, exclude := range opts.exclude {
s, err := flux.ParseResourceIDOptionalNamespace(opts.namespace, exclude)
if err != nil {
return err
}
excludes = append(excludes, s)
}
if kind == update.ReleaseKindPlan {
fmt.Fprintf(cmd.OutOrStderr(), "Submitting dry-run release ...\n")
} else {
fmt.Fprintf(cmd.OutOrStderr(), "Submitting release ...\n")
}
ctx := context.Background()
spec := update.ReleaseImageSpec{
ServiceSpecs: workloads,
ImageSpec: image,
Kind: kind,
Excludes: excludes,
Force: opts.force,
}
jobID, err := opts.API.UpdateManifests(ctx, update.Spec{
Type: update.Images,
Cause: opts.cause,
Spec: spec,
})
if err != nil {
return err
}
result, err := awaitJob(ctx, opts.API, jobID)
if err != nil {
return err
}
if opts.interactive {
spec, err := promptSpec(cmd.OutOrStdout(), result, opts.verbosity)
spec.Force = opts.force
if err != nil {
fmt.Fprintln(cmd.OutOrStderr(), err.Error())
return nil
}
fmt.Fprintf(cmd.OutOrStderr(), "Submitting selected release ...\n")
jobID, err = opts.API.UpdateManifests(ctx, update.Spec{
Type: update.Containers,
Cause: opts.cause,
Spec: spec,
})
if err != nil {
fmt.Fprintln(cmd.OutOrStderr(), err.Error())
return nil
}
opts.dryRun = false
}
err = await(ctx, cmd.OutOrStdout(), cmd.OutOrStderr(), opts.API, jobID, !opts.dryRun, opts.verbosity)
if !opts.watch || err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStderr(), "Monitoring rollout ...\n")
for {
completed := 0
workloads, err := opts.API.ListServicesWithOptions(ctx, v11.ListServicesOptions{Services: result.Result.AffectedResources()})
if err != nil {
return err
}
for _, workload := range workloads {
writeRolloutStatus(workload, opts.verbosity)
if workload.Status == cluster.StatusReady {
completed++
}
if workload.Rollout.Messages != nil {
fmt.Fprintf(cmd.OutOrStderr(), "There was a problem releasing %s:\n", workload.ID)
for _, msg := range workload.Rollout.Messages {
fmt.Fprintf(cmd.OutOrStderr(), "%s\n", msg)
}
return nil
}
}
if completed == len(workloads) {
fmt.Fprintf(cmd.OutOrStderr(), "All workloads ready.\n")
return nil
}
time.Sleep(2000 * time.Millisecond)
}
}
func writeRolloutStatus(workload v6.ControllerStatus, verbosity int) {
w := newTabwriter()
fmt.Fprintf(w, "WORKLOAD\tCONTAINER\tIMAGE\tRELEASE\tREPLICAS\n")
if len(workload.Containers) > 0 {
c := workload.Containers[0]
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d/%d", workload.ID, c.Name, c.Current.ID, workload.Status, workload.Rollout.Updated, workload.Rollout.Desired)
if verbosity > 0 {
fmt.Fprintf(w, " (%d outdated, %d ready)", workload.Rollout.Outdated, workload.Rollout.Ready)
}
fmt.Fprintf(w, "\n")
for _, c := range workload.Containers[1:] {
fmt.Fprintf(w, "\t%s\t%s\t\t\n", c.Name, c.Current.ID)
}
} else {
fmt.Fprintf(w, "%s\t\t\t%s\t%d/%d", workload.ID, workload.Status, workload.Rollout.Updated, workload.Rollout.Desired)
if verbosity > 0 {
fmt.Fprintf(w, " (%d outdated, %d ready)", workload.Rollout.Outdated, workload.Rollout.Ready)
}
fmt.Fprintf(w, "\n")
}
fmt.Fprintln(w)
w.Flush()
}
func promptSpec(out io.Writer, result job.Result, verbosity int) (update.ReleaseContainersSpec, error) {
menu := update.NewMenu(out, result.Result, verbosity)
containerSpecs, err := menu.Run()
return update.ReleaseContainersSpec{
Kind: update.ReleaseKindExecute,
ContainerSpecs: containerSpecs,
SkipMismatches: false,
}, err
}