-
Notifications
You must be signed in to change notification settings - Fork 7
/
release.go
397 lines (355 loc) · 14.6 KB
/
release.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
package flow
import (
"context"
"encoding/json"
"fmt"
"path"
"github.com/lunarway/release-manager/internal/artifact"
"github.com/lunarway/release-manager/internal/copy"
"github.com/lunarway/release-manager/internal/git"
"github.com/lunarway/release-manager/internal/log"
"github.com/pkg/errors"
)
// ReleaseBranch releases the latest artifact from a branch of a specific
// service to environment env.
//
// Flow
//
// Checkout the current kubernetes configuration status and find the
// artifact spec for the service and branch.
//
// Copy artifacts from the artifacts into the environment and commit the changes.
func (s *Service) ReleaseBranch(ctx context.Context, actor Actor, environment, service, branch string) (string, error) {
span, ctx := s.Tracer.FromCtx(ctx, "flow.ReleaseBranch")
defer span.Finish()
ok, err := s.CanRelease(ctx, service, branch, environment)
if err != nil {
return "", errors.WithMessage(err, "validate release policies")
}
if !ok {
return "", ErrReleaseProhibited
}
sourceConfigRepoPath, close, err := git.TempDirAsync(ctx, s.Tracer, "k8s-config-release-branch")
if err != nil {
return "", err
}
defer close(ctx)
_, err = s.Git.Clone(ctx, sourceConfigRepoPath)
if err != nil {
return "", errors.WithMessagef(err, "clone into '%s'", sourceConfigRepoPath)
}
// repo/artifacts/{service}/{branch}/{artifactFileName}
artifactSpecPath := path.Join(artifactPath(sourceConfigRepoPath, service, branch), s.ArtifactFileName)
artifactSpec, err := artifact.Get(artifactSpecPath)
if err != nil {
return "", errors.WithMessage(err, fmt.Sprintf("locate source spec"))
}
logger := log.WithContext(ctx)
logger.Infof("flow: ReleaseBranch: release branch: id '%s'", artifactSpec.ID)
// default to environment name for the namespace if none is specified
namespace := artifactSpec.Namespace
if namespace == "" {
namespace = environment
}
// check that the artifact to be released is not already released in the
// environment. If there is no artifact released to the target environment an
// artifact.ErrFileNotFound error is returned. This is OK as the currentSpec
// will then be the default value and this its ID will be the empty string.
currentSpec, err := envSpec(sourceConfigRepoPath, s.ArtifactFileName, service, environment, namespace)
if err != nil && errors.Cause(err) != artifact.ErrFileNotFound {
return "", errors.WithMessage(err, "get current released spec")
}
if currentSpec.ID == artifactSpec.ID {
return "", ErrNothingToRelease
}
err = s.PublishReleaseBranch(ctx, ReleaseBranchEvent{
Branch: branch,
Actor: actor,
Environment: environment,
Namespace: namespace,
Service: service,
})
if err != nil {
return "", errors.WithMessage(err, "publish event")
}
return artifactSpec.ID, nil
}
type ReleaseBranchEvent struct {
Service string `json:"service,omitempty"`
Environment string `json:"environment,omitempty"`
Namespace string `json:"namespace,omitempty"`
Branch string `json:"branch,omitempty"`
Actor Actor `json:"actor,omitempty"`
}
func (ReleaseBranchEvent) Type() string {
return "release.branch"
}
func (p ReleaseBranchEvent) Marshal() ([]byte, error) {
return json.Marshal(p)
}
func (p *ReleaseBranchEvent) Unmarshal(data []byte) error {
return json.Unmarshal(data, p)
}
func (s *Service) ExecReleaseBranch(ctx context.Context, event ReleaseBranchEvent) error {
span, ctx := s.Tracer.FromCtx(ctx, "flow.ExecReleaseBranch")
defer span.Finish()
err := s.retry(ctx, func(ctx context.Context, attempt int) (bool, error) {
logger := log.WithContext(ctx)
sourceConfigRepoPath, close, err := git.TempDirAsync(ctx, s.Tracer, "k8s-config-release-branch")
if err != nil {
return true, err
}
defer close(ctx)
_, err = s.Git.Clone(ctx, sourceConfigRepoPath)
if err != nil {
return true, errors.WithMessagef(err, "clone into '%s'", sourceConfigRepoPath)
}
service := event.Service
branch := event.Branch
environment := event.Environment
namespace := event.Namespace
actor := event.Actor
// repo/artifacts/{service}/{branch}/{artifactFileName}
artifactSpecPath := path.Join(artifactPath(sourceConfigRepoPath, service, branch), s.ArtifactFileName)
artifactSpec, err := artifact.Get(artifactSpecPath)
if err != nil {
return true, errors.WithMessage(err, fmt.Sprintf("locate source spec"))
}
// release service to env from the artifact path
// repo/artifacts/{service}/{branch}/{env}
artifactPath := srcPath(sourceConfigRepoPath, service, branch, environment)
// repo/{env}/releases/{ns}/{service}
destinationPath := releasePath(sourceConfigRepoPath, service, environment, namespace)
logger.Infof("flow: ReleaseBranch: copy resources from %s to %s", artifactPath, destinationPath)
err = s.cleanCopy(ctx, artifactPath, destinationPath)
if err != nil {
return true, errors.WithMessagef(err, "copy resources from '%s' to '%s'", artifactPath, destinationPath)
}
// copy artifact spec
// repo/{env}/releases/{ns}/{service}/{artifactFileName}
artifactDestinationPath := path.Join(releasePath(sourceConfigRepoPath, service, environment, namespace), s.ArtifactFileName)
logger.Infof("flow: ReleaseBranch: copy artifact from %s to %s", artifactSpecPath, artifactDestinationPath)
err = copy.CopyFile(ctx, artifactSpecPath, artifactDestinationPath)
if err != nil {
return true, errors.WithMessage(err, fmt.Sprintf("copy artifact spec from '%s' to '%s'", artifactSpecPath, artifactDestinationPath))
}
authorName := artifactSpec.Application.AuthorName
authorEmail := artifactSpec.Application.AuthorEmail
artifactID := artifactSpec.ID
releaseMessage := git.ReleaseCommitMessage(environment, service, artifactID, authorEmail)
err = s.Git.Commit(ctx, sourceConfigRepoPath, releasePath(".", service, environment, namespace), authorName, authorEmail, actor.Name, actor.Email, releaseMessage)
if err != nil {
if errors.Cause(err) == git.ErrNothingToCommit {
logger.Infof("Environment is up to date: dropping event: %v", err)
// TODO: notify actor that there was nothing to commit
return true, nil
}
// we can see races here where other changes are committed to the master repo
// after we cloned. Because of this we retry on any error.
return false, errors.WithMessage(err, fmt.Sprintf("commit changes from path '%s'", destinationPath))
}
s.notifyRelease(ctx, NotifyReleaseOptions{
Service: service,
Environment: environment,
Namespace: namespace,
Spec: artifactSpec,
Releaser: actor.Name,
})
logger.Infof("flow: ReleaseBranch: release committed: %s, Author: %s <%s>, Committer: %s <%s>", releaseMessage, authorName, authorEmail, actor.Name, actor.Email)
return true, nil
})
if err != nil {
return err
}
return nil
}
type ReleaseArtifactIDEvent struct {
Service string `json:"service,omitempty"`
Environment string `json:"environment,omitempty"`
Namespace string `json:"namespace,omitempty"`
ArtifactID string `json:"artifactID,omitempty"`
Branch string `json:"branch,omitempty"`
Actor Actor `json:"actor,omitempty"`
}
func (ReleaseArtifactIDEvent) Type() string {
return "release.artifactId"
}
func (p ReleaseArtifactIDEvent) Marshal() ([]byte, error) {
return json.Marshal(p)
}
func (p *ReleaseArtifactIDEvent) Unmarshal(data []byte) error {
return json.Unmarshal(data, p)
}
// ReleaseArtifactID releases a specific artifact to environment env.
//
// Flow
//
// Locate the commit of the artifact ID and checkout the config repository at
// this point.
//
// Copy resources from the artifact commit into the environment and commit
// the changes
func (s *Service) ReleaseArtifactID(ctx context.Context, actor Actor, environment, service, artifactID string) (string, error) {
span, ctx := s.Tracer.FromCtx(ctx, "flow.ReleaseArtifactID")
defer span.Finish()
sourceConfigRepoPath, closeSource, err := git.TempDirAsync(ctx, s.Tracer, "k8s-config-release-artifact-source")
if err != nil {
return "", err
}
defer closeSource(ctx)
sourceRepo, err := s.Git.Clone(ctx, sourceConfigRepoPath)
if err != nil {
return "", errors.WithMessagef(err, "clone into '%s'", sourceConfigRepoPath)
}
// FIXME: this is a bottleneck regarding response-time. We do a git
// rev-parse to find the hash of the artifact. If we can eliminate this need
// we can skip the initial master repo clone
hash, err := s.Git.LocateArtifact(ctx, sourceRepo, artifactID)
if err != nil {
return "", errors.WithMessagef(err, "locate release '%s'", artifactID)
}
err = s.Git.Checkout(ctx, sourceConfigRepoPath, hash)
if err != nil {
return "", errors.WithMessagef(err, "checkout release hash '%s'", hash)
}
branch, err := git.BranchFromHead(ctx, sourceRepo, s.ArtifactFileName, service)
if err != nil {
return "", errors.WithMessagef(err, "locate branch from commit hash '%s'", hash)
}
ok, err := s.CanRelease(ctx, service, branch, environment)
if err != nil {
return "", errors.WithMessage(err, "validate release policies")
}
if !ok {
return "", ErrReleaseProhibited
}
sourceSpec, err := artifact.Get(srcPath(sourceConfigRepoPath, service, branch, s.ArtifactFileName))
if err != nil {
return "", errors.WithMessage(err, fmt.Sprintf("locate source spec"))
}
logger := log.WithContext(ctx)
logger.Infof("flow: ReleaseArtifactID: hash '%s' id '%s'", hash, sourceSpec.ID)
// default to environment name for the namespace if none is specified
namespace := sourceSpec.Namespace
if namespace == "" {
namespace = environment
}
// check that the artifact to be released is not already released in the
// environment. If there is no artifact released to the target environment an
// artifact.ErrFileNotFound error is returned. This is OK as the currentSpec
// will then be the default value and this its ID will be the empty string.
destinationConfigRepoPath, closeDestinationSource, err := git.TempDirAsync(ctx, s.Tracer, "k8s-config-release-artifact-destination")
if err != nil {
return "", err
}
defer closeDestinationSource(ctx)
_, err = s.Git.Clone(ctx, destinationConfigRepoPath)
if err != nil {
return "", errors.WithMessagef(err, "clone into '%s'", sourceConfigRepoPath)
}
currentSpec, err := envSpec(destinationConfigRepoPath, s.ArtifactFileName, service, environment, namespace)
if err != nil && errors.Cause(err) != artifact.ErrFileNotFound {
return "", errors.WithMessage(err, "get current released spec")
}
if currentSpec.ID == sourceSpec.ID {
return "", ErrNothingToRelease
}
err = s.PublishReleaseArtifactID(ctx, ReleaseArtifactIDEvent{
ArtifactID: artifactID,
Actor: actor,
Branch: branch,
Environment: environment,
Namespace: namespace,
Service: service,
})
if err != nil {
return "", errors.WithMessage(err, "publish event")
}
return artifactID, nil
}
func (s *Service) ExecReleaseArtifactID(ctx context.Context, event ReleaseArtifactIDEvent) error {
span, ctx := s.Tracer.FromCtx(ctx, "flow.ExecReleaseArtifactID")
defer span.Finish()
err := s.retry(ctx, func(ctx context.Context, attempt int) (bool, error) {
logger := log.WithContext(ctx)
sourceConfigRepoPath, closeSource, err := git.TempDirAsync(ctx, s.Tracer, "k8s-config-release-artifact-source")
if err != nil {
return true, err
}
defer closeSource(ctx)
sourceRepo, err := s.Git.Clone(ctx, sourceConfigRepoPath)
if err != nil {
return true, errors.WithMessagef(err, "clone into '%s'", sourceConfigRepoPath)
}
destinationConfigRepoPath, closeDestination, err := git.TempDirAsync(ctx, s.Tracer, "k8s-config-release-artifact-destination")
if err != nil {
return true, err
}
defer closeDestination(ctx)
_, err = s.Git.Clone(ctx, destinationConfigRepoPath)
if err != nil {
return true, errors.WithMessagef(err, "clone destination repo into '%s'", destinationConfigRepoPath)
}
service := event.Service
branch := event.Branch
environment := event.Environment
namespace := event.Namespace
actor := event.Actor
artifactID := event.ArtifactID
hash, err := s.Git.LocateArtifact(ctx, sourceRepo, artifactID)
if err != nil {
return true, errors.WithMessagef(err, "locate release '%s'", artifactID)
}
err = s.Git.Checkout(ctx, sourceConfigRepoPath, hash)
if err != nil {
return true, errors.WithMessagef(err, "checkout release hash '%s'", hash)
}
// release service to env from original release
sourcePath := srcPath(sourceConfigRepoPath, service, branch, environment)
destinationPath := releasePath(destinationConfigRepoPath, service, environment, namespace)
logger.Infof("flow: ReleaseArtifactID: copy resources from %s to %s", sourcePath, destinationPath)
err = s.cleanCopy(ctx, sourcePath, destinationPath)
if err != nil {
return true, errors.WithMessagef(err, "copy resources from '%s' to '%s'", sourcePath, destinationPath)
}
// copy artifact spec
artifactSourcePath := srcPath(sourceConfigRepoPath, service, branch, s.ArtifactFileName)
artifactDestinationPath := path.Join(releasePath(destinationConfigRepoPath, service, environment, namespace), s.ArtifactFileName)
logger.Infof("flow: ReleaseArtifactID: copy artifact from %s to %s", artifactSourcePath, artifactDestinationPath)
err = copy.CopyFile(ctx, artifactSourcePath, artifactDestinationPath)
if err != nil {
return true, errors.WithMessage(err, fmt.Sprintf("copy artifact spec from '%s' to '%s'", artifactSourcePath, artifactDestinationPath))
}
sourceSpec, err := artifact.Get(srcPath(sourceConfigRepoPath, service, branch, s.ArtifactFileName))
if err != nil {
return true, errors.WithMessage(err, fmt.Sprintf("locate source spec"))
}
authorName := sourceSpec.Application.AuthorName
authorEmail := sourceSpec.Application.AuthorEmail
releaseMessage := git.ReleaseCommitMessage(environment, service, artifactID, authorEmail)
err = s.Git.Commit(ctx, destinationConfigRepoPath, releasePath(".", service, environment, namespace), authorName, authorEmail, actor.Name, actor.Email, releaseMessage)
if err != nil {
if errors.Cause(err) == git.ErrNothingToCommit {
logger.Infof("Environment is up to date: dropping event: %v", err)
// TODO: notify actor that there was nothing to commit
return true, nil
}
// we can see races here where other changes are committed to the master repo
// after we cloned. Because of this we retry on any error.
return false, errors.WithMessage(err, fmt.Sprintf("commit changes from path '%s'", destinationPath))
}
s.notifyRelease(ctx, NotifyReleaseOptions{
Service: service,
Environment: environment,
Namespace: namespace,
Spec: sourceSpec,
Releaser: actor.Name,
})
logger.Infof("flow: ReleaseArtifactID: release committed: %s, Author: %s <%s>, Committer: %s <%s>", releaseMessage, authorName, authorEmail, actor.Name, actor.Email)
return true, nil
})
if err != nil {
return err
}
return nil
}