-
Notifications
You must be signed in to change notification settings - Fork 7
/
rollback.go
219 lines (192 loc) · 8.25 KB
/
rollback.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
package flow
import (
"context"
"encoding/json"
"fmt"
"path"
"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"
"gopkg.in/src-d/go-git.v4/plumbing"
)
type RollbackResult struct {
Previous string
New string
OverwritingNamespace string
}
func (s *Service) Rollback(ctx context.Context, actor Actor, environment, namespace, service string) (RollbackResult, error) {
span, ctx := s.Tracer.FromCtx(ctx, "flow.Rollback")
defer span.Finish()
var result RollbackResult
sourceConfigRepoPath, closeSource, err := git.TempDirAsync(ctx, s.Tracer, "k8s-config-rollback-source")
if err != nil {
return RollbackResult{}, err
}
defer closeSource(ctx)
r, err := s.Git.Clone(ctx, sourceConfigRepoPath)
if err != nil {
return RollbackResult{}, errors.WithMessagef(err, "clone into '%s'", sourceConfigRepoPath)
}
// locate current release
currentHash, err := s.Git.LocateServiceRelease(ctx, r, environment, service)
if err != nil {
return RollbackResult{}, errors.WithMessagef(err, "locate current release at '%s'", sourceConfigRepoPath)
}
logger := log.WithContext(ctx)
logger.Debugf("flow: Rollback: current release hash '%v'", currentHash)
err = s.Git.Checkout(ctx, sourceConfigRepoPath, currentHash)
if err != nil {
return RollbackResult{}, errors.WithMessagef(err, "checkout current release hash '%v'", currentHash)
}
// default to environment name for the namespace if none is specified
if namespace == "" {
namespace = environment
}
logger.Infof("flow: Rollback: using namespace '%s'", namespace)
currentSpec, err := envSpec(sourceConfigRepoPath, s.ArtifactFileName, service, environment, namespace)
if err != nil {
return RollbackResult{}, errors.WithMessagef(err, "get spec of current release hash '%v'", currentHash)
}
// if artifact has no namespace we only allow using the environment as
// namespace.
if currentSpec.Namespace == "" && namespace != environment {
return RollbackResult{}, errors.WithMessagef(ErrNamespaceNotAllowedByArtifact, "namespace '%s'", namespace)
}
// a developer can mistakenly specify the wrong namespace when promoting and
// we will not be able to detect it before this point.
// It only affects "dev" promotes as we read from the artifacts here where we
// can find the artifact without taking the namespace into account.
if currentSpec.Namespace != "" && namespace != currentSpec.Namespace {
logger.Infof("flow: Rollback: overwriting namespace '%s' to '%s'", namespace, currentSpec.Namespace)
namespace = currentSpec.Namespace
result.OverwritingNamespace = currentSpec.Namespace
}
// locate new release (the previous released artifact for this service)
newHash, err := s.Git.LocateServiceReleaseRollbackSkip(ctx, r, environment, service, 1)
if err != nil {
return RollbackResult{}, errors.WithMessagef(err, "locate previous release at '%s'", sourceConfigRepoPath)
}
logger.Debugf("flow: Rollback: new release hash '%v'", newHash)
err = s.Git.Checkout(ctx, sourceConfigRepoPath, newHash)
if err != nil {
return RollbackResult{}, errors.WithMessagef(err, "checkout previous release hash '%v'", newHash)
}
newSpec, err := envSpec(sourceConfigRepoPath, s.ArtifactFileName, service, environment, namespace)
if err != nil {
return RollbackResult{}, errors.WithMessagef(err, "get spec of previous release hash '%v'", newHash)
}
err = s.PublishRollback(ctx, RollbackEvent{
Service: service,
NewHash: newHash.String(),
Actor: actor,
Environment: environment,
Namespace: namespace,
})
if err != nil {
return RollbackResult{}, errors.WithMessage(err, "publish event")
}
result.Previous = currentSpec.ID
result.New = newSpec.ID
return result, nil
}
type RollbackEvent struct {
Service string `json:"service,omitempty"`
Environment string `json:"environment,omitempty"`
Namespace string `json:"namespace,omitempty"`
CurrentArtifactID string `json:"currentArtifactID,omitempty"`
NewHash string `json:"newHash,omitempty"`
Actor Actor `json:"actor,omitempty"`
}
func (RollbackEvent) Type() string {
return "rollback"
}
func (p RollbackEvent) Marshal() ([]byte, error) {
return json.Marshal(p)
}
func (p *RollbackEvent) Unmarshal(data []byte) error {
return json.Unmarshal(data, p)
}
func (s *Service) ExecRollback(ctx context.Context, event RollbackEvent) error {
span, ctx := s.Tracer.FromCtx(ctx, "flow.ExecRollback")
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-rollback-source")
if err != nil {
return true, err
}
defer closeSource(ctx)
_, err = s.Git.Clone(ctx, sourceConfigRepoPath)
if err != nil {
return true, errors.WithMessagef(err, "clone into '%s'", sourceConfigRepoPath)
}
service := event.Service
environment := event.Environment
namespace := event.Namespace
currentSpecID := event.CurrentArtifactID
actor := event.Actor
newHash := plumbing.NewHash(event.NewHash)
err = s.Git.Checkout(ctx, sourceConfigRepoPath, newHash)
if err != nil {
return true, errors.WithMessagef(err, "checkout previous release hash '%v'", newHash)
}
destinationConfigRepoPath, closeDestination, err := git.TempDirAsync(ctx, s.Tracer, "k8s-config-rollback-destination")
if err != nil {
return true, err
}
defer closeDestination(ctx)
// copy current release artifacts into env
_, err = s.Git.Clone(ctx, destinationConfigRepoPath)
if err != nil {
return true, errors.WithMessagef(err, "clone destination repo into '%s'", destinationConfigRepoPath)
}
// release service to env from original release
sourcePath := releasePath(sourceConfigRepoPath, service, environment, namespace)
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 := path.Join(releasePath(sourceConfigRepoPath, service, environment, namespace), 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))
}
newSpec, err := envSpec(sourceConfigRepoPath, s.ArtifactFileName, service, environment, namespace)
if err != nil {
return true, errors.WithMessagef(err, "get spec of previous release hash '%v'", newHash)
}
authorName := newSpec.Application.AuthorName
authorEmail := newSpec.Application.AuthorEmail
releaseMessage := git.RollbackCommitMessage(environment, service, currentSpecID, newSpec.ID, 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: newSpec,
Releaser: actor.Name,
})
logger.Infof("flow: Rollback: rollback 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
}