-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathanago.go
407 lines (328 loc) · 11.2 KB
/
anago.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package anago
import (
"fmt"
"github.com/blang/semver"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/release/pkg/git"
"k8s.io/release/pkg/log"
"k8s.io/release/pkg/release"
"k8s.io/release/pkg/util"
"k8s.io/release/pkg/version"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
const (
// workspaceDir is the global directory where the stage and release process
// happens.
workspaceDir = "/workspace"
// gitRoot is the local repository root of k/k.
gitRoot = workspaceDir + "/src/k8s.io/kubernetes"
// releaseNotesHTMLFile is the name of the release notes in HTML
releaseNotesHTMLFile = workspaceDir + "/src/release-notes.html"
// releaseNotesJSONFile is the file containing the release notes in json format
releaseNotesJSONFile = workspaceDir + "/src/release-notes.json"
)
// Options are settings which will be used by `StageOptions` as well as
// `ReleaseOptions`.
type Options struct {
// Run the whole process in non-mocked mode. Which means that it uses
// production remote locations for storing artifacts and modifying git
// repositories.
NoMock bool
// The release type which should be produced. Can be either `alpha`,
// `beta`, `rc` or `official`.
ReleaseType string
// The release branch for which the release should be build. Can be
// `master` or any `release-x.y` branch.
ReleaseBranch string
// The build version to be released. Has to be specified in the format:
// `vX.Y.Z-[alpha|beta|rc].N.C+SHA`
BuildVersion string
}
// DefaultOptions returns a new Options instance.
func DefaultOptions() *Options {
return &Options{
ReleaseType: release.ReleaseTypeAlpha,
ReleaseBranch: git.DefaultBranch,
}
}
// String returns a string representation for the `ReleaseOptions` type.
func (o *Options) String() string {
return fmt.Sprintf(
"NoMock: %v, ReleaseType: %q, BuildVersion: %q, ReleaseBranch: %q",
o.NoMock, o.ReleaseType, o.BuildVersion, o.ReleaseBranch,
)
}
// Validate if the options are correctly set.
func (o *Options) Validate(state *State) error {
logrus.Infof("Validating generic options: %s", o.String())
if o.ReleaseType != release.ReleaseTypeAlpha &&
o.ReleaseType != release.ReleaseTypeBeta &&
o.ReleaseType != release.ReleaseTypeRC &&
o.ReleaseType != release.ReleaseTypeOfficial {
return errors.Errorf("invalid release type: %s", o.ReleaseType)
}
if !git.IsReleaseBranch(o.ReleaseBranch) {
return errors.Errorf("invalid release branch: %s", o.ReleaseBranch)
}
semverBuildVersion, err := util.TagStringToSemver(o.BuildVersion)
if err != nil {
return errors.Wrapf(err, "invalid build version: %s", o.BuildVersion)
}
state.semverBuildVersion = semverBuildVersion
return nil
}
// Bucket returns the Google Cloud Bucket for these `Options`.
func (o *Options) Bucket() string {
if o.NoMock {
return release.ProductionBucket
}
return release.TestBucket
}
// ContainerRegistry returns the container registry for these `Options`.
func (o *Options) ContainerRegistry() string {
if o.NoMock {
return release.GCRIOPathStaging
}
return release.GCRIOPathMock
}
// State holds all inferred and calculated values from the release process
// it's state mutates as each step es executed
type State struct {
// logFile is the internal logging file target.
logFile string // nolint: structcheck
// semverBuildVersion is the parsed build version which is set after the
// validation.
semverBuildVersion semver.Version
// The release versions generated after GenerateReleaseVersion()
versions *release.Versions
// Indicates if we're going to create a new release branch.
createReleaseBranch bool
}
// DefaultState returns a new empty State
func DefaultState() *State {
// The default state is empty, it will be initialized after ValidateOptions()
// runs in Stage/Releas. It will change as the satege/release processes move forward
return &State{}
}
func (s *State) SetCreateReleaseBranch(createReleaseBranch bool) {
s.createReleaseBranch = createReleaseBranch
}
func (s *State) SetVersions(versions *release.Versions) {
s.versions = versions
}
// StageState holds the release process state
type StageState struct {
*State
}
// DefaultStageState createa a new default `ReleaseOptions`.
func DefaultStageState() *StageState {
return &StageState{
State: DefaultState(),
}
}
// StageOptions contains the options for running `Stage`.
type StageOptions struct {
*Options
}
// DefaultStageOptions createa a new default `StageOptions`.
func DefaultStageOptions() *StageOptions {
return &StageOptions{
Options: DefaultOptions(),
}
}
// String returns a string representation for the `StageOptions` type.
func (s *StageOptions) String() string {
return s.Options.String()
}
// Validate if the options are correctly set.
func (s *StageOptions) Validate(state *State) error {
if err := s.Options.Validate(state); err != nil {
return errors.Wrap(err, "validating generic options")
}
return nil
}
// Stage is the structure to be used for staging releases.
type Stage struct {
client stageClient
}
// NewStage creates a new `Stage` instance.
func NewStage(options *StageOptions) *Stage {
return &Stage{NewDefaultStage(options)}
}
// SetClient can be used to set the internal stage client.
func (s *Stage) SetClient(client stageClient) {
s.client = client
}
// Submit can be used to submit a staging Google Cloud Build (GCB) job.
func (s *Stage) Submit(stream bool) error {
logrus.Info("Submitting stage GCB job")
if err := s.client.Submit(stream); err != nil {
return errors.Wrap(err, "submit stage job")
}
return nil
}
// Run for the `Stage` struct prepares a release and puts the results on a
// staging bucket.
// nolint:dupl
func (s *Stage) Run() error {
s.client.InitState()
if err := s.client.InitLogFile(); err != nil {
return errors.Wrap(err, "init log file")
}
logger := log.NewStepLogger(9)
logger.Infof("Using krel version:\n%s", version.Get().String())
logger.WithStep().Info("Validating options")
if err := s.client.ValidateOptions(); err != nil {
return errors.Wrap(err, "validate options")
}
logger.WithStep().Info("Checking prerequisites")
if err := s.client.CheckPrerequisites(); err != nil {
return errors.Wrap(err, "check prerequisites")
}
logger.WithStep().Info("Checking release branch state")
if err := s.client.CheckReleaseBranchState(); err != nil {
return errors.Wrap(err, "check release branch state")
}
logger.WithStep().Info("Generating release version")
if err := s.client.GenerateReleaseVersion(); err != nil {
return errors.Wrap(err, "generate release version")
}
logger.WithStep().Info("Preparing workspace")
if err := s.client.PrepareWorkspace(); err != nil {
return errors.Wrap(err, "prepare workspace")
}
logger.WithStep().Info("Tagging repository")
if err := s.client.TagRepository(); err != nil {
return errors.Wrap(err, "tag repository")
}
logger.WithStep().Info("Building release")
if err := s.client.Build(); err != nil {
return errors.Wrap(err, "build release")
}
logger.WithStep().Info("Generating changelog")
if err := s.client.GenerateChangelog(); err != nil {
return errors.Wrap(err, "generate changelog")
}
logger.WithStep().Info("Staging artifacts")
if err := s.client.StageArtifacts(); err != nil {
return errors.Wrap(err, "stage release artifacts")
}
logger.Info("Stage done")
return nil
}
// ReleaseState holds the release process state
type ReleaseState struct {
*State
}
// DefaultReleaseState createa a new default `ReleaseOptions`.
func DefaultReleaseState() *ReleaseState {
return &ReleaseState{
State: DefaultState(),
}
}
// ReleaseOptions contains the options for running `Release`.
type ReleaseOptions struct {
*Options
}
// DefaultReleaseOptions createa a new default `ReleaseOptions`.
func DefaultReleaseOptions() *ReleaseOptions {
return &ReleaseOptions{
Options: DefaultOptions(),
}
}
// String returns a string representation for the `ReleaseOptions` type.
func (r *ReleaseOptions) String() string {
return r.Options.String()
}
// Validate if the options are correctly set.
func (r *ReleaseOptions) Validate(state *State) error {
if err := r.Options.Validate(state); err != nil {
return errors.Wrap(err, "validating generic options")
}
return nil
}
// Release is the structure to be used for releasing staged releases.
type Release struct {
client releaseClient
}
// NewRelease creates a new `Release` instance.
func NewRelease(options *ReleaseOptions) *Release {
return &Release{NewDefaultRelease(options)}
}
// SetClient can be used to set the internal stage client.
func (r *Release) SetClient(client releaseClient) {
r.client = client
}
// Submit can be used to submit a releasing Google Cloud Build (GCB) job.
func (r *Release) Submit(stream bool) error {
logrus.Info("Submitting release GCB job")
if err := r.client.Submit(stream); err != nil {
return errors.Wrap(err, "submit release job")
}
return nil
}
// Run for for `Release` struct finishes a previously staged release.
// nolint:dupl
func (r *Release) Run() error {
r.client.InitState()
if err := r.client.InitLogFile(); err != nil {
return errors.Wrap(err, "init log file")
}
logger := log.NewStepLogger(10)
logger.Infof("Using krel version:\n%s", version.Get().String())
logger.WithStep().Info("Validating options")
if err := r.client.ValidateOptions(); err != nil {
return errors.Wrap(err, "validate options")
}
logger.WithStep().Info("Checking prerequisites")
if err := r.client.CheckPrerequisites(); err != nil {
return errors.Wrap(err, "check prerequisites")
}
logger.WithStep().Info("Checking release branch state")
if err := r.client.CheckReleaseBranchState(); err != nil {
return errors.Wrap(err, "check release branch state")
}
logger.WithStep().Info("Generating release version")
if err := r.client.GenerateReleaseVersion(); err != nil {
return errors.Wrap(err, "generate release version")
}
logger.WithStep().Info("Preparing workspace")
if err := r.client.PrepareWorkspace(); err != nil {
return errors.Wrap(err, "prepare workspace")
}
logger.WithStep().Info("Pushing artifacts")
if err := r.client.PushArtifacts(); err != nil {
return errors.Wrap(err, "push artifacts")
}
logger.WithStep().Info("Pushing git objects")
if err := r.client.PushGitObjects(); err != nil {
return errors.Wrap(err, "push git objects")
}
logger.WithStep().Info("Creating announcement")
if err := r.client.CreateAnnouncement(); err != nil {
return errors.Wrap(err, "create announcement")
}
logger.WithStep().Info("Updating GitHub release page")
if err := r.client.UpdateGitHubPage(); err != nil {
return errors.Wrap(err, "updating github page")
}
logger.WithStep().Info("Archiving release")
if err := r.client.Archive(); err != nil {
return errors.Wrap(err, "archive release")
}
logger.Info("Release done")
return nil
}