-
Notifications
You must be signed in to change notification settings - Fork 139
/
deployment.go
420 lines (371 loc) · 14.7 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
// 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"
"github.com/pipe-cd/pipe/pkg/model"
)
type Pipelineable interface {
GetStage(index int32) (PipelineStage, bool)
}
// KubernetesDeploymentSpec represents a deployment configuration for Kubernetes application.
type KubernetesDeploymentSpec struct {
Input KubernetesDeploymentInput `json:"input"`
CommitMatcher DeploymentCommitMatcher `json:"commitMatcher"`
Sync K8sSyncStageOptions `json:"sync"`
Pipeline *DeploymentPipeline `json:"pipeline"`
// Which resource should be considered as the Service of application.
// Empty means the first Service resource will be used.
Service K8sResourceReference `json:"service"`
// Which resources should be considered as the Workload of application.
// Empty means all Deployments.
// e.g. "Deployment/deployment-name", "ReplicaSet/replicaset-name"
Workloads []K8sResourceReference `json:"workloads"`
// Which method should be used for traffic routing.
TrafficRouting *TrafficRouting `json:"trafficRouting"`
}
func (s *KubernetesDeploymentSpec) 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
}
// Validate returns an error if any wrong configuration value was found.
func (s *KubernetesDeploymentSpec) Validate() error {
return nil
}
// TerraformDeploymentSpec represents a deployment configuration for Terraform application.
type TerraformDeploymentSpec struct {
Input TerraformDeploymentInput `json:"input"`
Pipeline *DeploymentPipeline `json:"pipeline"`
}
func (s *TerraformDeploymentSpec) 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
}
// Validate returns an error if any wrong configuration value was found.
func (s *TerraformDeploymentSpec) Validate() error {
return nil
}
// 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.
Sync string `json:"sync"`
// 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, StatefullSet)
// - ConfigMaps, Secrets that are mounted as volumes or envs in the deployment.
type DeploymentPipeline struct {
Stages []PipelineStage `json:"stages"`
}
type TrafficRoutingMethod string
const (
TrafficRoutingMethodPodSelector TrafficRoutingMethod = "podselector"
TrafficRoutingMethodIstio TrafficRoutingMethod = "istio"
)
type TrafficRouting struct {
Method TrafficRoutingMethod `json:"method"`
Istio *IstioTrafficRouting `json:"istio"`
}
// DetermineTrafficRoutingMethod determines the routing method should be used based on the TrafficRouting config.
// The default is PodSelector: the way by updating the selector in Service to switching all of traffic.
func DetermineTrafficRoutingMethod(cfg *TrafficRouting) TrafficRoutingMethod {
if cfg == nil {
return TrafficRoutingMethodPodSelector
}
if cfg.Method == "" {
return TrafficRoutingMethodPodSelector
}
return cfg.Method
}
type IstioTrafficRouting struct {
EditableRoutes []string `json:"editableRoutes"`
// TODO: Add a validate to ensure this was configured or using the default value by service name.
Host string `json:"host"`
VirtualService K8sResourceReference `json:"virtualService"`
}
type K8sResourceReference struct {
Kind string `json:"kind"`
Name string `json:"name"`
}
// PiplineStage 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
TerraformPlanStageOptions *TerraformPlanStageOptions
TerraformApplyStageOptions *TerraformApplyStageOptions
}
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)
}
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.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)
}
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 {
Approvers []string `json:"approvers"`
}
// K8sSyncStageOptions contains all configurable values for a K8S_SYNC stage.
type K8sSyncStageOptions struct {
// Whether the PRIMARY variant label should be added to manifests if they were missing.
AddVariantLabelToSelector bool `json:"addVariantLabelToSelector"`
// Whether the resources that are no longer defined in Git will be removed.
Prune bool `json:"prune"`
}
// K8sPrimaryRolloutStageOptions contains all configurable values for a K8S_PRIMARY_ROLLOUT stage.
type K8sPrimaryRolloutStageOptions struct {
// Suffix that should be used when naming the PRIMARY variant's resources.
// Default is "primary".
Suffix string `json:"suffix"`
// Whether the PRIMARY service should be created.
CreateService bool `json:"createService"`
// Whether the PRIMARY variant label should be added to manifests if they were missing.
AddVariantLabelToSelector bool `json:"addVariantLabelToSelector"`
// Whether the resources that are no longer defined in Git will be removed.
Prune bool `json:"prune"`
}
// K8sCanaryRolloutStageOptions contains all configurable values for a K8S_CANARY_ROLLOUT stage.
type K8sCanaryRolloutStageOptions struct {
// How many pods for CANARY workloads.
// An integer value can be specified to indicate an absolute value of pod number.
// Or a string suffixed by "%" to indicate an percentage value compared to the pod number of PRIMARY.
// Default is 1 pod.
Replicas Replicas `json:"replicas"`
// Suffix that should be used when naming the CANARY variant's resources.
// Default is "canary".
Suffix string `json:"suffix"`
// Whether the CANARY service should be created.
CreateService bool `json:"createService"`
}
// K8sCanaryCleanStageOptions contains all configurable values for a K8S_CANARY_CLEAN stage.
type K8sCanaryCleanStageOptions struct {
}
// K8sBaselineRolloutStageOptions contains all configurable values for a K8S_BASELINE_ROLLOUT stage.
type K8sBaselineRolloutStageOptions struct {
// How many pods for BASELINE workloads.
// An integer value can be specified to indicate an absolute value of pod number.
// Or a string suffixed by "%" to indicate an percentage value compared to the pod number of PRIMARY.
// Default is 1 pod.
Replicas Replicas `json:"replicas"`
// Suffix that should be used when naming the BASELINE variant's resources.
// Default is "baseline".
Suffix string `json:"suffix"`
// Whether the BASELINE service should be created.
CreateService bool `json:"createService"`
}
// K8sBaselineCleanStageOptions contains all configurable values for a K8S_BASELINE_CLEAN stage.
type K8sBaselineCleanStageOptions struct {
}
// K8sTrafficRoutingStageOptions contains all configurable values for a K8S_TRAFFIC_ROUTING stage.
type K8sTrafficRoutingStageOptions struct {
// Which variant should receive all traffic.
All string `json:"all"`
// The percentage of traffic should be routed to PRIMARY variant.
Primary int `json:"primary"`
// The percentage of traffic should be routed to CANARY variant.
Canary int `json:"canary"`
// The percentage of traffic should be routed to BASELINE variant.
Baseline int `json:"baseline"`
}
func (opts K8sTrafficRoutingStageOptions) Percentages() (primary, canary, baseline int) {
switch opts.All {
case "primary":
return 100, 0, 0
case "canary":
return 0, 100, 0
case "baseline":
return 0, 0, 100
}
return opts.Primary, opts.Canary, opts.Baseline
}
// TerraformPlanStageOptions contains all configurable values for a K8S_TERRAFORM_PLAN stage.
type TerraformPlanStageOptions struct {
}
// TerraformApplyStageOptions contains all configurable values for a K8S_TERRAFORM_APPLY stage.
type TerraformApplyStageOptions struct {
// How many times to retry applying terraform changes.
Retries int `json:"retries"`
}
// 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"`
Dynamic AnalysisDynamic `json:"dynamic"`
}
type AnalysisTemplateRef struct {
Name string `json:"name"`
Args map[string]string `json:"args"`
}
// 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 KubernetesDeploymentInput struct {
Manifests []string `json:"manifests"`
KubectlVersion string `json:"kubectlVersion"`
KustomizeVersion string `json:"kustomizeVersion"`
HelmChart *InputHelmChart `json:"helmChart"`
HelmOptions *InputHelmOptions `json:"helmOptions"`
HelmVersion string `json:"helmVersion"`
// The namespace where manifests will be applied.
Namespace string `json:"namespace"`
// Automatically reverts all changes from all stages when one of them failed.
// Default is true.
AutoRollback bool `json:"autoRollback"`
Dependencies []string `json:"dependencies,omitempty"`
}
type TerraformDeploymentInput struct {
Workspace string `json:"workspace,omitempty"`
TerraformVersion string `json:"terraformVersion,omitempty"`
// Automatically reverts all changes from all stages when one of them failed.
// Default is false.
AutoRollback bool `json:"autoRollback"`
Dependencies []string `json:"dependencies,omitempty"`
}
type InputHelmChart struct {
// Empty means current repository.
GitRemote string `json:"gitRemote"`
// The commit SHA or tag for remote git.
Ref string `json:"ref"`
// Relative path from the repository root directory to the chart directory.
Path string `json:"path"`
// The name of an added Helm chart repository.
Repository string `json:"repository"`
Name string `json:"name"`
Version string `json:"version"`
}
type InputHelmOptions struct {
// By default the release name is equal to the application name.
ReleaseName string `json:"releaseName"`
// List of value files should be loaded.
ValueFiles []string `json:"valueFiles"`
SetFiles map[string]string
}