From 1dcb956a9d145409651fabae2092edf5548401ad Mon Sep 17 00:00:00 2001 From: Ricardo Zanini <1538000+ricardozanini@users.noreply.github.com> Date: Fri, 16 Feb 2024 10:17:20 -0300 Subject: [PATCH] SRVLOGIC-227: KIE Tools #2160 - Fix resources generation prefix order - KIE Tools #2160 - Fix resources generation prefix order (#398) (cherry picked from commit 731b2d464deade64c18a1e08ed8ca86d85a77b9c) --- workflowproj/io.go | 4 +-- workflowproj/workflowproj.go | 17 +++++++------ workflowproj/workflowproj_test.go | 42 +++++++++++++++++++++++-------- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/workflowproj/io.go b/workflowproj/io.go index 39b637076..152d37bee 100644 --- a/workflowproj/io.go +++ b/workflowproj/io.go @@ -42,11 +42,11 @@ func ensurePath(path string) error { return nil } -func saveAsKubernetesManifest(object client.Object, savePath, prefix string) error { +func saveAsKubernetesManifest(object client.Object, savePath string, prefix int) error { if reflect.ValueOf(object).IsNil() { return nil } - filename := strings.ToLower(fmt.Sprintf("%s%s_%s%s", + filename := strings.ToLower(fmt.Sprintf("%02d-%s_%s%s", prefix, object.GetObjectKind().GroupVersionKind().Kind, object.GetName(), diff --git a/workflowproj/workflowproj.go b/workflowproj/workflowproj.go index afabd7e5c..30d9db74c 100644 --- a/workflowproj/workflowproj.go +++ b/workflowproj/workflowproj.go @@ -153,18 +153,21 @@ func (w *workflowProjectHandler) SaveAsKubernetesManifests(path string) error { if err := w.parseRawProject(); err != nil { return err } - fileCount := 1 - if err := saveAsKubernetesManifest(w.project.Workflow, path, fmt.Sprintf("%02d-", 1)); err != nil { - return err + fileCount := 0 + if w.project.Properties != nil { + fileCount++ + if err := saveAsKubernetesManifest(w.project.Properties, path, fileCount); err != nil { + return err + } } - for i, r := range w.project.Resources { - fileCount = i + 1 - if err := saveAsKubernetesManifest(r, path, fmt.Sprintf("%02d-", fileCount)); err != nil { + for _, r := range w.project.Resources { + fileCount++ + if err := saveAsKubernetesManifest(r, path, fileCount); err != nil { return err } } fileCount++ - if err := saveAsKubernetesManifest(w.project.Properties, path, fmt.Sprintf("%02d-", fileCount)); err != nil { + if err := saveAsKubernetesManifest(w.project.Workflow, path, fileCount); err != nil { return err } return nil diff --git a/workflowproj/workflowproj_test.go b/workflowproj/workflowproj_test.go index c1207527f..9135e488d 100644 --- a/workflowproj/workflowproj_test.go +++ b/workflowproj/workflowproj_test.go @@ -28,6 +28,8 @@ import ( "strings" "testing" + "k8s.io/apimachinery/pkg/runtime/schema" + "github.com/apache/incubator-kie-kogito-serverless-operator/api/metadata" "github.com/stretchr/testify/assert" "k8s.io/client-go/kubernetes/scheme" @@ -140,19 +142,36 @@ func Test_Handler_WorklflowServiceAndPropsAndSpec_SaveAs(t *testing.T) { assert.NoError(t, err) assert.Len(t, files, 4) - 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()) - } + expectedFiles := []string{ + "01-configmap_service-props.yaml", + "02-configmap_01-service-resources.yaml", + "03-configmap_02-service-resources.yaml", + "04-sonataflow_service.yaml", + } + expectedKinds := []schema.GroupVersionKind{ + {Group: "", Version: "v1", Kind: "ConfigMap"}, + {Group: "", Version: "v1", Kind: "ConfigMap"}, + {Group: "", Version: "v1", Kind: "ConfigMap"}, + {Group: "sonataflow.org", Version: "v1alpha08", Kind: "SonataFlow"}, + } + + for i := 0; i < len(files); i++ { + assert.Equal(t, files[i].Name(), expectedFiles[i]) + assertIsK8sObject(t, tmpPath, files[i].Name(), expectedKinds[i]) } } +func assertIsK8sObject(t *testing.T, basePath string, fileName string, gvk schema.GroupVersionKind) { + contents, err := os.ReadFile(path.Join(basePath, fileName)) + 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()) + assert.Equal(t, gvk, k8sObj.GetObjectKind().GroupVersionKind()) +} + func Test_Handler_WorkflowService_SaveAs(t *testing.T) { testRun := func(t *testing.T, handler WorkflowProjectHandler) { proj, err := handler.AsObjects() @@ -170,6 +189,9 @@ func Test_Handler_WorkflowService_SaveAs(t *testing.T) { for _, f := range files { if strings.HasSuffix(f.Name(), yamlFileExt) { + // we have only one file produced in these test cases + prefix := fmt.Sprintf("%02d-", 1) + assert.True(t, strings.HasPrefix(f.Name(), prefix)) contents, err := os.ReadFile(path.Join(tmpPath, f.Name())) assert.NoError(t, err) decode := scheme.Codecs.UniversalDeserializer().Decode