diff --git a/pkg/workflow_handler/types.go b/pkg/workflow_handler/types.go index 194db00d..b9c4828c 100644 --- a/pkg/workflow_handler/types.go +++ b/pkg/workflow_handler/types.go @@ -10,7 +10,7 @@ type WorkflowsClient interface { ConstructTemplates(workflowsBatch *common.WorkflowsBatch, configName string) ([]v1alpha1.Template, error) ConstructSpec(templates []v1alpha1.Template, params []v1alpha1.Parameter, configName string) (*v1alpha1.WorkflowSpec, error) CreateWorkflow(spec *v1alpha1.WorkflowSpec, workflowsBatch *common.WorkflowsBatch) (*v1alpha1.Workflow, error) - SetConfig(wf *v1alpha1.Workflow, spec *v1alpha1.WorkflowSpec) error + SelectConfig(workflowsBatch *common.WorkflowsBatch) (string, error) Lint(wf *v1alpha1.Workflow) error Submit(ctx *context.Context, wf *v1alpha1.Workflow) error HandleWorkflowBatch(ctx *context.Context, workflowsBatch *common.WorkflowsBatch) error diff --git a/pkg/workflow_handler/workflows.go b/pkg/workflow_handler/workflows.go index 22556ab0..f1fc68eb 100644 --- a/pkg/workflow_handler/workflows.go +++ b/pkg/workflow_handler/workflows.go @@ -108,9 +108,38 @@ func (wfc *WorkflowsClientImpl) CreateWorkflow(spec *v1alpha1.WorkflowSpec, work return workflow, nil } -func (wfc *WorkflowsClientImpl) SetConfig(wf *v1alpha1.Workflow, spec *v1alpha1.WorkflowSpec) error { - //TODO implement me - panic("implement me") +func (wfc *WorkflowsClientImpl) SelectConfig(workflowsBatch *common.WorkflowsBatch) (string, error) { + var configName string + ok := IsConfigExists(&wfc.cfg.WorkflowsConfig, "default") + if ok { + 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 { + 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( + "error in selecting config, staying with default config for repo %s branch %s", + workflowsBatch.Payload.Repo, + workflowsBatch.Payload.Branch, + ) // Error + } + } + + return configName, nil } func (wfc *WorkflowsClientImpl) Lint(wf *v1alpha1.Workflow) error { @@ -129,13 +158,10 @@ func (wfc *WorkflowsClientImpl) Submit(ctx *context.Context, wf *v1alpha1.Workfl func (wfc *WorkflowsClientImpl) HandleWorkflowBatch(ctx *context.Context, workflowsBatch *common.WorkflowsBatch) error { var params []v1alpha1.Parameter - var configName string - _, ok := wfc.cfg.WorkflowsConfig.Configs["default"] - if ok { - configName = "default" - } - if *workflowsBatch.Config != "" { - configName = *workflowsBatch.Config + + configName, err := wfc.SelectConfig(workflowsBatch) + if err != nil { + return err } templates, err := wfc.ConstructTemplates(workflowsBatch, configName) diff --git a/pkg/workflow_handler/workflows_test.go b/pkg/workflow_handler/workflows_test.go new file mode 100644 index 00000000..f3ed4e5a --- /dev/null +++ b/pkg/workflow_handler/workflows_test.go @@ -0,0 +1,97 @@ +package workflow_handler + +import ( + "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" + "github.com/rookout/piper/pkg/common" + "github.com/rookout/piper/pkg/conf" + "github.com/rookout/piper/pkg/git_provider" + assertion "github.com/stretchr/testify/assert" + "testing" +) + +func TestSelectConfig(t *testing.T) { + var wfc *conf.WorkflowsConfig + + assert := assertion.New(t) + // Create a sample WorkflowsBatch object for testing + configName := "default" + workflowsBatch := &common.WorkflowsBatch{ + Config: &configName, // Set the desired config name here + Payload: &git_provider.WebhookPayload{}, + } + + // Create a mock WorkflowsClientImpl object with necessary dependencies + wfc = &conf.WorkflowsConfig{Configs: map[string]*conf.ConfigInstance{ + "default": {Spec: v1alpha1.WorkflowSpec{}, + OnExit: []v1alpha1.DAGTask{}}, + "config1": {Spec: v1alpha1.WorkflowSpec{}, + OnExit: []v1alpha1.DAGTask{}}, + }} + + wfcImpl := &WorkflowsClientImpl{ + cfg: &conf.GlobalConfig{ + WorkflowsConfig: *wfc, + }, + } + + // Call the SelectConfig function + returnConfigName, err := wfcImpl.SelectConfig(workflowsBatch) + + // Assert the expected output + assert.Nil(err) + assert.Equal("default", returnConfigName) + + // Test case 2 + configName = "config1" + workflowsBatch = &common.WorkflowsBatch{ + Config: &configName, // Set the desired config name here + Payload: &git_provider.WebhookPayload{}, + } + + // Call the SelectConfig function + returnConfigName, err = wfcImpl.SelectConfig(workflowsBatch) + + // Assert the expected output + assert.Nil(err) + assert.Equal("config1", returnConfigName) + + // Test case 3 - selection of non-existing config when default config exists + configName = "notInConfigs" + workflowsBatch = &common.WorkflowsBatch{ + Config: &configName, // Set the desired config name here + Payload: &git_provider.WebhookPayload{}, + } + + // Call the SelectConfig function + returnConfigName, err = wfcImpl.SelectConfig(workflowsBatch) + + // Assert the expected output + assert.Nil(err) + assert.Equal("default", returnConfigName) + + // Test case 4 - selection of non-existing config when default config not exists + configName = "notInConfig" + workflowsBatch = &common.WorkflowsBatch{ + Config: &configName, // Set the desired config name here + Payload: &git_provider.WebhookPayload{}, + } + + wfc4 := &conf.WorkflowsConfig{Configs: map[string]*conf.ConfigInstance{ + "config1": {Spec: v1alpha1.WorkflowSpec{}, + OnExit: []v1alpha1.DAGTask{}}, + }} + + wfcImpl4 := &WorkflowsClientImpl{ + cfg: &conf.GlobalConfig{ + WorkflowsConfig: *wfc4, + }, + } + + // Call the SelectConfig function + returnConfigName, err = wfcImpl4.SelectConfig(workflowsBatch) + + // Assert the expected output + assert.Nil(err) + assert.NotNil(returnConfigName) + +} diff --git a/pkg/workflow_handler/workflows_utils.go b/pkg/workflow_handler/workflows_utils.go index 843a1483..11661a9e 100644 --- a/pkg/workflow_handler/workflows_utils.go +++ b/pkg/workflow_handler/workflows_utils.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1" + "github.com/rookout/piper/pkg/conf" "github.com/rookout/piper/pkg/git_provider" "github.com/rookout/piper/pkg/utils" "gopkg.in/yaml.v3" @@ -63,3 +64,8 @@ func GetParameters(paramsFile *git_provider.CommitFile) ([]v1alpha1.Parameter, e } return params, nil } + +func IsConfigExists(cfg *conf.WorkflowsConfig, config string) bool { + _, ok := cfg.Configs[config] + return ok +}