diff --git a/api/v08/kogitoserverlessworkflow_support.go b/api/v08/kogitoserverlessworkflow_support.go new file mode 100644 index 00000000000..a36addf9768 --- /dev/null +++ b/api/v08/kogitoserverlessworkflow_support.go @@ -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)) + } +} diff --git a/builder/builder.go b/builder/builder.go index 274752ebb9b..5d253606c6d 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -16,6 +16,7 @@ package builder import ( + "context" "fmt" "github.com/davidesalerno/kogito-serverless-operator/constants" "github.com/ricardozanini/kogito-builder/api" @@ -23,10 +24,20 @@ import ( "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) @@ -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{ @@ -62,11 +74,11 @@ 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 @@ -74,10 +86,10 @@ func BuildImage(b KogitoBuilder) (*api.Build, error) { 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) diff --git a/builder/builder_test.go b/builder/builder_test.go index 73912adc6ae..1161b971eb2 100644 --- a/builder/builder_test.go +++ b/builder/builder_test.go @@ -1,6 +1,7 @@ package builder import ( + "context" "github.com/davidesalerno/kogito-serverless-operator/constants" "github.com/stretchr/testify/assert" "os" @@ -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") diff --git a/config/samples/sw.kogito.kie.org__v08_kogitoserverlessworkflow.yaml b/config/samples/sw.kogito.kie.org__v08_kogitoserverlessworkflow.yaml index a5aac2dbab5..ba8b498b631 100644 --- a/config/samples/sw.kogito.kie.org__v08_kogitoserverlessworkflow.yaml +++ b/config/samples/sw.kogito.kie.org__v08_kogitoserverlessworkflow.yaml @@ -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 @@ -36,5 +36,5 @@ spec: functionRef: refName: greetFunction arguments: - message: .greeting + .name + message: ".greeting+.name" end: true diff --git a/config/samples/sw.kogito.kie.org__v08_kogitoserverlessworkflow_withITA.yaml b/config/samples/sw.kogito.kie.org__v08_kogitoserverlessworkflow_withITA.yaml new file mode 100644 index 00000000000..acf8a8fccc0 --- /dev/null +++ b/config/samples/sw.kogito.kie.org__v08_kogitoserverlessworkflow_withITA.yaml @@ -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 diff --git a/controllers/kogitoserverlessworkflow_controller.go b/controllers/kogitoserverlessworkflow_controller.go index b440d0e36fd..fb0bea8dd65 100644 --- a/controllers/kogitoserverlessworkflow_controller.go +++ b/controllers/kogitoserverlessworkflow_controller.go @@ -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" @@ -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 } diff --git a/converters/kogitoserverlessworkflow_converter.go b/converters/kogitoserverlessworkflow_converter.go index a3e82eb4be2..1999f097c86 100644 --- a/converters/kogitoserverlessworkflow_converter.go +++ b/converters/kogitoserverlessworkflow_converter.go @@ -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{