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

fix: config selection improved logic #54

Merged
merged 14 commits into from
Jun 27, 2023
Merged
36 changes: 14 additions & 22 deletions pkg/workflow_handler/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ func (wfc *WorkflowsClientImpl) ConstructTemplates(workflowsBatch *common.Workfl
return nil, err
}
if onExit == nil || len(onExit.DAG.Tasks) == 0 {
_, ok := wfc.cfg.WorkflowsConfig.Configs[configName]
if ok && wfc.cfg.WorkflowsConfig.Configs[configName].OnExit != nil {
if IsConfigExists(&wfc.cfg.WorkflowsConfig, configName) && IsConfigsOnExitExists(&wfc.cfg.WorkflowsConfig, configName) {
template := &v1alpha1.Template{
Name: ONEXIT,
DAG: &v1alpha1.DAGTemplate{
Expand All @@ -74,8 +73,7 @@ func (wfc *WorkflowsClientImpl) ConstructTemplates(workflowsBatch *common.Workfl

func (wfc *WorkflowsClientImpl) ConstructSpec(templates []v1alpha1.Template, params []v1alpha1.Parameter, configName string) (*v1alpha1.WorkflowSpec, error) {
finalSpec := &v1alpha1.WorkflowSpec{}
_, ok := wfc.cfg.WorkflowsConfig.Configs[configName]
if ok {
if IsConfigExists(&wfc.cfg.WorkflowsConfig, configName) {
*finalSpec = wfc.cfg.WorkflowsConfig.Configs[configName].Spec
if len(wfc.cfg.WorkflowsConfig.Configs[configName].OnExit) != 0 {
finalSpec.OnExit = ONEXIT
Expand Down Expand Up @@ -110,35 +108,29 @@ func (wfc *WorkflowsClientImpl) CreateWorkflow(spec *v1alpha1.WorkflowSpec, work

func (wfc *WorkflowsClientImpl) SelectConfig(workflowsBatch *common.WorkflowsBatch) (string, error) {
var configName string
ok := IsConfigExists(&wfc.cfg.WorkflowsConfig, "default")
if ok {
if IsConfigExists(&wfc.cfg.WorkflowsConfig, "default") {
configName = "default"
log.Printf(
"%s config selected for workflow in repo: %s branch %s",
configName,
workflowsBatch.Payload.Repo,
workflowsBatch.Payload.Branch,
) // Info
}

if *workflowsBatch.Config != "" {
ok = IsConfigExists(&wfc.cfg.WorkflowsConfig, *workflowsBatch.Config)
if ok {
if IsConfigExists(&wfc.cfg.WorkflowsConfig, *workflowsBatch.Config) {
configName = *workflowsBatch.Config
log.Printf(
"%s config overwrited for workflow in repo: %s branch %s",
*workflowsBatch.Config,
workflowsBatch.Payload.Repo,
workflowsBatch.Payload.Branch,
) // Info
} else {
log.Printf(
return configName, fmt.Errorf(
gosharo marked this conversation as resolved.
Show resolved Hide resolved
"error in selecting config, staying with default config for repo %s branch %s",
workflowsBatch.Payload.Repo,
workflowsBatch.Payload.Branch,
) // Error
)
}
}

log.Printf(
"%s config selected for workflow in repo: %s branch %s",
configName,
workflowsBatch.Payload.Repo,
workflowsBatch.Payload.Branch,
) // Info

return configName, nil
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/workflow_handler/workflows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestSelectConfig(t *testing.T) {
returnConfigName, err := wfcImpl.SelectConfig(workflowsBatch)

// Assert the expected output
assert.Nil(err)
assert.Equal("default", returnConfigName)
assert.Nil(err)

// Test case 2
configName = "config1"
Expand All @@ -52,8 +52,8 @@ func TestSelectConfig(t *testing.T) {
returnConfigName, err = wfcImpl.SelectConfig(workflowsBatch)

// Assert the expected output
assert.Nil(err)
assert.Equal("config1", returnConfigName)
assert.Nil(err)

// Test case 3 - selection of non-existing config when default config exists
configName = "notInConfigs"
Expand All @@ -66,8 +66,8 @@ func TestSelectConfig(t *testing.T) {
returnConfigName, err = wfcImpl.SelectConfig(workflowsBatch)

// Assert the expected output
assert.Nil(err)
assert.Equal("default", returnConfigName)
assert.NotNil(err)

// Test case 4 - selection of non-existing config when default config not exists
configName = "notInConfig"
Expand All @@ -91,7 +91,6 @@ func TestSelectConfig(t *testing.T) {
returnConfigName, err = wfcImpl4.SelectConfig(workflowsBatch)

// Assert the expected output
assert.Nil(err)
assert.NotNil(returnConfigName)

assert.NotNil(err)
}
4 changes: 4 additions & 0 deletions pkg/workflow_handler/workflows_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ func IsConfigExists(cfg *conf.WorkflowsConfig, config string) bool {
_, ok := cfg.Configs[config]
return ok
}

func IsConfigsOnExitExists(cfg *conf.WorkflowsConfig, config string) bool {
return len(cfg.Configs[config].OnExit) != 0
}