Skip to content

Commit a20eaef

Browse files
authored
Refine BuildQuickSyncPipelineStages signature in deployment plugin interface (#5599)
* Refactor stage creation by introducing a toModel method for cleaner code Signed-off-by: Yoshiki Fujikane <[email protected]> * Add BuildQuickSyncStagesInput and BuildQuickSyncStagesRequest types for improved stage building Signed-off-by: Yoshiki Fujikane <[email protected]> * Add BuildQuickSyncStagesResponse and QuickSyncStage types for quick sync process Signed-off-by: Yoshiki Fujikane <[email protected]> * Fix BuildQuickSyncStages method signature and implement logic for stage building Signed-off-by: Yoshiki Fujikane <[email protected]> * Remove error from newQuickSyncStagesResponse Signed-off-by: Yoshiki Fujikane <[email protected]> --------- Signed-off-by: Yoshiki Fujikane <[email protected]>
1 parent ad00a56 commit a20eaef

File tree

1 file changed

+101
-16
lines changed

1 file changed

+101
-16
lines changed

pkg/plugin/sdk/deployment.go

+101-16
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type DeploymentPlugin[Config, DeployTargetConfig any] interface {
6565
// DetermineStrategy determines the strategy to deploy the resources.
6666
DetermineStrategy(context.Context, *Config, *Client, TODO) (TODO, error)
6767
// BuildQuickSyncStages builds the stages that will be executed during the quick sync process.
68-
BuildQuickSyncStages(context.Context, *Config, *Client, TODO) (TODO, error)
68+
BuildQuickSyncStages(context.Context, *Config, *BuildQuickSyncStagesInput) (*BuildQuickSyncStagesResponse, error)
6969
}
7070

7171
// StagePlugin is the interface implemented by a plugin that focuses on executing generic stages.
@@ -168,8 +168,23 @@ func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) BuildPipelin
168168
}
169169
return buildPipelineSyncStages(ctx, s.base, &s.config, client, request, s.logger)
170170
}
171-
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) BuildQuickSyncStages(context.Context, *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
172-
return nil, status.Errorf(codes.Unimplemented, "method BuildQuickSyncStages not implemented")
171+
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) BuildQuickSyncStages(ctx context.Context, request *deployment.BuildQuickSyncStagesRequest) (*deployment.BuildQuickSyncStagesResponse, error) {
172+
input := &BuildQuickSyncStagesInput{
173+
Request: BuildQuickSyncStagesRequest{
174+
Rollback: request.GetRollback(),
175+
},
176+
Client: &Client{
177+
base: s.client,
178+
pluginName: s.Name(),
179+
},
180+
Logger: s.logger,
181+
}
182+
183+
response, err := s.base.BuildQuickSyncStages(ctx, &s.config, input)
184+
if err != nil {
185+
return nil, status.Errorf(codes.Internal, "failed to build quick sync stages: %v", err)
186+
}
187+
return newQuickSyncStagesResponse(s.base, time.Now(), response), nil
173188
}
174189
func (s *DeploymentPluginServiceServer[Config, DeployTargetConfig]) ExecuteStage(ctx context.Context, request *deployment.ExecuteStageRequest) (response *deployment.ExecuteStageResponse, _ error) {
175190
lp := s.logPersister.StageLogPersister(request.GetInput().GetDeployment().GetId(), request.GetInput().GetStage().GetId())
@@ -377,25 +392,26 @@ func newPipelineSyncStagesResponse(plugin Plugin, now time.Time, request *deploy
377392
if id == "" {
378393
id = fmt.Sprintf("%s-stage-%d", plugin.Name(), s.Index)
379394
}
380-
stages = append(stages, &model.PipelineStage{
381-
Id: id,
382-
Name: s.Name,
383-
Desc: requestStage.GetDesc(),
384-
Index: int32(s.Index),
385-
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
386-
StatusReason: "", // TODO: set the reason
387-
Metadata: s.Metadata,
388-
Rollback: s.Rollback,
389-
CreatedAt: now.Unix(),
390-
UpdatedAt: now.Unix(),
391-
AvailableOperation: s.AvailableOperation.toModelEnum(),
392-
})
395+
396+
stages = append(stages, s.toModel(id, requestStage.GetDesc(), now))
393397
}
394398
return &deployment.BuildPipelineSyncStagesResponse{
395399
Stages: stages,
396400
}, nil
397401
}
398402

