-
Notifications
You must be signed in to change notification settings - Fork 141
/
planner.go
368 lines (324 loc) · 11.6 KB
/
planner.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
// Copyright 2020 The PipeCD 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 controller
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"time"
"go.uber.org/atomic"
"go.uber.org/zap"
"github.com/pipe-cd/pipecd/pkg/app/piped/deploysource"
"github.com/pipe-cd/pipecd/pkg/app/piped/metadatastore"
pln "github.com/pipe-cd/pipecd/pkg/app/piped/planner"
"github.com/pipe-cd/pipecd/pkg/app/piped/planner/registry"
"github.com/pipe-cd/pipecd/pkg/app/server/service/pipedservice"
"github.com/pipe-cd/pipecd/pkg/cache"
"github.com/pipe-cd/pipecd/pkg/config"
"github.com/pipe-cd/pipecd/pkg/model"
"github.com/pipe-cd/pipecd/pkg/regexpool"
)
// What planner does:
// - Wait until there is no PLANNED or RUNNING deployment
// - Pick the oldest PENDING deployment to plan its pipeline
// - Compare with the last successful commit
// - Decide the pipeline should be executed (scale, progressive, rollback)
// - Update the pipeline stages and change the deployment status to PLANNED
type planner struct {
// Readonly deployment model.
deployment *model.Deployment
envName string
lastSuccessfulCommitHash string
workingDir string
apiClient apiClient
gitClient gitClient
metadataStore metadatastore.MetadataStore
notifier notifier
secretDecrypter secretDecrypter
plannerRegistry registry.Registry
pipedConfig *config.PipedSpec
appManifestsCache cache.Cache
logger *zap.Logger
done atomic.Bool
doneTimestamp time.Time
doneDeploymentStatus model.DeploymentStatus
cancelled bool
cancelledCh chan *model.ReportableCommand
nowFunc func() time.Time
}
func newPlanner(
d *model.Deployment,
envName string,
lastSuccessfulCommitHash string,
workingDir string,
apiClient apiClient,
gitClient gitClient,
notifier notifier,
sd secretDecrypter,
pipedConfig *config.PipedSpec,
appManifestsCache cache.Cache,
logger *zap.Logger,
) *planner {
logger = logger.Named("planner").With(
zap.String("deployment-id", d.Id),
zap.String("app-id", d.ApplicationId),
zap.String("env-id", d.EnvId),
zap.String("project-id", d.ProjectId),
zap.String("app-kind", d.Kind.String()),
zap.String("working-dir", workingDir),
)
p := &planner{
deployment: d,
envName: envName,
lastSuccessfulCommitHash: lastSuccessfulCommitHash,
workingDir: workingDir,
apiClient: apiClient,
gitClient: gitClient,
metadataStore: metadatastore.NewMetadataStore(apiClient, d),
notifier: notifier,
secretDecrypter: sd,
pipedConfig: pipedConfig,
plannerRegistry: registry.DefaultRegistry(),
appManifestsCache: appManifestsCache,
doneDeploymentStatus: d.Status,
cancelledCh: make(chan *model.ReportableCommand, 1),
nowFunc: time.Now,
logger: logger,
}
return p
}
// ID returns the id of planner.
// This is the same value with deployment ID.
func (p *planner) ID() string {
return p.deployment.Id
}
// IsDone tells whether this planner is done it tasks or not.
// Returning true means this planner can be removable.
func (p *planner) IsDone() bool {
return p.done.Load()
}
// DoneTimestamp returns the time when planner has done.
func (p *planner) DoneTimestamp() time.Time {
return p.doneTimestamp
}
// DoneDeploymentStatus returns the deployment status when planner has done.
// This can be used only after IsDone() returns true.
func (p *planner) DoneDeploymentStatus() model.DeploymentStatus {
return p.doneDeploymentStatus
}
func (p *planner) Cancel(cmd model.ReportableCommand) {
if p.cancelled {
return
}
p.cancelled = true
p.cancelledCh <- &cmd
close(p.cancelledCh)
}
func (p *planner) Run(ctx context.Context) error {
p.logger.Info("start running planner")
defer func() {
p.doneTimestamp = p.nowFunc()
p.done.Store(true)
}()
repoCfg := config.PipedRepository{
RepoID: p.deployment.GitPath.Repo.Id,
Remote: p.deployment.GitPath.Repo.Remote,
Branch: p.deployment.GitPath.Repo.Branch,
}
in := pln.Input{
ApplicationID: p.deployment.ApplicationId,
ApplicationName: p.deployment.ApplicationName,
GitPath: *p.deployment.GitPath,
Trigger: *p.deployment.Trigger,
MostRecentSuccessfulCommitHash: p.lastSuccessfulCommitHash,
PipedConfig: p.pipedConfig,
AppManifestsCache: p.appManifestsCache,
RegexPool: regexpool.DefaultPool(),
GitClient: p.gitClient,
Logger: p.logger,
}
in.TargetDSP = deploysource.NewProvider(
filepath.Join(p.workingDir, "target-deploysource"),
deploysource.NewGitSourceCloner(p.gitClient, repoCfg, "target", p.deployment.Trigger.Commit.Hash),
*p.deployment.GitPath,
p.secretDecrypter,
)
if p.lastSuccessfulCommitHash != "" {
in.RunningDSP = deploysource.NewProvider(
filepath.Join(p.workingDir, "running-deploysource"),
deploysource.NewGitSourceCloner(p.gitClient, repoCfg, "running", p.lastSuccessfulCommitHash),
*p.deployment.GitPath,
p.secretDecrypter,
)
}
planner, ok := p.plannerRegistry.Planner(p.deployment.Kind)
if !ok {
p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_FAILURE
p.reportDeploymentFailed(ctx, "Unable to find the planner for this application kind")
return fmt.Errorf("unable to find the planner for application %v", p.deployment.Kind)
}
out, err := planner.Plan(ctx, in)
// If the deployment was already cancelled, we ignore the plan result.
select {
case cmd := <-p.cancelledCh:
if cmd != nil {
p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_CANCELLED
desc := fmt.Sprintf("Deployment was cancelled by %s while planning", cmd.Commander)
p.reportDeploymentCancelled(ctx, cmd.Commander, desc)
return cmd.Report(ctx, model.CommandStatus_COMMAND_SUCCEEDED, nil, nil)
}
default:
}
if err != nil {
p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_FAILURE
return p.reportDeploymentFailed(ctx, fmt.Sprintf("Unable to plan the deployment (%v)", err))
}
p.doneDeploymentStatus = model.DeploymentStatus_DEPLOYMENT_PLANNED
return p.reportDeploymentPlanned(ctx, p.lastSuccessfulCommitHash, out)
}
func (p *planner) reportDeploymentPlanned(ctx context.Context, runningCommitHash string, out pln.Output) error {
var (
err error
retry = pipedservice.NewRetry(10)
req = &pipedservice.ReportDeploymentPlannedRequest{
DeploymentId: p.deployment.Id,
Summary: out.Summary,
StatusReason: "The deployment has been planned",
RunningCommitHash: runningCommitHash,
Version: out.Version,
Stages: out.Stages,
DeploymentChainId: p.deployment.DeploymentChainId,
DeploymentChainBlockIndex: p.deployment.DeploymentChainBlockIndex,
}
)
accounts, err := p.getMentionedAccounts(model.NotificationEventType_EVENT_DEPLOYMENT_PLANNED)
if err != nil {
p.logger.Error("failed to get the list of accounts", zap.Error(err))
}
defer func() {
p.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_DEPLOYMENT_PLANNED,
Metadata: &model.NotificationEventDeploymentPlanned{
Deployment: p.deployment,
EnvName: p.envName,
Summary: out.Summary,
MentionedAccounts: accounts,
},
})
}()
for retry.WaitNext(ctx) {
if _, err = p.apiClient.ReportDeploymentPlanned(ctx, req); err == nil {
return nil
}
err = fmt.Errorf("failed to report deployment status to control-plane: %v", err)
}
if err != nil {
p.logger.Error("failed to mark deployment to be planned", zap.Error(err))
}
return err
}
func (p *planner) reportDeploymentFailed(ctx context.Context, reason string) error {
var (
err error
now = p.nowFunc()
req = &pipedservice.ReportDeploymentCompletedRequest{
DeploymentId: p.deployment.Id,
Status: model.DeploymentStatus_DEPLOYMENT_FAILURE,
StatusReason: reason,
StageStatuses: nil,
DeploymentChainId: p.deployment.DeploymentChainId,
DeploymentChainBlockIndex: p.deployment.DeploymentChainBlockIndex,
CompletedAt: now.Unix(),
}
retry = pipedservice.NewRetry(10)
)
accounts, err := p.getMentionedAccounts(model.NotificationEventType_EVENT_DEPLOYMENT_FAILED)
if err != nil {
p.logger.Error("failed to get the list of accounts", zap.Error(err))
}
defer func() {
p.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_DEPLOYMENT_FAILED,
Metadata: &model.NotificationEventDeploymentFailed{
Deployment: p.deployment,
EnvName: p.envName,
Reason: reason,
MentionedAccounts: accounts,
},
})
}()
for retry.WaitNext(ctx) {
if _, err = p.apiClient.ReportDeploymentCompleted(ctx, req); err == nil {
return nil
}
err = fmt.Errorf("failed to report deployment status to control-plane: %v", err)
}
if err != nil {
p.logger.Error("failed to mark deployment to be failed", zap.Error(err))
}
return err
}
func (p *planner) reportDeploymentCancelled(ctx context.Context, commander, reason string) error {
var (
err error
now = p.nowFunc()
req = &pipedservice.ReportDeploymentCompletedRequest{
DeploymentId: p.deployment.Id,
Status: model.DeploymentStatus_DEPLOYMENT_CANCELLED,
StatusReason: reason,
StageStatuses: nil,
DeploymentChainId: p.deployment.DeploymentChainId,
DeploymentChainBlockIndex: p.deployment.DeploymentChainBlockIndex,
CompletedAt: now.Unix(),
}
retry = pipedservice.NewRetry(10)
)
accounts, err := p.getMentionedAccounts(model.NotificationEventType_EVENT_DEPLOYMENT_CANCELLED)
if err != nil {
p.logger.Error("failed to get the list of accounts", zap.Error(err))
}
defer func() {
p.notifier.Notify(model.NotificationEvent{
Type: model.NotificationEventType_EVENT_DEPLOYMENT_CANCELLED,
Metadata: &model.NotificationEventDeploymentCancelled{
Deployment: p.deployment,
EnvName: p.envName,
Commander: commander,
MentionedAccounts: accounts,
},
})
}()
for retry.WaitNext(ctx) {
if _, err = p.apiClient.ReportDeploymentCompleted(ctx, req); err == nil {
return nil
}
err = fmt.Errorf("failed to report deployment status to control-plane: %v", err)
}
if err != nil {
p.logger.Error("failed to mark deployment to be cancelled", zap.Error(err))
}
return err
}
func (p *planner) getMentionedAccounts(event model.NotificationEventType) ([]string, error) {
n, ok := p.metadataStore.Shared().Get(model.MetadataKeyDeploymentNotification)
if !ok {
return []string{}, nil
}
var notification config.DeploymentNotification
if err := json.Unmarshal([]byte(n), ¬ification); err != nil {
return nil, fmt.Errorf("could not extract mentions config: %w", err)
}
return notification.FindSlackAccounts(event), nil
}