Skip to content

Commit 20577c0

Browse files
authored
Implement BuildPipelineSync with SDK (#5672)
Signed-off-by: Yoshiki Fujikane <[email protected]>
1 parent f50b273 commit 20577c0

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed

pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline.go

+34
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ func BuildQuickSyncPipeline(autoRollback bool) []sdk.QuickSyncStage {
154154
return out
155155
}
156156

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

@@ -197,3 +198,36 @@ func buildPipelineStages(stages []*deployment.BuildPipelineSyncStagesRequest_Sta
197198

198199
return out
199200
}
201+
202+
// buildPipelineStagesWithSDK builds the pipeline stages with the given SDK stages.
203+
// TODO: Rename this function to buildPipelineStages after removing the old one.
204+
func buildPipelineStagesWithSDK(stages []sdk.StageConfig, autoRollback bool) []sdk.PipelineStage {
205+
out := make([]sdk.PipelineStage, 0, len(stages)+1)
206+
207+
for _, s := range stages {
208+
out = append(out, sdk.PipelineStage{
209+
Name: s.Name,
210+
Index: s.Index,
211+
Rollback: false,
212+
Metadata: make(map[string]string, 0),
213+
AvailableOperation: sdk.ManualOperationNone,
214+
})
215+
}
216+
217+
if autoRollback {
218+
// we set the index of the rollback stage to the minimum index of all stages.
219+
minIndex := slices.MinFunc(stages, func(a, b sdk.StageConfig) int {
220+
return a.Index - b.Index
221+
}).Index
222+
// we copy the predefined stage to avoid modifying the original one.
223+
out = append(out, sdk.PipelineStage{
224+
Name: StageK8sRollback.String(),
225+
Index: minIndex,
226+
Rollback: true,
227+
Metadata: make(map[string]string, 0),
228+
AvailableOperation: sdk.ManualOperationNone,
229+
})
230+
}
231+
232+
return out
233+
}

pkg/app/pipedv1/plugin/kubernetes/deployment/pipeline_test.go

+88
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,91 @@ func TestBuildQuickSyncPipeline(t *testing.T) {
251251
})
252252
}
253253
}
254+
255+
func Test_buildPipelineStagesWithSDK(t *testing.T) {
256+
t.Parallel()
257+
258+
tests := []struct {
259+
name string
260+
stages []sdk.StageConfig
261+
autoRollback bool
262+
expected []sdk.PipelineStage
263+
}{
264+
{
265+
name: "without auto rollback",
266+
stages: []sdk.StageConfig{
267+
{
268+
Name: "Stage 1",
269+
Index: 0,
270+
},
271+
{
272+
Name: "Stage 2",
273+
Index: 1,
274+
},
275+
},
276+
autoRollback: false,
277+
expected: []sdk.PipelineStage{
278+
{
279+
Name: "Stage 1",
280+
Index: 0,
281+
Rollback: false,
282+
Metadata: make(map[string]string, 0),
283+
AvailableOperation: sdk.ManualOperationNone,
284+
},
285+
{
286+
Name: "Stage 2",
287+
Index: 1,
288+
Rollback: false,
289+
Metadata: make(map[string]string, 0),
290+
AvailableOperation: sdk.ManualOperationNone,
291+
},
292+
},
293+
},
294+
{
295+
name: "with auto rollback",
296+
stages: []sdk.StageConfig{
297+
{
298+
Name: "Stage 1",
299+
Index: 0,
300+
},
301+
{
302+
Name: "Stage 2",
303+
Index: 1,
304+
},
305+
},
306+
autoRollback: true,
307+
expected: []sdk.PipelineStage{
308+
{
309+
Name: "Stage 1",
310+
Index: 0,
311+
Rollback: false,
312+
Metadata: make(map[string]string, 0),
313+
AvailableOperation: sdk.ManualOperationNone,
314+
},
315+
{
316+
Name: "Stage 2",
317+
Index: 1,
318+
Rollback: false,
319+
Metadata: make(map[string]string, 0),
320+
AvailableOperation: sdk.ManualOperationNone,
321+
},
322+
{
323+
Name: StageK8sRollback.String(),
324+
Index: 0,
325+
Rollback: true,
326+
Metadata: make(map[string]string, 0),
327+
AvailableOperation: sdk.ManualOperationNone,
328+
},
329+
},
330+
},
331+
}
332+
333+
for _, tt := range tests {
334+
t.Run(tt.name, func(t *testing.T) {
335+
t.Parallel()
336+
337+
actual := buildPipelineStagesWithSDK(tt.stages, tt.autoRollback)
338+
assert.Equal(t, tt.expected, actual)
339+
})
340+
}
341+
}

pkg/app/pipedv1/plugin/kubernetes/deployment/plugin.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ func (p *Plugin) FetchDefinedStages() []string {
6363
return stages
6464
}
6565

66-
// FIXME
6766
func (p *Plugin) BuildPipelineSyncStages(ctx context.Context, _ *sdk.ConfigNone, input *sdk.BuildPipelineSyncStagesInput) (*sdk.BuildPipelineSyncStagesResponse, error) {
68-
return nil, nil
67+
return &sdk.BuildPipelineSyncStagesResponse{
68+
Stages: buildPipelineStagesWithSDK(input.Request.Stages, input.Request.Rollback),
69+
}, nil
6970
}
7071

7172
// FIXME

0 commit comments

Comments
 (0)