Skip to content

Commit

Permalink
KIE Tools #2160 - Fix resources generation prefix order (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardozanini authored Feb 16, 2024
1 parent 775e630 commit 731b2d4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
4 changes: 2 additions & 2 deletions workflowproj/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
17 changes: 10 additions & 7 deletions workflowproj/workflowproj.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 32 additions & 10 deletions workflowproj/workflowproj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 731b2d4

Please sign in to comment.