-
Notifications
You must be signed in to change notification settings - Fork 153
/
deployment.go
608 lines (542 loc) · 19.3 KB
/
deployment.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// 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 config
import (
"encoding/json"
"fmt"
"time"
"github.com/pipe-cd/pipe/pkg/model"
)
const (
defaultWaitApprovalTimeout = Duration(6 * time.Hour)
defaultAnalysisQueryTimeout = Duration(30 * time.Second)
allEventsSymbol = "*"
)
type GenericDeploymentSpec struct {
// Configuration used while planning deployment.
Planner DeploymentPlanner `json:"planner"`
// Forcibly use QuickSync or Pipeline when commit message matched the specified pattern.
CommitMatcher DeploymentCommitMatcher `json:"commitMatcher"`
// Pipeline for deploying progressively.
Pipeline *DeploymentPipeline `json:"pipeline"`
// List of directories or files where their changes will trigger the deployment.
// Regular expression can be used.
// Deprecated: use Trigger.Paths instead.
TriggerPaths []string `json:"triggerPaths,omitempty"`
// The trigger configuration use to determine trigger logic.
Trigger Trigger `json:"trigger"`
// Configuration to be used once the deployment is triggered successfully.
PostSync *PostSync `json:"postSync"`
// The maximum length of time to execute deployment before giving up.
// Default is 6h.
Timeout Duration `json:"timeout,omitempty" default:"6h"`
// List of encrypted secrets and targets that should be decoded before using.
Encryption *SecretEncryption `json:"encryption"`
// Additional configuration used while sending notification to external services.
DeploymentNotification *DeploymentNotification `json:"notification"`
}
type DeploymentPlanner struct {
// Disable auto-detecting to use QUICK_SYNC or PROGRESSIVE_SYNC.
// Always use the speficied pipeline for all deployments.
AlwaysUsePipeline bool `json:"alwaysUsePipeline"`
}
type Trigger struct {
// Configurable fields used while deciding the application
// should be triggered or not based on commit changes.
OnCommit OnCommit `json:"onCommit"`
// Configurable fields used while deciding the application
// should be triggered or not based on received SYNC command.
OnCommand OnCommand `json:"onCommand"`
// Configurable fields used while deciding the application
// should be triggered or not based on OUT_OF_SYNC state.
OnOutOfSync OnOutOfSync `json:"onOutOfSync"`
}
type OnCommit struct {
// Whether to exclude application from triggering target
// when a new commit touched the application.
// Default is false.
Disabled bool `json:"disabled,omitempty"`
// List of directories or files where their changes will trigger the deployment.
// Regular expression can be used.
Paths []string `json:"paths,omitempty"`
}
type OnCommand struct {
// Whether to exclude application from triggering target
// when received a new SYNC command.
// Default is false.
Disabled bool `json:"disabled,omitempty"`
}
type OnOutOfSync struct {
// Whether to exclude application from triggering target
// when application is at OUT_OF_SYNC state.
// Default is true.
Disabled *bool `json:"disabled,omitempty" default:"true"`
// TODO: Add a field to control the trigger frequency.
// MinWindow Duration `json:"minWindow,omitempty"`
}
func (s *GenericDeploymentSpec) Validate() error {
if s.Pipeline != nil {
for _, stage := range s.Pipeline.Stages {
if stage.AnalysisStageOptions != nil {
if err := stage.AnalysisStageOptions.Validate(); err != nil {
return err
}
}
}
}
if ps := s.PostSync; ps != nil {
if err := ps.Validate(); err != nil {
return err
}
}
if e := s.Encryption; e != nil {
if err := e.Validate(); err != nil {
return err
}
}
if s.DeploymentNotification != nil {
for _, m := range s.DeploymentNotification.Mentions {
if err := m.Validate(); err != nil {
return err
}
}
}
return nil
}
func (s GenericDeploymentSpec) GetStage(index int32) (PipelineStage, bool) {
if s.Pipeline == nil {
return PipelineStage{}, false
}
if int(index) >= len(s.Pipeline.Stages) {
return PipelineStage{}, false
}
return s.Pipeline.Stages[index], true
}
// HasStage checks if the given stage is included in the pipeline.
func (s GenericDeploymentSpec) HasStage(stage model.Stage) bool {
if s.Pipeline == nil {
return false
}
for _, s := range s.Pipeline.Stages {
if s.Name == stage {
return true
}
}
return false
}
// DeploymentCommitMatcher provides a way to decide how to deploy.
type DeploymentCommitMatcher struct {
// It makes sure to perform syncing if the commit message matches this regular expression.
QuickSync string `json:"quickSync"`
// It makes sure to perform pipeline if the commit message matches this regular expression.
Pipeline string `json:"pipeline"`
}
// DeploymentPipeline represents the way to deploy the application.
// The pipeline is triggered by changes in any of the following objects:
// - Target PodSpec (Target can be Deployment, DaemonSet, StatefulSet)
// - ConfigMaps, Secrets that are mounted as volumes or envs in the deployment.
type DeploymentPipeline struct {
Stages []PipelineStage `json:"stages"`
}
// PipelineStage represents a single stage of a pipeline.
// This is used as a generic struct for all stage type.
type PipelineStage struct {
Id string
Name model.Stage
Desc string
Timeout Duration
WaitStageOptions *WaitStageOptions
WaitApprovalStageOptions *WaitApprovalStageOptions
AnalysisStageOptions *AnalysisStageOptions
K8sPrimaryRolloutStageOptions *K8sPrimaryRolloutStageOptions
K8sCanaryRolloutStageOptions *K8sCanaryRolloutStageOptions
K8sCanaryCleanStageOptions *K8sCanaryCleanStageOptions
K8sBaselineRolloutStageOptions *K8sBaselineRolloutStageOptions
K8sBaselineCleanStageOptions *K8sBaselineCleanStageOptions
K8sTrafficRoutingStageOptions *K8sTrafficRoutingStageOptions
TerraformSyncStageOptions *TerraformSyncStageOptions
TerraformPlanStageOptions *TerraformPlanStageOptions
TerraformApplyStageOptions *TerraformApplyStageOptions
CloudRunSyncStageOptions *CloudRunSyncStageOptions
CloudRunPromoteStageOptions *CloudRunPromoteStageOptions
LambdaSyncStageOptions *LambdaSyncStageOptions
LambdaCanaryRolloutStageOptions *LambdaCanaryRolloutStageOptions
LambdaPromoteStageOptions *LambdaPromoteStageOptions
ECSSyncStageOptions *ECSSyncStageOptions
ECSCanaryRolloutStageOptions *ECSCanaryRolloutStageOptions
ECSPrimaryRolloutStageOptions *ECSPrimaryRolloutStageOptions
ECSCanaryCleanStageOptions *ECSCanaryCleanStageOptions
ECSTrafficRoutingStageOptions *ECSTrafficRoutingStageOptions
}
type genericPipelineStage struct {
Id string `json:"id"`
Name model.Stage `json:"name"`
Desc string `json:"desc,omitempty"`
Timeout Duration `json:"timeout"`
With json.RawMessage `json:"with"`
}
func (s *PipelineStage) UnmarshalJSON(data []byte) error {
var err error
gs := genericPipelineStage{}
if err = json.Unmarshal(data, &gs); err != nil {
return err
}
s.Id = gs.Id
s.Name = gs.Name
s.Desc = gs.Desc
s.Timeout = gs.Timeout
switch s.Name {
case model.StageWait:
s.WaitStageOptions = &WaitStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.WaitStageOptions)
}
case model.StageWaitApproval:
s.WaitApprovalStageOptions = &WaitApprovalStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.WaitApprovalStageOptions)
}
if s.WaitApprovalStageOptions.Timeout <= 0 {
s.WaitApprovalStageOptions.Timeout = defaultWaitApprovalTimeout
}
case model.StageAnalysis:
s.AnalysisStageOptions = &AnalysisStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.AnalysisStageOptions)
}
for i := 0; i < len(s.AnalysisStageOptions.Metrics); i++ {
if s.AnalysisStageOptions.Metrics[i].Timeout <= 0 {
s.AnalysisStageOptions.Metrics[i].Timeout = defaultAnalysisQueryTimeout
}
}
case model.StageK8sPrimaryRollout:
s.K8sPrimaryRolloutStageOptions = &K8sPrimaryRolloutStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.K8sPrimaryRolloutStageOptions)
}
case model.StageK8sCanaryRollout:
s.K8sCanaryRolloutStageOptions = &K8sCanaryRolloutStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.K8sCanaryRolloutStageOptions)
}
case model.StageK8sCanaryClean:
s.K8sCanaryCleanStageOptions = &K8sCanaryCleanStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.K8sCanaryCleanStageOptions)
}
case model.StageK8sBaselineRollout:
s.K8sBaselineRolloutStageOptions = &K8sBaselineRolloutStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.K8sBaselineRolloutStageOptions)
}
case model.StageK8sBaselineClean:
s.K8sBaselineCleanStageOptions = &K8sBaselineCleanStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.K8sBaselineCleanStageOptions)
}
case model.StageK8sTrafficRouting:
s.K8sTrafficRoutingStageOptions = &K8sTrafficRoutingStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.K8sTrafficRoutingStageOptions)
}
case model.StageTerraformSync:
s.TerraformSyncStageOptions = &TerraformSyncStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.TerraformSyncStageOptions)
}
case model.StageTerraformPlan:
s.TerraformPlanStageOptions = &TerraformPlanStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.TerraformPlanStageOptions)
}
case model.StageTerraformApply:
s.TerraformApplyStageOptions = &TerraformApplyStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.TerraformApplyStageOptions)
}
case model.StageCloudRunSync:
s.CloudRunSyncStageOptions = &CloudRunSyncStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.CloudRunSyncStageOptions)
}
case model.StageCloudRunPromote:
s.CloudRunPromoteStageOptions = &CloudRunPromoteStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.CloudRunPromoteStageOptions)
}
case model.StageLambdaSync:
s.LambdaSyncStageOptions = &LambdaSyncStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.LambdaSyncStageOptions)
}
case model.StageLambdaPromote:
s.LambdaPromoteStageOptions = &LambdaPromoteStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.LambdaPromoteStageOptions)
}
case model.StageLambdaCanaryRollout:
s.LambdaCanaryRolloutStageOptions = &LambdaCanaryRolloutStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.LambdaCanaryRolloutStageOptions)
}
case model.StageECSSync:
s.ECSSyncStageOptions = &ECSSyncStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.ECSSyncStageOptions)
}
case model.StageECSCanaryRollout:
s.ECSCanaryRolloutStageOptions = &ECSCanaryRolloutStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.ECSCanaryRolloutStageOptions)
}
case model.StageECSPrimaryRollout:
s.ECSPrimaryRolloutStageOptions = &ECSPrimaryRolloutStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.ECSPrimaryRolloutStageOptions)
}
case model.StageECSCanaryClean:
s.ECSCanaryCleanStageOptions = &ECSCanaryCleanStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.ECSCanaryCleanStageOptions)
}
case model.StageECSTrafficRouting:
s.ECSTrafficRoutingStageOptions = &ECSTrafficRoutingStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.ECSTrafficRoutingStageOptions)
}
default:
err = fmt.Errorf("unsupported stage name: %s", s.Name)
}
return err
}
// WaitStageOptions contains all configurable values for a WAIT stage.
type WaitStageOptions struct {
Duration Duration `json:"duration"`
}
// WaitStageOptions contains all configurable values for a WAIT_APPROVAL stage.
type WaitApprovalStageOptions struct {
// The maximum length of time to wait before giving up.
// Defaults to 6h.
Timeout Duration `json:"timeout"`
Approvers []string `json:"approvers"`
}
// AnalysisStageOptions contains all configurable values for a K8S_ANALYSIS stage.
type AnalysisStageOptions struct {
// How long the analysis process should be executed.
Duration Duration `json:"duration"`
// TODO: Consider about how to handle a pod restart
// possible count of pod restarting
RestartThreshold int `json:"restartThreshold"`
Metrics []TemplatableAnalysisMetrics `json:"metrics"`
Logs []TemplatableAnalysisLog `json:"logs"`
Https []TemplatableAnalysisHTTP `json:"https"`
}
func (a *AnalysisStageOptions) Validate() error {
if a.Duration == 0 {
return fmt.Errorf("the ANALYSIS stage requires duration field")
}
for _, m := range a.Metrics {
if m.Template.Name != "" {
if err := m.Template.Validate(); err != nil {
return fmt.Errorf("one of metrics configurations of ANALYSIS stage is invalid: %w", err)
}
continue
}
if err := m.AnalysisMetrics.Validate(); err != nil {
return fmt.Errorf("one of metrics configurations of ANALYSIS stage is invalid: %w", err)
}
}
for _, l := range a.Logs {
if l.Template.Name != "" {
if err := l.Template.Validate(); err != nil {
return fmt.Errorf("one of log configurations of ANALYSIS stage is invalid: %w", err)
}
continue
}
if err := l.AnalysisLog.Validate(); err != nil {
return fmt.Errorf("one of log configurations of ANALYSIS stage is invalid: %w", err)
}
}
for _, h := range a.Https {
if h.Template.Name != "" {
if err := h.Template.Validate(); err != nil {
return fmt.Errorf("one of http configurations of ANALYSIS stage is invalid: %w", err)
}
continue
}
if err := h.AnalysisHTTP.Validate(); err != nil {
return fmt.Errorf("one of http configurations of ANALYSIS stage is invalid: %w", err)
}
}
return nil
}
type AnalysisTemplateRef struct {
Name string `json:"name"`
AppArgs map[string]string `json:"appArgs"`
}
func (a *AnalysisTemplateRef) Validate() error {
if a.Name == "" {
return fmt.Errorf("the reference of analysis template name is empty")
}
return nil
}
// TemplatableAnalysisMetrics wraps AnalysisMetrics to allow specify template to use.
type TemplatableAnalysisMetrics struct {
AnalysisMetrics
Template AnalysisTemplateRef `json:"template"`
}
// TemplatableAnalysisLog wraps AnalysisLog to allow specify template to use.
type TemplatableAnalysisLog struct {
AnalysisLog
Template AnalysisTemplateRef `json:"template"`
}
// TemplatableAnalysisHTTP wraps AnalysisHTTP to allow specify template to use.
type TemplatableAnalysisHTTP struct {
AnalysisHTTP
Template AnalysisTemplateRef `json:"template"`
}
type SealedSecretMapping struct {
// Relative path from the application directory to sealed secret file.
Path string `json:"path"`
// The filename for the decrypted secret.
// Empty means the same name with the sealed secret file.
OutFilename string `json:"outFilename"`
// The directory name where to put the decrypted secret.
// Empty means the same directory with the sealed secret file.
OutDir string `json:"outDir"`
}
type SecretEncryption struct {
// List of encrypted secrets.
EncryptedSecrets map[string]string `json:"encryptedSecrets"`
// List of files to be decrypted before using.
DecryptionTargets []string `json:"decryptionTargets"`
}
func (e *SecretEncryption) Validate() error {
for k, v := range e.EncryptedSecrets {
if k == "" {
return fmt.Errorf("key field in encryptedSecrets must not be empty")
}
if v == "" {
return fmt.Errorf("value field of %s in encryptedSecrets must not be empty", k)
}
}
return nil
}
// DeploymentNotification represents the way to send to users.
type DeploymentNotification struct {
// List of users to be notified for each event.
Mentions []NotificationMention `json:"mentions"`
}
func (n *DeploymentNotification) FindSlackAccounts(event model.NotificationEventType) []string {
as := make(map[string]struct{})
for _, m := range n.Mentions {
if m.Event != allEventsSymbol && "EVENT_"+m.Event != event.String() {
continue
}
for _, s := range m.Slack {
as[s] = struct{}{}
}
}
approvers := make([]string, 0, len(as))
for a := range as {
approvers = append(approvers, a)
}
return approvers
}
type NotificationMention struct {
// The event to be notified to users.
Event string `json:"event"`
// List of user IDs for mentioning in Slack.
// See https://api.slack.com/reference/surfaces/formatting#mentioning-users
// for more information on how to check them.
Slack []string `json:"slack"`
// TODO: Support for email notification
// The email for notification.
Email []string `json:"email"`
}
func (n *NotificationMention) Validate() error {
if n.Event == allEventsSymbol {
return nil
}
e := "EVENT_" + n.Event
for k := range model.NotificationEventType_value {
if e == k {
return nil
}
}
return fmt.Errorf("event %q is incorrect as NotificationEventType", n.Event)
}
// PostSync provides all configurations to be used once the current deployment
// is triggered successfully.
type PostSync struct {
DeploymentChain *DeploymentChain `json:"chain"`
}
func (p *PostSync) Validate() error {
if dc := p.DeploymentChain; dc != nil {
return dc.Validate()
}
return nil
}
// DeploymentChain provides all configurations used to trigger a chain of deployments.
type DeploymentChain struct {
// Nodes provides list of DeploymentChainNodes which contain filters to be used
// to find applications to deploy as chain node. It's required to not empty.
Nodes []*DeploymentChainNode `json:"applications"`
// Conditions provides configuration used to determine should the piped in charge in
// the first applications in the chain trigger a whole new deployment chain or not.
// If this field is not set, always trigger a whole new deployment chain when the current
// application is triggered.
// TODO: Add conditions to deployment chain configuration.
// Conditions *DeploymentChainTriggerCondition `json:"conditions,omitempty"`
}
func (dc *DeploymentChain) Validate() error {
if len(dc.Nodes) == 0 {
return fmt.Errorf("missing specified applications that will be triggered on this chain of deployment")
}
for _, n := range dc.Nodes {
if err := n.Validate(); err != nil {
return err
}
}
// if cc := dc.Conditions; cc != nil {
// if err := cc.Validate(); err != nil {
// return err
// }
// }
return nil
}
// DeploymentChainNode provides filters used to find the right applications to trigger
// as a part of the deployment chain.
type DeploymentChainNode struct {
AppName string `json:"name"`
AppKind string `json:"kind"`
AppLabels map[string]string `json:"labels"`
}
func (n *DeploymentChainNode) Validate() error {
hasFilterCond := n.AppName != "" || n.AppKind != "" || len(n.AppLabels) != 0
if !hasFilterCond {
return fmt.Errorf("at least one of \"name\", \"kind\" or \"labels\" must be set to find applications to deploy")
}
return nil
}
type DeploymentChainTriggerCondition struct {
CommitPrefix string `json:"commitPrefix"`
}
func (c *DeploymentChainTriggerCondition) Validate() error {
hasCond := c.CommitPrefix != ""
if !hasCond {
return fmt.Errorf("missing commitPrefix configration as deployment chain trigger condition")
}
return nil
}