Skip to content
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

Merged
merged 2 commits into from
Mar 17, 2025
Merged

Conversation

ffjlabo
Copy link
Member

@ffjlabo ffjlabo commented Mar 17, 2025

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?:

  • How are users affected by this change:
  • Is this breaking change:
  • How to migrate (if breaking change):


// 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
}

@@ -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)
})
}
}

@ffjlabo ffjlabo marked this pull request as ready for review March 17, 2025 04:36
Copy link

codecov bot commented Mar 17, 2025

Codecov Report

Attention: Patch coverage is 89.28571% with 3 lines in your changes missing coverage. Please review.

Project coverage is 25.60%. Comparing base (38a0086) to head (d0fd69f).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...app/pipedv1/plugin/kubernetes/deployment/plugin.go 0.00% 3 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ffjlabo ffjlabo enabled auto-merge (squash) March 17, 2025 04:36
Copy link
Contributor

@Warashi Warashi left a 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(),
Copy link
Contributor

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.

Copy link
Contributor

@Warashi Warashi left a comment

Choose a reason for hiding this comment

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

LGTM

@ffjlabo ffjlabo merged commit 20577c0 into master Mar 17, 2025
14 of 16 checks passed
@ffjlabo ffjlabo deleted the implement-pipeline-sync-with-SDK branch March 17, 2025 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants