-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathdeployment.go
More file actions
504 lines (444 loc) · 20 KB
/
deployment.go
File metadata and controls
504 lines (444 loc) · 20 KB
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
// Copyright 2025 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 sdk
import (
"context"
"encoding/json"
"fmt"
"time"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
config "github.com/pipe-cd/pipecd/pkg/configv1"
"github.com/pipe-cd/pipecd/pkg/model"
"github.com/pipe-cd/pipecd/pkg/plugin/api/v1alpha1/deployment"
"github.com/pipe-cd/pipecd/pkg/plugin/logpersister"
"github.com/pipe-cd/pipecd/pkg/plugin/pipedapi"
"github.com/pipe-cd/pipecd/pkg/plugin/signalhandler"
)
var (
deploymentServiceServer interface {
Plugin
Register(server *grpc.Server)
setCommonFields(commonFields)
setConfig([]byte) error
deployment.DeploymentServiceServer
}
)
// DeployTargetsNone is a type alias for a slice of pointers to DeployTarget
// with an empty struct as the generic type parameter. It represents a case
// where there are no deployment targets.
// This utility is defined for plugins which has no deploy targets handling in ExecuteStage.
type DeployTargetsNone = []*DeployTarget[struct{}]
// ConfigNone is a type alias for a pointer to a struct with an empty struct as the generic type parameter.
// This utility is defined for plugins which has no config handling in ExecuteStage.
type ConfigNone = *struct{}
// DeploymentPlugin is the interface that be implemented by a full-spec deployment plugin.
// This kind of plugin should implement all methods to manage resources and execute stages.
// The Config and DeployTargetConfig are the plugin's config defined in piped's config.
type DeploymentPlugin[Config, DeployTargetConfig any] interface {
StagePlugin[Config, DeployTargetConfig]
// DetermineVersions determines the versions of the resources that will be deployed.
DetermineVersions(context.Context, *Config, *Client, TODO) (TODO, error)
// DetermineStrategy determines the strategy to deploy the resources.
DetermineStrategy(context.Context, *Config, *Client, TODO) (TODO, error)
// BuildQuickSyncStages builds the stages that will be executed during the quick sync process.
BuildQuickSyncStages(context.Context, *Config, *Client, TODO) (TODO, error)
}
// StagePlugin is the interface implemented by a plugin that focuses on executing generic stages.
// This kind of plugin may not implement quick sync stages.
// The Config and DeployTargetConfig are the plugin's config defined in piped's config.
type StagePlugin[Config, DeployTargetConfig any] interface {
Plugin
// FetchDefinedStages returns the list of stages that the plugin can execute.
FetchDefinedStages() []string
// BuildPipelineSyncStages builds the stages that will be executed by the plugin.
BuildPipelineSyncStages(context.Context, *Config, *BuildPipelineSyncStagesInput) (*BuildPipelineSyncStagesResponse, error)
// ExecuteStage executes the given stage.
ExecuteStage(context.Context, *Config, []*DeployTarget[DeployTargetConfig], *ExecuteStageInput) (*ExecuteStageResponse, error)
}
// DeployTarget defines the deploy target configuration for the piped.
type DeployTarget[Config any] struct {
// The name of the deploy target.
Name string `json:"name"`
// The labes of the deploy target.
Labels map[string]string `json:"labels,omitempty"`
// The configuration of the deploy target.
Config Config `json:"config"`
}
// RegisterDeploymentPlugin registers the given deployment plugin.
// It will be used when running the piped.
func RegisterDeploymentPlugin[Config, DeployTargetConfig any](plugin DeploymentPlugin[Config, DeployTargetConfig]) {
deploymentServiceServer = &DeploymentPluginServiceServer[Config, DeployTargetConfig]{base: plugin}
}
// RegisterStagePlugin registers the given stage plugin.
// It will be used when running the piped.
func RegisterStagePlugin[Config, DeployTargetConfig any](plugin StagePlugin[Config, DeployTargetConfig]) {
deploymentServiceServer = &StagePluginServiceServer[Config, DeployTargetConfig]{base: plugin}
}
type logPersister interface {
StageLogPersister(deploymentID, stageID string) logpersister.StageLogPersister
}
type commonFields struct {
config *config.PipedPlugin
logger *zap.Logger
logPersister logPersister
client *pipedapi.PipedServiceClient
}
// DeploymentPluginServiceServer is the gRPC server that handles requests from the piped.
type DeploymentPluginServiceServer[Config, DeployTargetConfig any] struct {
deployment.UnimplementedDeploymentServiceServer
commonFields
base DeploymentPlugin[Config, DeployTargetConfig]
config Config
}
// Name returns the name of the plugin.
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) Name() string {
return s.base.Name()
}
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) Version() string {
return s.base.Version()
}
// Register registers the server to the given gRPC server.
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) Register(server *grpc.Server) {
deployment.RegisterDeploymentServiceServer(server, s)
}
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) setCommonFields(fields commonFields) {
s.commonFields = fields
}
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) setConfig(bytes []byte) error {
if bytes == nil {
return nil
}
if err := json.Unmarshal(bytes, &s.config); err != nil {
return fmt.Errorf("failed to unmarshal the plugin config: %v", err)
}
return nil
}
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {
return &deployment.FetchDefinedStagesResponse{Stages: s.base.FetchDefinedStages()}, nil
}
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DetermineVersions not implemented")
}
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DetermineStrategy not implemented")
}
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) BuildPipelineSyncStages(ctx context.Context, request *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
client := &Client{
base: s.client,
pluginName: s.Name(),
}
return buildPipelineSyncStages(ctx, s.base, &s.config, client, request, s.logger)
}
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method BuildQuickSyncStages not implemented")
}
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) ExecuteStage(ctx context.Context, request *deployment.ExecuteStageRequest) (response *deployment.ExecuteStageResponse, _ error) {
lp := s.logPersister.StageLogPersister(request.GetInput().GetDeployment().GetId(), request.GetInput().GetStage().GetId())
defer func() {
// When termination signal received and the stage is not completed yet, we should not mark the log persister as completed.
// This can occur when the piped is shutting down while the stage is still running.
if !response.GetStatus().IsCompleted() && signalhandler.Terminated() {
return
}
lp.Complete(time.Minute)
}()
client := &Client{
base: s.client,
pluginName: s.Name(),
applicationID: request.GetInput().GetDeployment().GetApplicationId(),
deploymentID: request.GetInput().GetDeployment().GetId(),
stageID: request.GetInput().GetStage().GetId(),
logPersister: lp,
}
return executeStage(ctx, s.base, &s.config, nil, client, request, s.logger) // TODO: pass the deployTargets
}
// StagePluginServiceServer is the gRPC server that handles requests from the piped.
type StagePluginServiceServer[Config, DeployTargetConfig any] struct {
deployment.UnimplementedDeploymentServiceServer
commonFields
base StagePlugin[Config, DeployTargetConfig]
config Config
}
// Name returns the name of the plugin.
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) Name() string {
return s.base.Name()
}
// Version returns the version of the plugin.
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) Version() string {
return s.base.Version()
}
// Register registers the server to the given gRPC server.
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) Register(server *grpc.Server) {
deployment.RegisterDeploymentServiceServer(server, s)
}
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) setCommonFields(fields commonFields) {
s.commonFields = fields
}
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) setConfig(bytes []byte) error {
if bytes == nil {
return nil
}
if err := json.Unmarshal(bytes, &s.config); err != nil {
return fmt.Errorf("failed to unmarshal the plugin config: %v", err)
}
return nil
}
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) FetchDefinedStages(context.Context, *deployment.FetchDefinedStagesRequest) (*deployment.FetchDefinedStagesResponse, error) {
return &deployment.FetchDefinedStagesResponse{Stages: s.base.FetchDefinedStages()}, nil
}
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) DetermineVersions(context.Context, *deployment.DetermineVersionsRequest) (*deployment.DetermineVersionsResponse, error) {
return &deployment.DetermineVersionsResponse{}, nil
}
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) DetermineStrategy(context.Context, *deployment.DetermineStrategyRequest) (*deployment.DetermineStrategyResponse, error) {
return &deployment.DetermineStrategyResponse{Unsupported: true}, nil
}
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) BuildPipelineSyncStages(ctx context.Context, request *deployment.BuildPipelineSyncStagesRequest) (*deployment.BuildPipelineSyncStagesResponse, error) {
client := &Client{
base: s.client,
pluginName: s.Name(),
}
return buildPipelineSyncStages(ctx, s.base, &s.config, client, request, s.logger) // TODO: pass the real client
}
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method BuildQuickSyncStages not implemented")
}
func (s *StagePluginServiceServer[Config, DeployTargetConfig]) ExecuteStage(ctx context.Context, request *deployment.ExecuteStageRequest) (response *deployment.ExecuteStageResponse, _ error) {
lp := s.logPersister.StageLogPersister(request.GetInput().GetDeployment().GetId(), request.GetInput().GetStage().GetId())
defer func() {
// When termination signal received and the stage is not completed yet, we should not mark the log persister as completed.
// This can occur when the piped is shutting down while the stage is still running.
if !response.GetStatus().IsCompleted() && signalhandler.Terminated() {
return
}
lp.Complete(time.Minute)
}()
client := &Client{
base: s.client,
pluginName: s.Name(),
applicationID: request.GetInput().GetDeployment().GetApplicationId(),
deploymentID: request.GetInput().GetDeployment().GetId(),
stageID: request.GetInput().GetStage().GetId(),
logPersister: lp,
}
return executeStage(ctx, s.base, &s.config, nil, client, request, s.logger) // TODO: pass the deployTargets
}
// buildPipelineSyncStages builds the stages that will be executed by the plugin.
func buildPipelineSyncStages[Config, DeployTargetConfig any](ctx context.Context, plugin StagePlugin[Config, DeployTargetConfig], config *Config, client *Client, request *deployment.BuildPipelineSyncStagesRequest, logger *zap.Logger) (*deployment.BuildPipelineSyncStagesResponse, error) {
resp, err := plugin.BuildPipelineSyncStages(ctx, config, newPipelineSyncStagesInput(request, client, logger))
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to build pipeline sync stages: %v", err)
}
return newPipelineSyncStagesResponse(plugin, time.Now(), request, resp)
}
func executeStage[Config, DeployTargetConfig any](
ctx context.Context,
plugin StagePlugin[Config, DeployTargetConfig],
config *Config,
deployTargets []*DeployTarget[DeployTargetConfig],
client *Client,
request *deployment.ExecuteStageRequest,
logger *zap.Logger,
) (*deployment.ExecuteStageResponse, error) {
in := &ExecuteStageInput{
Request: ExecuteStageRequest{
StageName: request.GetInput().GetStage().GetName(),
StageConfig: request.GetInput().GetStageConfig(),
},
Client: client,
Logger: logger,
}
resp, err := plugin.ExecuteStage(ctx, config, deployTargets, in)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to execute stage: %v", err)
}
return &deployment.ExecuteStageResponse{
Status: resp.Status.toModelEnum(),
}, nil
}
// ManualOperation represents the manual operation that the user can perform.
type ManualOperation int
const (
// ManualOperationNone indicates that there is no manual operation.
ManualOperationNone ManualOperation = iota
// ManualOperationSkip indicates that the manual operation is to skip the stage.
ManualOperationSkip
// ManualOperationApprove indicates that the manual operation is to approve the stage.
ManualOperationApprove
)
// toModelEnum converts the ManualOperation to the model.ManualOperation.
func (o ManualOperation) toModelEnum() model.ManualOperation {
switch o {
case ManualOperationNone:
return model.ManualOperation_MANUAL_OPERATION_NONE
case ManualOperationSkip:
return model.ManualOperation_MANUAL_OPERATION_SKIP
case ManualOperationApprove:
return model.ManualOperation_MANUAL_OPERATION_APPROVE
default:
return model.ManualOperation_MANUAL_OPERATION_UNKNOWN
}
}
// newPipelineSyncStagesInput converts the request to the internal representation.
func newPipelineSyncStagesInput(request *deployment.BuildPipelineSyncStagesRequest, client *Client, logger *zap.Logger) *BuildPipelineSyncStagesInput {
stages := make([]StageConfig, 0, len(request.Stages))
for _, s := range request.GetStages() {
stages = append(stages, StageConfig{
Index: int(s.GetIndex()),
Name: s.GetName(),
Config: s.GetConfig(),
})
}
req := BuildPipelineSyncStagesRequest{
Rollback: request.GetRollback(),
Stages: stages,
}
return &BuildPipelineSyncStagesInput{
Request: req,
Client: client,
Logger: logger,
}
}
// newPipelineSyncStagesResponse converts the response to the external representation.
func newPipelineSyncStagesResponse(plugin Plugin, now time.Time, request *deployment.BuildPipelineSyncStagesRequest, response *BuildPipelineSyncStagesResponse) (*deployment.BuildPipelineSyncStagesResponse, error) {
// Convert the request stages to a map for easier access.
requestStages := make(map[int]*deployment.BuildPipelineSyncStagesRequest_StageConfig, len(request.GetStages()))
for _, s := range request.GetStages() {
requestStages[int(s.GetIndex())] = s
}
stages := make([]*model.PipelineStage, 0, len(response.Stages))
for _, s := range response.Stages {
// Find the corresponding stage in the request.
requestStage, ok := requestStages[s.Index]
if !ok {
return nil, status.Errorf(codes.Internal, "missing stage with index %d in the request, it's unexpected behavior of the plugin", s.Index)
}
id := requestStage.GetId()
if id == "" {
id = fmt.Sprintf("%s-stage-%d", plugin.Name(), s.Index)
}
stages = append(stages, &model.PipelineStage{
Id: id,
Name: s.Name,
Desc: requestStage.GetDesc(),
Index: int32(s.Index),
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
StatusReason: "", // TODO: set the reason
Metadata: s.Metadata,
Rollback: s.Rollback,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
AvailableOperation: s.AvailableOperation.toModelEnum(),
})
}
return &deployment.BuildPipelineSyncStagesResponse{
Stages: stages,
}, nil
}
// BuildPipelineSyncStagesInput is the input for the BuildPipelineSyncStages method.
type BuildPipelineSyncStagesInput struct {
// Request is the request to build pipeline sync stages.
Request BuildPipelineSyncStagesRequest
// Client is the client to interact with the piped.
Client *Client
// Logger is the logger to log the events.
Logger *zap.Logger
}
// BuildPipelineSyncStagesRequest is the request to build pipeline sync stages.
// Rollback indicates whether the stages for rollback are requested.
type BuildPipelineSyncStagesRequest struct {
// Rollback indicates whether the stages for rollback are requested.
Rollback bool
// Stages contains the stage names and their configurations.
Stages []StageConfig
}
// StageConfig represents the configuration of a stage.
type StageConfig struct {
// Index is the order of the stage in the pipeline.
Index int
// Name is the name of the stage.
// It must be one of the stages returned by FetchDefinedStages.
Name string
// Config is the configuration of the stage.
// It should be marshaled to JSON bytes.
// The plugin should unmarshal it to the appropriate struct.
Config []byte
}
// BuildPipelineSyncStagesResponse is the response of the request to build pipeline sync stages.
type BuildPipelineSyncStagesResponse struct {
Stages []PipelineStage
}
// PipelineStage represents a stage in the pipeline.
type PipelineStage struct {
// Index is the order of the stage in the pipeline.
// The value must be one of the index of the stage in the request.
// The rollback stage should have the same index as the original stage.
Index int
// Name is the name of the stage.
// It must be one of the stages returned by FetchDefinedStages.
Name string
// Rollback indicates whether the stage is for rollback.
Rollback bool
// Metadata contains the metadata of the stage.
Metadata map[string]string
// AvailableOperation indicates the manual operation that the user can perform.
AvailableOperation ManualOperation
}
// ExecuteStageInput is the input for the ExecuteStage method.
type ExecuteStageInput struct {
// Request is the request to execute a stage.
Request ExecuteStageRequest
// Client is the client to interact with the piped.
Client *Client
// Logger is the logger to log the events.
Logger *zap.Logger
}
// ExecuteStageRequest is the request to execute a stage.
type ExecuteStageRequest struct {
// The name of the stage to execute.
StageName string
// Json encoded configuration of the stage.
StageConfig []byte
}
// ExecuteStageResponse is the response of the request to execute a stage.
type ExecuteStageResponse struct {
Status StageStatus
}
// StageStatus represents the current status of a stage of a deployment.
type StageStatus int
const (
StageStatusSuccess StageStatus = 2
StageStatusFailure StageStatus = 3
StageStatusCancelled StageStatus = 4
// StageStatusSkipped StageStatus = 5 // TODO: If SDK can handle whole skipping, this is unnecessary.
// StageStatusExited can be used when the stage succeeded and exit the pipeline without executing the following stages.
StageStatusExited StageStatus = 6
)
// toModelEnum converts the StageStatus to the model.StageStatus.
func (o StageStatus) toModelEnum() model.StageStatus {
switch o {
case StageStatusSuccess:
return model.StageStatus_STAGE_SUCCESS
case StageStatusFailure:
return model.StageStatus_STAGE_FAILURE
case StageStatusCancelled:
return model.StageStatus_STAGE_CANCELLED
case StageStatusExited:
return model.StageStatus_STAGE_EXITED
default:
return model.StageStatus_STAGE_FAILURE
}
}