Skip to content

Commit 7a07070

Browse files
authored
Refactor k8s plugin logic (#5678)
* Relocate stage logics for k8s plugin Signed-off-by: Yoshiki Fujikane <[email protected]> * Split tests Signed-off-by: Yoshiki Fujikane <[email protected]> * Add license Signed-off-by: Yoshiki Fujikane <[email protected]> * Add comments Signed-off-by: Yoshiki Fujikane <[email protected]> * Remove type Stage Signed-off-by: Yoshiki Fujikane <[email protected]> * Fix for lint Signed-off-by: Yoshiki Fujikane <[email protected]> --------- Signed-off-by: Yoshiki Fujikane <[email protected]>
1 parent 57c48c1 commit 7a07070

File tree

8 files changed

+893
-843
lines changed

8 files changed

+893
-843
lines changed

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

+19-47
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,36 @@ package deployment
1717
import (
1818
"slices"
1919

20-
"github.com/pipe-cd/pipecd/pkg/model"
2120
"github.com/pipe-cd/pipecd/pkg/plugin/sdk"
2221
)
2322

24-
type Stage string
25-
2623
const (
2724
// StageK8sSync represents the state where
2825
// all resources should be synced with the Git state.
29-
StageK8sSync Stage = "K8S_SYNC"
26+
StageK8sSync = "K8S_SYNC"
3027
// StageK8sPrimaryRollout represents the state where
3128
// the PRIMARY variant resources has been updated to the new version/configuration.
32-
StageK8sPrimaryRollout Stage = "K8S_PRIMARY_ROLLOUT"
29+
StageK8sPrimaryRollout = "K8S_PRIMARY_ROLLOUT"
3330
// StageK8sCanaryRollout represents the state where
3431
// the CANARY variant resources has been rolled out with the new version/configuration.
35-
StageK8sCanaryRollout Stage = "K8S_CANARY_ROLLOUT"
32+
StageK8sCanaryRollout = "K8S_CANARY_ROLLOUT"
3633
// StageK8sCanaryClean represents the state where
3734
// the CANARY variant resources has been cleaned.
38-
StageK8sCanaryClean Stage = "K8S_CANARY_CLEAN"
35+
StageK8sCanaryClean = "K8S_CANARY_CLEAN"
3936
// StageK8sBaselineRollout represents the state where
4037
// the BASELINE variant resources has been rolled out.
41-
StageK8sBaselineRollout Stage = "K8S_BASELINE_ROLLOUT"
38+
StageK8sBaselineRollout = "K8S_BASELINE_ROLLOUT"
4239
// StageK8sBaselineClean represents the state where
4340
// the BASELINE variant resources has been cleaned.
44-
StageK8sBaselineClean Stage = "K8S_BASELINE_CLEAN"
41+
StageK8sBaselineClean = "K8S_BASELINE_CLEAN"
4542
// StageK8sTrafficRouting represents the state where the traffic to application
4643
// should be splitted as the specified percentage to PRIMARY, CANARY, BASELINE variants.
47-
StageK8sTrafficRouting Stage = "K8S_TRAFFIC_ROUTING"
44+
StageK8sTrafficRouting = "K8S_TRAFFIC_ROUTING"
4845
// StageK8sRollback represents the state where all deployed resources should be rollbacked.
49-
StageK8sRollback Stage = "K8S_ROLLBACK"
46+
StageK8sRollback = "K8S_ROLLBACK"
5047
)
5148

52-
var AllStages = []Stage{
49+
var allStages = []string{
5350
StageK8sSync,
5451
StageK8sPrimaryRollout,
5552
StageK8sCanaryRollout,
@@ -60,54 +57,29 @@ var AllStages = []Stage{
6057
StageK8sRollback,
6158
}
6259

63-
func (s Stage) String() string {
64-
return string(s)
65-
}
66-
6760
const (
68-
PredefinedStageK8sSync = "K8sSync"
69-
PredefinedStageRollback = "K8sRollback"
61+
// StageDescriptionK8sSync represents the description of the K8sSync stage.
62+
StageDescriptionK8sSync = "Sync by applying all manifests"
63+
// StageDescriptionK8sRollback represents the description of the K8sRollback stage.
64+
StageDescriptionK8sRollback = "Rollback the deployment"
7065
)
7166

72-
var predefinedStages = map[string]*model.PipelineStage{
73-
PredefinedStageK8sSync: {
74-
Id: PredefinedStageK8sSync,
75-
Name: string(StageK8sSync),
76-
Desc: "Sync by applying all manifests",
77-
Rollback: false,
78-
},
79-
PredefinedStageRollback: {
80-
Id: PredefinedStageRollback,
81-
Name: string(StageK8sRollback),
82-
Desc: "Rollback the deployment",
83-
Rollback: true,
84-
},
85-
}
86-
87-
// GetPredefinedStage finds and returns the predefined stage for the given id.
88-
func GetPredefinedStage(id string) (*model.PipelineStage, bool) {
89-
stage, ok := predefinedStages[id]
90-
return stage, ok
91-
}
92-
9367
func buildQuickSyncPipeline(autoRollback bool) []sdk.QuickSyncStage {
9468
out := make([]sdk.QuickSyncStage, 0, 2)
9569

96-
stage, _ := GetPredefinedStage(PredefinedStageK8sSync)
9770
out = append(out, sdk.QuickSyncStage{
98-
Name: stage.GetName(),
99-
Description: stage.GetDesc(),
71+
Name: StageK8sSync,
72+
Description: StageDescriptionK8sSync,
10073
Rollback: false,
10174
Metadata: make(map[string]string, 0),
10275
AvailableOperation: sdk.ManualOperationNone,
10376
},
10477
)
10578

10679
if autoRollback {
107-
s, _ := GetPredefinedStage(PredefinedStageRollback)
10880
out = append(out, sdk.QuickSyncStage{
109-
Name: s.GetName(),
110-
Description: s.GetDesc(),
81+
Name: StageK8sRollback,
82+
Description: StageDescriptionK8sRollback,
11183
Rollback: true,
11284
Metadata: make(map[string]string, 0),
11385
AvailableOperation: sdk.ManualOperationNone,
@@ -136,9 +108,9 @@ func buildPipelineStages(stages []sdk.StageConfig, autoRollback bool) []sdk.Pipe
136108
minIndex := slices.MinFunc(stages, func(a, b sdk.StageConfig) int {
137109
return a.Index - b.Index
138110
}).Index
139-
// we copy the predefined stage to avoid modifying the original one.
111+
140112
out = append(out, sdk.PipelineStage{
141-
Name: StageK8sRollback.String(),
113+
Name: StageK8sRollback,
142114
Index: minIndex,
143115
Rollback: true,
144116
Metadata: make(map[string]string, 0),

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func Test_buildQuickSyncPipeline(t *testing.T) {
3535
rollback: false,
3636
expected: []sdk.QuickSyncStage{
3737
{
38-
Name: predefinedStages[PredefinedStageK8sSync].Name,
39-
Description: predefinedStages[PredefinedStageK8sSync].Desc,
38+
Name: StageK8sSync,
39+
Description: StageDescriptionK8sSync,
4040
Rollback: false,
4141
Metadata: make(map[string]string, 0),
4242
AvailableOperation: sdk.ManualOperationNone,
@@ -48,15 +48,15 @@ func Test_buildQuickSyncPipeline(t *testing.T) {
4848
rollback: true,
4949
expected: []sdk.QuickSyncStage{
5050
{
51-
Name: predefinedStages[PredefinedStageK8sSync].Name,
52-
Description: predefinedStages[PredefinedStageK8sSync].Desc,
51+
Name: StageK8sSync,
52+
Description: StageDescriptionK8sSync,
5353
Rollback: false,
5454
Metadata: make(map[string]string, 0),
5555
AvailableOperation: sdk.ManualOperationNone,
5656
},
5757
{
58-
Name: predefinedStages[PredefinedStageRollback].Name,
59-
Description: predefinedStages[PredefinedStageRollback].Desc,
58+
Name: StageK8sRollback,
59+
Description: StageDescriptionK8sRollback,
6060
Rollback: true,
6161
Metadata: make(map[string]string, 0),
6262
AvailableOperation: sdk.ManualOperationNone,
@@ -143,7 +143,7 @@ func Test_buildPipelineStages(t *testing.T) {
143143
AvailableOperation: sdk.ManualOperationNone,
144144
},
145145
{
146-
Name: StageK8sRollback.String(),
146+
Name: StageK8sRollback,
147147
Index: 0,
148148
Rollback: true,
149149
Metadata: make(map[string]string, 0),

0 commit comments

Comments
 (0)