Skip to content

Commit

Permalink
[KOGITO-7856] Fixes to export a Kogito compliant JSON workflow defini…
Browse files Browse the repository at this point in the history
…tion (apache#25)

Signed-off-by: Davide Salerno <[email protected]>
  • Loading branch information
davidesalerno authored Oct 5, 2022
1 parent d18bbc4 commit c5c8b34
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 19 deletions.
24 changes: 24 additions & 0 deletions api/v08/kogitoserverlessworkflow_support.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package v08

import "fmt"

func (e StateType) String() string {
switch e {
case EventStateType:
return "event"
case OperationStateType:
return "operation"
case SwitchStateType:
return "switch"
case SleepStateType:
return "sleep"
case ParallelStateType:
return "parallel"
case InjectStateType:
return "inject"
case ForEachStateType:
return "foreach"
default:
return fmt.Sprintf("%s", string(e))
}
}
26 changes: 19 additions & 7 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,28 @@
package builder

import (
"context"
"fmt"
"github.com/davidesalerno/kogito-serverless-operator/constants"
"github.com/ricardozanini/kogito-builder/api"
"github.com/ricardozanini/kogito-builder/builder"
"github.com/ricardozanini/kogito-builder/client"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
"time"
)

func BuildImageWithDefaults(sourceSwfName string, sourceSwf []byte) (*api.Build, error) {
type Builder struct {
ctx context.Context
}

// NewBuilder ...
func NewBuilder(contex context.Context) Builder {
return Builder{ctx: contex}
}

func (b *Builder) BuildImageWithDefaults(sourceSwfName string, sourceSwf []byte) (*api.Build, error) {
wd, _ := os.Getwd()
dockerFile, _ := os.ReadFile(wd + "/builder/Dockerfile")
ib := NewImageBuilder(sourceSwfName, sourceSwf, dockerFile)
Expand All @@ -37,10 +48,11 @@ func BuildImageWithDefaults(sourceSwfName string, sourceSwf []byte) (*api.Build,
ib.WithSecret(constants.DEFAULT_KANIKO_SECRET)
ib.WithRegistryAddress(constants.DEFAULT_REGISTRY_REPO)
ib.WithTimeout(5 * time.Minute)
return BuildImage(ib.Build())
return b.BuildImage(ib.Build())
}

func BuildImage(b KogitoBuilder) (*api.Build, error) {
func (r *Builder) BuildImage(b KogitoBuilder) (*api.Build, error) {
log := ctrllog.FromContext(r.ctx)
cli, err := client.NewClient(true)
platform := api.PlatformBuild{
ObjectReference: api.ObjectReference{
Expand All @@ -62,22 +74,22 @@ func BuildImage(b KogitoBuilder) (*api.Build, error) {
}

build, err := builder.NewBuild(platform, b.ImageName, b.PodMiddleName).
WithResource(constants.BUILDER_RESOURCE_NAME_DEFAULT, b.DockerFile).WithResource(b.SourceSwfName, b.SourceSwf).
WithResource(constants.BUILDER_RESOURCE_NAME_DEFAULT, b.DockerFile).WithResource(b.SourceSwfName+".sw.json", b.SourceSwf).
WithClient(cli).
Schedule()
if err != nil {
fmt.Println(err.Error())
log.Error(err, err.Error())
return nil, err
}
//FIXME: Remove this For loop as soon as the KogitoServerlessBuild CR will be availbable
// from now the Reconcile method can be called until the build is finished
for build.Status.Phase != api.BuildPhaseSucceeded &&
build.Status.Phase != api.BuildPhaseError &&
build.Status.Phase != api.BuildPhaseFailed {
fmt.Printf("\nBuild status is %s", build.Status.Phase)
log.Info("Build status is ", "status", build.Status.Phase)
build, err = builder.FromBuild(build).WithClient(cli).Reconcile()
if err != nil {
fmt.Println("Failed to run test")
log.Info("Failed to build")
panic(fmt.Errorf("build %v just failed", build))
}
time.Sleep(10 * time.Second)
Expand Down
4 changes: 3 additions & 1 deletion builder/builder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package builder

import (
"context"
"github.com/davidesalerno/kogito-serverless-operator/constants"
"github.com/stretchr/testify/assert"
"os"
Expand Down Expand Up @@ -31,7 +32,8 @@ func TestBuild(t *testing.T) {

ib.WithTimeout(5 * time.Minute)

build, error := BuildImage(ib.Build())
builder := NewBuilder(context.TODO())
build, error := builder.BuildImage(ib.Build())
// after some build minikube need more time for the schedule
time.Sleep(20 * time.Second)
assert.NotNilf(t, build, "Build result")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ spec:
- name: ChooseOnLanguage
type: switch
dataConditions:
- condition: ${ .language == "English" }
- condition: "${ .language == \"English\" }"
transition: GreetInEnglish
- condition: ${ .language == "Spanish" }
- condition: "${ .language == \"Spanish\" }"
transition: GreetInSpanish
defaultCondition: GreetInEnglish
- name: GreetInEnglish
type: inject
data:
greeting: "Hello from k8s Workflow!"
greeting: "Hello from JSON Workflow, "
transition: GreetPerson
- name: GreetInSpanish
type: inject
data:
greeting: "Saludos desde k8s Workflow!"
greeting: "Saludos desde JSON Workflow, "
transition: GreetPerson
- name: GreetPerson
type: operation
Expand All @@ -36,5 +36,5 @@ spec:
functionRef:
refName: greetFunction
arguments:
message: .greeting + .name
message: ".greeting+.name"
end: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
apiVersion: sw.kogito.kie.org/v08
kind: KogitoServerlessWorkflow
metadata:
name: greeting
annotations:
sw.kogito.kie.org/description: Greeting example on k8s!
spec:
start: ChooseOnLanguage
functions:
- name: greetFunction
type: custom
operation: sysout
states:
- name: ChooseOnLanguage
type: switch
dataConditions:
- condition: "${ .language == \"English\" }"
transition: GreetInEnglish
- condition: "${ .language == \"Spanish\" }"
transition: GreetInSpanish
- condition: "${ .language == \"Italian\" }"
transition: GreetInItalian
defaultCondition: GreetInEnglish
- name: GreetInEnglish
type: inject
data:
greeting: "Hello from JSON Workflow, "
transition: GreetPerson
- name: GreetInSpanish
type: inject
data:
greeting: "Saludos desde JSON Workflow, "
transition: GreetPerson
- name: GreetInItalian
type: inject
data:
greeting: "Saluti dal JSON Workflow, "
transition: GreetPerson
- name: GreetPerson
type: operation
actions:
- name: greetAction
functionRef:
refName: greetFunction
arguments:
message: ".greeting+.name"
end: true
14 changes: 9 additions & 5 deletions controllers/kogitoserverlessworkflow_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package controllers

import (
"context"
"encoding/json"
apiv08 "github.com/davidesalerno/kogito-serverless-operator/api/v08"
"github.com/davidesalerno/kogito-serverless-operator/builder"
"github.com/davidesalerno/kogito-serverless-operator/converters"
"github.com/ghodss/yaml"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -73,17 +73,21 @@ func (r *KogitoServerlessWorkflowReconciler) Reconcile(ctx context.Context, req
log.Error(err, "Failed converting KogitoServerlessWorkflow into Workflow")
return ctrl.Result{}, err
}
yamlWorkflow, err := yaml.Marshal(workflow)
jsonWorkflow, err := json.Marshal(workflow)
if err != nil {
log.Error(err, "Failed converting KogitoServerlessWorkflow into YAML")
log.Error(err, "Failed converting KogitoServerlessWorkflow into JSON")
return ctrl.Result{}, err
}
log.Info("Converted Workflow CR into Kogito Yaml Workflow", "workflow", yamlWorkflow)
log.Info("Converted Workflow CR into Kogito JSON Workflow", "workflow", jsonWorkflow)
//TODO Save into Shared Volume
//"greetings.sw.json"
//TODO KOGITO-7498 Kogito Serverless Workflow Builder Image
//[KOGITO-7899]-Integrate Kaniko into SWF Operator
builder.BuildImageWithDefaults(workflow.ID, yamlWorkflow)
builder := builder.NewBuilder(ctx)
_, err = builder.BuildImageWithDefaults(workflow.ID, jsonWorkflow)
if err != nil {
log.Info("Error building KogitoServerlessWorkflow into Workflow: ", "message", err.Error())
}
return ctrl.Result{}, nil
}

Expand Down
8 changes: 7 additions & 1 deletion converters/kogitoserverlessworkflow_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,16 @@ func retrieveStates(incomingStates []apiv08.State) []model.State {
states := make([]model.State, len(incomingStates))
log.Info("States: ", "states", incomingStates)
for i, s := range incomingStates {
newBaseState := &model.BaseState{Name: s.Name}
stateT := model.StateType(s.Type.String())
newBaseState := &model.BaseState{Name: s.Name, Type: stateT}
if s.End {
newBaseState.End = &model.End{Terminate: true}
}
if s.Transition != nil {
newBaseState.Transition = &model.Transition{
NextState: *s.Transition,
}
}
switch sType := s.Type; sType {
case "switch":
newBaseSwitchState := &model.BaseSwitchState{
Expand Down

0 comments on commit c5c8b34

Please sign in to comment.