-
Notifications
You must be signed in to change notification settings - Fork 153
/
application.go
716 lines (636 loc) · 22.4 KB
/
application.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
// Copyright 2023 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"
"os"
"path/filepath"
"strings"
"github.com/pipe-cd/pipecd/pkg/model"
)
const allEventsSymbol = "*"
type GenericApplicationSpec struct {
// The application name.
// This is required if you set the application through the application configuration file.
Name string `json:"name"`
// Additional attributes to identify applications.
Labels map[string]string `json:"labels"`
// Notes on the Application.
Description string `json:"description"`
// 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"`
// 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"`
// List of files that should be attached to application manifests before using.
Attachment *Attachment `json:"attachment"`
// Additional configuration used while sending notification to external services.
DeploymentNotification *DeploymentNotification `json:"notification"`
// List of the configuration for event watcher.
EventWatcher []EventWatcherConfig `json:"eventWatcher"`
// Configuration for drift detection
DriftDetection *DriftDetection `json:"driftDetection"`
}
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"`
// Configurable fields used while deciding the application
// should be triggered based on received CHAIN_SYNC command.
OnChain OnChain `json:"onChain"`
}
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"`
// List of directories or files where their changes will be ignored.
// Regular expression can be used.
Ignores []string `json:"ignores,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"`
// Minimum amount of time must be elapsed since the last deployment.
// This can be used to avoid triggering unnecessary continuous deployments based on OUT_OF_SYNC status.
MinWindow Duration `json:"minWindow,omitempty" default:"5m"`
}
type OnChain struct {
// Whether to exclude application from triggering target
// when received a new CHAIN_SYNC command.
// Default is true.
Disabled *bool `json:"disabled,omitempty" default:"true"`
}
func (s *GenericApplicationSpec) 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 stage.WaitApprovalStageOptions != nil {
if err := stage.WaitApprovalStageOptions.Validate(); err != nil {
return err
}
}
if stage.CustomSyncOptions != nil {
if err := stage.CustomSyncOptions.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 am := s.Attachment; am != nil {
if err := am.Validate(); err != nil {
return err
}
}
if s.DeploymentNotification != nil {
for _, m := range s.DeploymentNotification.Mentions {
if err := m.Validate(); err != nil {
return err
}
}
}
if dd := s.DriftDetection; dd != nil {
if err := dd.Validate(); err != nil {
return err
}
}
return nil
}
func (s GenericApplicationSpec) 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 GenericApplicationSpec) 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
CustomSyncOptions *CustomSyncOptions
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.StageCustomSync:
s.CustomSyncOptions = &CustomSyncOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.CustomSyncOptions)
}
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)
}
case model.StageAnalysis:
s.AnalysisStageOptions = &AnalysisStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.AnalysisStageOptions)
}
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" default:"6h"`
Approvers []string `json:"approvers"`
MinApproverNum int `json:"minApproverNum" default:"1"`
}
func (w *WaitApprovalStageOptions) Validate() error {
if w.MinApproverNum < 1 {
return fmt.Errorf("minApproverNum %d should be greater than 0", w.MinApproverNum)
}
return nil
}
type CustomSyncOptions struct {
Timeout Duration `json:"timeout" default:"6h"`
Envs map[string]string `json:"envs"`
Run string `json:"run"`
}
func (c *CustomSyncOptions) Validate() error {
if c.Run == "" {
return fmt.Errorf("the CUSTOM_SYNC stage requires run field")
}
return nil
}
// 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 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
}
type Attachment struct {
// Map of name to refer with the file path which contain embedding source data.
Sources map[string]string `json:"sources"`
// List of files to be embedded before using.
Targets []string `json:"targets"`
}
func (e *Attachment) Validate() error {
for k, v := range e.Sources {
if k == "" {
return fmt.Errorf("key field in sources must not be empty")
}
if v == "" {
return fmt.Errorf("value field in sources must not be empty")
}
}
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 {
// ApplicationMatchers provides list of ChainApplicationMatcher which contain filters to be used
// to find applications to deploy as chain node. It's required to not empty.
ApplicationMatchers []ChainApplicationMatcher `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.ApplicationMatchers) == 0 {
return fmt.Errorf("missing specified applications that will be triggered on this chain of deployment")
}
for _, m := range dc.ApplicationMatchers {
if err := m.Validate(); err != nil {
return err
}
}
// if cc := dc.Conditions; cc != nil {
// if err := cc.Validate(); err != nil {
// return err
// }
// }
return nil
}
// ChainApplicationMatcher provides filters used to find the right applications to trigger
// as a part of the deployment chain.
type ChainApplicationMatcher struct {
Name string `json:"name"`
Kind string `json:"kind"`
Labels map[string]string `json:"labels"`
}
func (m *ChainApplicationMatcher) Validate() error {
hasFilterCond := m.Name != "" || m.Kind != "" || len(m.Labels) != 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
}
type DriftDetection struct {
// IgnoreFields are a list of 'apiVersion:kind:namespace:name#fieldPath'
IgnoreFields []string `json:"ignoreFields"`
}
func (dd *DriftDetection) Validate() error {
for _, ignoreField := range dd.IgnoreFields {
splited := strings.Split(ignoreField, "#")
if len(splited) != 2 {
return fmt.Errorf("ignoreFields must be in the form of 'apiVersion:kind:namespace:name#fieldPath'")
}
}
return nil
}
func LoadApplication(repoPath, configRelPath string, appKind model.ApplicationKind) (*GenericApplicationSpec, error) {
var absPath = filepath.Join(repoPath, configRelPath)
cfg, err := LoadFromYAML(absPath)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("application config file %s was not found in Git", configRelPath)
}
return nil, err
}
if kind, ok := cfg.Kind.ToApplicationKind(); !ok || kind != appKind {
return nil, fmt.Errorf("invalid application kind in the application config file, got: %s, expected: %s", kind, appKind)
}
spec, ok := cfg.GetGenericApplication()
if !ok {
return nil, fmt.Errorf("unsupported application kind: %s", appKind)
}
return &spec, nil
}