Skip to content

Implement BuildPipelineSync with SDK #5672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func BuildQuickSyncPipeline(autoRollback bool) []sdk.QuickSyncStage {
return out
}

// TODO: Rename this function
func buildPipelineStages(stages []*deployment.BuildPipelineSyncStagesRequest_StageConfig, autoRollback bool, now time.Time) []*model.PipelineStage {
out := make([]*model.PipelineStage, 0, len(stages)+1)

Expand Down Expand Up @@ -197,3 +198,38 @@ func buildPipelineStages(stages []*deployment.BuildPipelineSyncStagesRequest_Sta

return out
}

// buildPipelineStagesWithSDK builds the pipeline stages with the given SDK stages.
// TODO: Rename this function to buildPipelineStages after removing the old one.
func buildPipelineStagesWithSDK(stages []sdk.StageConfig, autoRollback bool) []sdk.PipelineStage {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reference implementation

func buildPipelineStages(stages []*deployment.BuildPipelineSyncStagesRequest_StageConfig, autoRollback bool, now time.Time) []*model.PipelineStage {
out := make([]*model.PipelineStage, 0, len(stages)+1)
for _, s := range stages {
id := s.GetId()
if id == "" {
id = fmt.Sprintf("stage-%d", s.GetIndex())
}
stage := &model.PipelineStage{
Id: id,
Name: s.GetName(),
Desc: s.GetDesc(),
Index: s.GetIndex(),
Rollback: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
}
out = append(out, stage)
}
if autoRollback {
// we set the index of the rollback stage to the minimum index of all stages.
minIndex := slices.MinFunc(stages, func(a, b *deployment.BuildPipelineSyncStagesRequest_StageConfig) int {
return int(a.GetIndex() - b.GetIndex())
}).GetIndex()
s, _ := GetPredefinedStage(PredefinedStageRollback)
// we copy the predefined stage to avoid modifying the original one.
out = append(out, &model.PipelineStage{
Id: s.GetId(),
Name: s.GetName(),
Desc: s.GetDesc(),
Index: minIndex,
Rollback: s.GetRollback(),
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
})
}
return out
}

out := make([]sdk.PipelineStage, 0, len(stages)+1)

for _, s := range stages {
out = append(out, sdk.PipelineStage{
Name: s.Name,
Index: s.Index,
Rollback: false,
Metadata: make(map[string]string, 0),
AvailableOperation: sdk.ManualOperationNone,
})
}

if autoRollback {
// we set the index of the rollback stage to the minimum index of all stages.
minIndex := slices.MinFunc(stages, func(a, b sdk.StageConfig) int {
return a.Index - b.Index
}).Index

s, _ := GetPredefinedStage(PredefinedStageRollback)
// we copy the predefined stage to avoid modifying the original one.
out = append(out, sdk.PipelineStage{
Name: s.GetName(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The s is used only here, so can we replace this with string(StageK8sRollback)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, fixed on d0fd69f
Also, I used StageK8sRollback.String() method.

Index: minIndex,
Rollback: true,
Metadata: make(map[string]string, 0),
AvailableOperation: sdk.ManualOperationNone,
})
}

return out
}
88 changes: 88 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,91 @@ func TestBuildQuickSyncPipeline(t *testing.T) {
})
}
}

func Test_buildPipelineStagesWithSDK(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reference implementation

func TestBuildPipelineStages(t *testing.T) {
t.Parallel()
now := time.Now()
tests := []struct {
name string
stages []*deployment.BuildPipelineSyncStagesRequest_StageConfig
autoRollback bool
expected []*model.PipelineStage
}{
{
name: "without auto rollback",
stages: []*deployment.BuildPipelineSyncStagesRequest_StageConfig{
{
Id: "stage-1",
Name: "Stage 1",
Desc: "Description 1",
Index: 0,
},
{
Id: "stage-2",
Name: "Stage 2",
Desc: "Description 2",
Index: 1,
},
},
autoRollback: false,
expected: []*model.PipelineStage{
{
Id: "stage-1",
Name: "Stage 1",
Desc: "Description 1",
Index: 0,
Rollback: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
},
{
Id: "stage-2",
Name: "Stage 2",
Desc: "Description 2",
Index: 1,
Rollback: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
},
},
},
{
name: "with auto rollback",
stages: []*deployment.BuildPipelineSyncStagesRequest_StageConfig{
{
Id: "stage-1",
Name: "Stage 1",
Desc: "Description 1",
Index: 0,
},
{
Id: "stage-2",
Name: "Stage 2",
Desc: "Description 2",
Index: 1,
},
},
autoRollback: true,
expected: []*model.PipelineStage{
{
Id: "stage-1",
Name: "Stage 1",
Desc: "Description 1",
Index: 0,
Rollback: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
},
{
Id: "stage-2",
Name: "Stage 2",
Desc: "Description 2",
Index: 1,
Rollback: false,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
},
{
Id: PredefinedStageRollback,
Name: StageK8sRollback.String(),
Desc: "Rollback the deployment",
Index: 0,
Rollback: true,
Status: model.StageStatus_STAGE_NOT_STARTED_YET,
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := buildPipelineStages(tt.stages, tt.autoRollback, now)
assert.Equal(t, tt.expected, actual)
})
}
}

t.Parallel()

tests := []struct {
name string
stages []sdk.StageConfig
autoRollback bool
expected []sdk.PipelineStage
}{
{
name: "without auto rollback",
stages: []sdk.StageConfig{
{
Name: "Stage 1",
Index: 0,
},
{
Name: "Stage 2",
Index: 1,
},
},
autoRollback: false,
expected: []sdk.PipelineStage{
{
Name: "Stage 1",
Index: 0,
Rollback: false,
Metadata: make(map[string]string, 0),
AvailableOperation: sdk.ManualOperationNone,
},
{
Name: "Stage 2",
Index: 1,
Rollback: false,
Metadata: make(map[string]string, 0),
AvailableOperation: sdk.ManualOperationNone,
},
},
},
{
name: "with auto rollback",
stages: []sdk.StageConfig{
{
Name: "Stage 1",
Index: 0,
},
{
Name: "Stage 2",
Index: 1,
},
},
autoRollback: true,
expected: []sdk.PipelineStage{
{
Name: "Stage 1",
Index: 0,
Rollback: false,
Metadata: make(map[string]string, 0),
AvailableOperation: sdk.ManualOperationNone,
},
{
Name: "Stage 2",
Index: 1,
Rollback: false,
Metadata: make(map[string]string, 0),
AvailableOperation: sdk.ManualOperationNone,
},
{
Name: StageK8sRollback.String(),
Index: 0,
Rollback: true,
Metadata: make(map[string]string, 0),
AvailableOperation: sdk.ManualOperationNone,
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

actual := buildPipelineStagesWithSDK(tt.stages, tt.autoRollback)
assert.Equal(t, tt.expected, actual)
})
}
}
5 changes: 3 additions & 2 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@
return stages
}

// FIXME
func (p *Plugin) BuildPipelineSyncStages(ctx context.Context, _ *sdk.ConfigNone, input *sdk.BuildPipelineSyncStagesInput) (*sdk.BuildPipelineSyncStagesResponse, error) {
return nil, nil
return &sdk.BuildPipelineSyncStagesResponse{
Stages: buildPipelineStagesWithSDK(input.Request.Stages, input.Request.Rollback),
}, nil

Check warning on line 69 in pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go#L67-L69

Added lines #L67 - L69 were not covered by tests
}

// FIXME
Expand Down