Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KIE Tools #2160 - Fix resources generation prefix order #398

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading