Skip to content

Commit

Permalink
Fix apache#317 - Make namespace an optional parameter when generating…
Browse files Browse the repository at this point in the history
… manifests in workflowproj (apache#320)

Signed-off-by: Ricardo Zanini <[email protected]>
  • Loading branch information
ricardozanini authored and rgdoliveira committed Jan 29, 2024
1 parent ff9a12d commit 5dc9df2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
20 changes: 14 additions & 6 deletions workflowproj/workflowproj.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
Expand Down
65 changes: 39 additions & 26 deletions workflowproj/workflowproj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -36,17 +37,21 @@ 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)
}

func Test_Handler_WorkflowMinimalAndProps(t *testing.T) {
proj, err := New("default").
Named("minimal").
Profile(metadata.ProdProfile).
WithWorkflow(getWorkflowMinimal()).
WithAppProperties(getWorkflowProperties()).
AsObjects()
Expand All @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5dc9df2

Please sign in to comment.