-
Notifications
You must be signed in to change notification settings - Fork 157
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
Implement BuildPipelineSync with SDK #5672
Conversation
Signed-off-by: Yoshiki Fujikane <[email protected]>
|
||
// 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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reference implementation
pipecd/pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline.go
Lines 157 to 199 in 38a0086
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 | |
} |
@@ -251,3 +251,91 @@ func TestBuildQuickSyncPipeline(t *testing.T) { | |||
}) | |||
} | |||
} | |||
|
|||
func Test_buildPipelineStagesWithSDK(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reference implementation
pipecd/pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline_test.go
Lines 91 to 200 in 38a0086
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) | |
}) | |
} | |
} |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5672 +/- ##
==========================================
+ Coverage 25.56% 25.60% +0.03%
==========================================
Files 478 478
Lines 51280 51307 +27
==========================================
+ Hits 13110 13135 +25
- Misses 37165 37167 +2
Partials 1005 1005 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
I commented on nits
s, _ := GetPredefinedStage(PredefinedStageRollback) | ||
// we copy the predefined stage to avoid modifying the original one. | ||
out = append(out, sdk.PipelineStage{ | ||
Name: s.GetName(), |
There was a problem hiding this comment.
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)
?
There was a problem hiding this comment.
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.
Signed-off-by: Yoshiki Fujikane <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
What this PR does:
I implemented k8s BuildPipelineSync by using SDK.
Why we need it:
We want to implement plugins with SDK.
Which issue(s) this PR fixes:
Part of #4980 #5006
Which issue(s) this PR fixes:
Fixes #
Does this PR introduce a user-facing change?: