From 4d125c4e2ef4ede12c225abbdc72fdb2f40767ae Mon Sep 17 00:00:00 2001 From: Ricardo Zanini <1538000+ricardozanini@users.noreply.github.com> Date: Wed, 13 Dec 2023 11:46:10 -0300 Subject: [PATCH] Fix #317 - Make namespace an optional parameter when generating manifests in workflowproj (#320) Signed-off-by: Ricardo Zanini --- workflowproj/workflowproj.go | 20 +++++++--- workflowproj/workflowproj_test.go | 65 ++++++++++++++++++------------- 2 files changed, 53 insertions(+), 32 deletions(-) diff --git a/workflowproj/workflowproj.go b/workflowproj/workflowproj.go index 039527156..3b46f8c95 100644 --- a/workflowproj/workflowproj.go +++ b/workflowproj/workflowproj.go @@ -49,6 +49,8 @@ type WorkflowProjectHandler interface { // Named overwrites the workflow ID. The handler will use this name instead to generate the manifests name. // Remember that together with the Namespace, the Name is the unique key of a Kubernetes object. Named(name string) WorkflowProjectHandler + // Profile overrides the default profile (dev) in the generated SonataFlow manifest + Profile(profile metadata.ProfileType) WorkflowProjectHandler // WithWorkflow reader for a file or the content stream of a workflow definition. WithWorkflow(reader io.Reader) WorkflowProjectHandler // WithAppProperties reader for a file or the content stream of a workflow application properties. @@ -81,7 +83,6 @@ type resource struct { // New is the entry point for this package. // You can create a new handler with the given namespace, meaning that every manifest generated will use this namespace. -// namespace is a required parameter. func New(namespace string) WorkflowProjectHandler { s := scheme.Scheme utilruntime.Must(operatorapi.AddToScheme(s)) @@ -96,6 +97,7 @@ func New(namespace string) WorkflowProjectHandler { type workflowProjectHandler struct { name string namespace string + profile metadata.ProfileType scheme *runtime.Scheme project WorkflowProject rawWorkflow io.Reader @@ -110,6 +112,12 @@ func (w *workflowProjectHandler) Named(name string) WorkflowProjectHandler { return w } +func (w *workflowProjectHandler) Profile(profile metadata.ProfileType) WorkflowProjectHandler { + w.profile = profile + w.parsed = false + return w +} + func (w *workflowProjectHandler) WithWorkflow(reader io.Reader) WorkflowProjectHandler { w.rawWorkflow = reader w.parsed = false @@ -190,9 +198,6 @@ func (w *workflowProjectHandler) parseRawProject() error { } func (w *workflowProjectHandler) sanityCheck() error { - if len(w.namespace) == 0 { - return errors.New("Namespace is required when building Workflow projects") - } if w.rawWorkflow == nil { return errors.New("A workflow reader pointer is required when building Workflow projects") } @@ -221,8 +226,11 @@ func (w *workflowProjectHandler) parseRawWorkflow() error { w.project.Workflow, err = operatorapi.FromCNCFWorkflow(workflowDef, context.TODO()) w.project.Workflow.Name = w.name w.project.Workflow.Namespace = w.namespace - - SetWorkflowProfile(w.project.Workflow, metadata.DevProfile) + profile := metadata.DevProfile + if len(w.profile) > 0 { + profile = w.profile + } + SetWorkflowProfile(w.project.Workflow, profile) SetDefaultLabels(w.project.Workflow, w.project.Workflow) if err = SetTypeToObject(w.project.Workflow, w.scheme); err != nil { return err diff --git a/workflowproj/workflowproj_test.go b/workflowproj/workflowproj_test.go index dec1fc6f9..bb0daa8a8 100644 --- a/workflowproj/workflowproj_test.go +++ b/workflowproj/workflowproj_test.go @@ -27,6 +27,7 @@ import ( "strings" "testing" + "github.com/apache/incubator-kie-kogito-serverless-operator/api/metadata" "github.com/stretchr/testify/assert" "k8s.io/client-go/kubernetes/scheme" ) @@ -36,10 +37,13 @@ func Test_Handler_WorkflowMinimal(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, proj) assert.Equal(t, "hello", proj.Workflow.Name) + assert.Equal(t, string(metadata.DevProfile), proj.Workflow.Annotations[metadata.Profile]) } func Test_Handler_WorkflowMinimalInvalid(t *testing.T) { - proj, err := New("default").WithWorkflow(getWorkflowMinimalInvalid()).AsObjects() + proj, err := New("default"). + WithWorkflow(getWorkflowMinimalInvalid()). + AsObjects() assert.Error(t, err) assert.Nil(t, proj) } @@ -47,6 +51,7 @@ func Test_Handler_WorkflowMinimalInvalid(t *testing.T) { func Test_Handler_WorkflowMinimalAndProps(t *testing.T) { proj, err := New("default"). Named("minimal"). + Profile(metadata.ProdProfile). WithWorkflow(getWorkflowMinimal()). WithAppProperties(getWorkflowProperties()). AsObjects() @@ -55,6 +60,7 @@ func Test_Handler_WorkflowMinimalAndProps(t *testing.T) { assert.NotNil(t, proj.Properties) assert.Equal(t, "minimal", proj.Workflow.Name) assert.Equal(t, "minimal-props", proj.Properties.Name) + assert.Equal(t, string(metadata.ProdProfile), proj.Workflow.Annotations[metadata.Profile]) assert.NotEmpty(t, proj.Properties.Data) } @@ -144,33 +150,40 @@ func Test_Handler_WorklflowServiceAndPropsAndSpec_SaveAs(t *testing.T) { } func Test_Handler_WorkflowService_SaveAs(t *testing.T) { - handler := New("default"). - WithWorkflow(getWorkflowService()) - - proj, err := handler.AsObjects() - assert.NoError(t, err) - assert.NotNil(t, proj.Workflow) - - tmpPath, err := os.MkdirTemp("", "*-test") - assert.NoError(t, err) - defer os.RemoveAll(tmpPath) - - assert.NoError(t, handler.SaveAsKubernetesManifests(tmpPath)) - files, err := os.ReadDir(tmpPath) - assert.NoError(t, err) - assert.Len(t, files, 1) - - for _, f := range files { - if strings.HasSuffix(f.Name(), yamlFileExt) { - contents, err := os.ReadFile(path.Join(tmpPath, f.Name())) - assert.NoError(t, err) - decode := scheme.Codecs.UniversalDeserializer().Decode - k8sObj, _, err := decode(contents, nil, nil) - assert.NoError(t, err) - assert.NotNil(t, k8sObj) - assert.NotEmpty(t, k8sObj.GetObjectKind().GroupVersionKind().String()) + testRun := func(t *testing.T, handler WorkflowProjectHandler) { + proj, err := handler.AsObjects() + assert.NoError(t, err) + assert.NotNil(t, proj.Workflow) + + tmpPath, err := os.MkdirTemp("", "*-test") + assert.NoError(t, err) + defer os.RemoveAll(tmpPath) + + assert.NoError(t, handler.SaveAsKubernetesManifests(tmpPath)) + files, err := os.ReadDir(tmpPath) + assert.NoError(t, err) + assert.Len(t, files, 1) + + for _, f := range files { + if strings.HasSuffix(f.Name(), yamlFileExt) { + contents, err := os.ReadFile(path.Join(tmpPath, f.Name())) + assert.NoError(t, err) + decode := scheme.Codecs.UniversalDeserializer().Decode + k8sObj, _, err := decode(contents, nil, nil) + assert.NoError(t, err) + assert.NotNil(t, k8sObj) + assert.NotEmpty(t, k8sObj.GetObjectKind().GroupVersionKind().String()) + } } } + + t.Run("SaveAs in default namespace", func(t *testing.T) { + testRun(t, New("default").WithWorkflow(getWorkflowService())) + }) + + t.Run("SaveAs with empty namespace namespace", func(t *testing.T) { + testRun(t, New("").WithWorkflow(getWorkflowService())) + }) } func getWorkflowMinimalInvalid() io.Reader {