403+
// newQuickSyncStagesResponse converts the response to the external representation.
404+
func newQuickSyncStagesResponse(plugin Plugin, now time.Time, response *BuildQuickSyncStagesResponse) *deployment.BuildQuickSyncStagesResponse {
405+
stages := make([]*model.PipelineStage, 0, len(response.Stages))
406+
for i, s := range response.Stages {
407+
id := fmt.Sprintf("%s-stage-%d", plugin.Name(), i)
408+
stages = append(stages, s.toModel(id, now))
409+
}
410+
return &deployment.BuildQuickSyncStagesResponse{
411+
Stages: stages,
412+
}
413+
}
414+
399415
// BuildPipelineSyncStagesInput is the input for the BuildPipelineSyncStages method.
400416
type BuildPipelineSyncStagesInput struct {
401417
// Request is the request to build pipeline sync stages.
@@ -415,6 +431,23 @@ type BuildPipelineSyncStagesRequest struct {
415431
Stages []StageConfig
416432
}
417433

434+
// BuildQuickSyncStagesInput is the input for the BuildQuickSyncStages method.
435+
type BuildQuickSyncStagesInput struct {
436+
// Request is the request to build pipeline sync stages.
437+
Request BuildQuickSyncStagesRequest
438+
// Client is the client to interact with the piped.
439+
Client *Client
440+
// Logger is the logger to log the events.
441+
Logger *zap.Logger
442+
}
443+
444+
// BuildQuickSyncStagesRequest is the request to build quick sync stages.
445+
// Rollback indicates whether the stages for rollback are requested.
446+
type BuildQuickSyncStagesRequest struct {
447+
// Rollback indicates whether the stages for rollback are requested.
448+
Rollback bool
449+
}
450+
418451
// StageConfig represents the configuration of a stage.
419452
type StageConfig struct {
420453
// Index is the order of the stage in the pipeline.
@@ -433,6 +466,11 @@ type BuildPipelineSyncStagesResponse struct {
433466
Stages []PipelineStage
434467
}
435468

469+
// BuildQuickSyncStagesResponse is the response of the request to build quick sync stages.
470+
type BuildQuickSyncStagesResponse struct {
471+
Stages []QuickSyncStage
472+
}
473+
436474
// PipelineStage represents a stage in the pipeline.
437475
type PipelineStage struct {
438476
// Index is the order of the stage in the pipeline.
@@ -450,6 +488,53 @@ type PipelineStage struct {
450488
AvailableOperation ManualOperation
451489
}
452490

491+
func (p *PipelineStage) toModel(id, description string, now time.Time) *model.PipelineStage {
492+
return &model.PipelineStage{
493+
Id: id,
494+
Name: p.Name,
495+
Desc: description,
496+
Index: int32(p.Index),
497+
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
498+
StatusReason: "", // TODO: set the reason
499+
Metadata: p.Metadata,
500+
Rollback: p.Rollback,
501+
CreatedAt: now.Unix(),
502+
UpdatedAt: now.Unix(),
503+
AvailableOperation: p.AvailableOperation.toModelEnum(),
504+
}
505+
}
506+
507+
// QuickSyncStage represents a stage in the pipeline.
508+
type QuickSyncStage struct {
509+
// Name is the name of the stage.
510+
// It must be one of the stages returned by FetchDefinedStages.
511+
Name string
512+
// Description is the description of the stage.
513+
Description string
514+
// Rollback indicates whether the stage is for rollback.
515+
Rollback bool
516+
// Metadata contains the metadata of the stage.
517+
Metadata map[string]string
518+
// AvailableOperation indicates the manual operation that the user can perform.
519+
AvailableOperation ManualOperation
520+
}
521+
522+
func (p *QuickSyncStage) toModel(id string, now time.Time) *model.PipelineStage {
523+
return &model.PipelineStage{
524+
Id: id,
525+
Name: p.Name,
526+
Desc: p.Description,
527+
Index: 0,
528+
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
529+
StatusReason: "", // TODO: set the reason
530+
Metadata: p.Metadata,
531+
Rollback: p.Rollback,
532+
CreatedAt: now.Unix(),
533+
UpdatedAt: now.Unix(),
534+
AvailableOperation: p.AvailableOperation.toModelEnum(),
535+
}
536+
}
537+
453538
// ExecuteStageInput is the input for the ExecuteStage method.
454539
type ExecuteStageInput struct {
455540
// Request is the request to execute a stage.

0 commit comments

Comments
 (0)