Skip to content

Commit

Permalink
Add option script run stage
Browse files Browse the repository at this point in the history
Signed-off-by: Yoshiki Fujikane <[email protected]>
  • Loading branch information
ffjlabo committed Dec 6, 2023
1 parent 45eee6b commit e6118ce
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ type PipelineStage struct {
WaitStageOptions *WaitStageOptions
WaitApprovalStageOptions *WaitApprovalStageOptions
AnalysisStageOptions *AnalysisStageOptions
ScriptRunStageOptions *ScriptRunStageOptions

K8sPrimaryRolloutStageOptions *K8sPrimaryRolloutStageOptions
K8sCanaryRolloutStageOptions *K8sCanaryRolloutStageOptions
Expand Down Expand Up @@ -291,6 +292,12 @@ func (s *PipelineStage) UnmarshalJSON(data []byte) error {
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.AnalysisStageOptions)
}
case model.StageScriptRun:
s.ScriptRunStageOptions = &ScriptRunStageOptions{}
if len(gs.With) > 0 {
err = json.Unmarshal(gs.With, s.ScriptRunStageOptions)
}

case model.StageK8sPrimaryRollout:
s.K8sPrimaryRolloutStageOptions = &K8sPrimaryRolloutStageOptions{}
if len(gs.With) > 0 {
Expand Down Expand Up @@ -485,6 +492,21 @@ func (a *AnalysisStageOptions) Validate() error {
return nil
}

// ScriptRunStageOptions contains all configurable values for a SCRIPT_RUN stage.
type ScriptRunStageOptions struct {
Env map[string]string `json:"env"`
Run string `json:"run"`
OnRollback string `json:"onRollback"`
}

// Validate checks the required fields of ScriptRunStageOptions.
func (s *ScriptRunStageOptions) Validate() error {
if s.Run == "" {
return fmt.Errorf("SCRIPT_RUN stage requires run field")
}
return nil
}

type AnalysisTemplateRef struct {
Name string `json:"name"`
AppArgs map[string]string `json:"appArgs"`
Expand Down
30 changes: 30 additions & 0 deletions pkg/config/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,3 +702,33 @@ func TestCustomSyncConfig(t *testing.T) {
})
}
}

// TODO: Add testcases for other kinds of applications.
func TestScriptSycConfiguration(t *testing.T) {
testcases := []struct {
name string
opts ScriptRunStageOptions
wantErr bool
}{
{
name: "valid",
opts: ScriptRunStageOptions{
Run: "echo 'hello world'",
},
wantErr: false,
},
{
name: "invalid",
opts: ScriptRunStageOptions{
Run: "",
},
wantErr: true,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
err := tc.opts.Validate()
assert.Equal(t, tc.wantErr, err != nil)
})
}
}
3 changes: 3 additions & 0 deletions pkg/model/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const (
// StageAnalysis represents the waiting state for analysing
// the application status based on metrics, log, http request...
StageAnalysis Stage = "ANALYSIS"
// StageScriptRun represents a state where
// the specified script will be executed.
StageScriptRun Stage = "SCRIPT_RUN"

// StageK8sSync represents the state where
// all resources should be synced with the Git state.
Expand Down

0 comments on commit e6118ce

Please sign in to comment.