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

[issue-547] Knative Broker is required even if is not defined in SonataFlow or SonataFlowPlatform #553

Merged
merged 1 commit into from
Oct 31, 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
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ test: manifests generate envtest test-api ## Run tests.
@$(MAKE) vet
@$(MAKE) fmt
@echo "🔍 Running controller tests..."
@KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" \
go test $(shell go list ./... | grep -v /test/) -coverprofile cover.out > /dev/null 2>&1
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $(shell go list ./... | grep -v /test/) -coverprofile cover.out
@echo "✅ Tests completed successfully. Coverage report generated: cover.out."

.PHONY: test-api
Expand Down
17 changes: 3 additions & 14 deletions internal/controller/profiles/common/object_creators.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/profiles"

"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/workflowdef"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"

cncfmodel "github.com/serverlessworkflow/sdk-go/v2/model"
Expand All @@ -46,7 +45,6 @@ import (

operatorapi "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/knative"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/platform/services"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/profiles/common/constants"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/profiles/common/persistence"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/profiles/common/properties"
Expand Down Expand Up @@ -281,14 +279,7 @@ func SinkBindingCreator(workflow *operatorapi.SonataFlow, plf *operatorapi.Sonat
if err != nil {
return nil, err
}
dataIndexEnabled := services.IsDataIndexEnabled(plf)
jobServiceEnabled := services.IsJobServiceEnabled(plf)

// skip if no produced event is found and there is no DataIndex/JobService enabled
if sink == nil {
if dataIndexEnabled || jobServiceEnabled || workflowdef.ContainsEventKind(workflow, cncfmodel.EventKindProduced) {
return nil, fmt.Errorf("a sink in the SonataFlow %s or broker in the SonataFlowPlatform %s should be configured when DataIndex or JobService is enabled", workflow.Name, plf.Name)
}
return nil, nil /*nothing to do*/
}

Expand Down Expand Up @@ -385,11 +376,9 @@ func TriggersCreator(workflow *operatorapi.SonataFlow, plf *operatorapi.SonataFl
if err != nil {
return nil, err
}
if brokerRef == nil {
return nil, fmt.Errorf("no broker configured for eventType %s in SonataFlow %s", event.Type, workflow.Name)
}
if !knative.IsKnativeBroker(brokerRef) {
return nil, fmt.Errorf("no valid broker configured for eventType %s in SonataFlow %s", event.Type, workflow.Name)
if brokerRef == nil || !knative.IsKnativeBroker(brokerRef) {
// No broker configured for the eventType. Skip and will not create trigger for it.
continue
}
if err := knative.ValidateBroker(brokerRef.Name, brokerRef.Namespace); err != nil {
return nil, err
Expand Down
6 changes: 2 additions & 4 deletions internal/controller/profiles/common/object_creators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ func TestEnsureWorkflowSinkBindingWithoutBrokerAreNotCreated(t *testing.T) {
plf := test.GetBasePlatformWithBroker()
plf.Spec.Eventing = nil // No broker configured in the platform, but data index and jobs service are enabled
sinkBinding, err := SinkBindingCreator(workflow, plf)
assert.Error(t, err)
assert.Contains(t, err.Error(), "a sink in the SonataFlow vet or broker in the SonataFlowPlatform sonataflow-platform should be configured when DataIndex or JobService is enabled")
assert.NoError(t, err)
assert.Nil(t, sinkBinding)
}

Expand Down Expand Up @@ -371,8 +370,7 @@ func TestEnsureWorkflowTriggersWithoutBrokerAreNotCreated(t *testing.T) {
plf := test.GetBasePlatform()

triggers, err := TriggersCreator(workflow, plf)
assert.Error(t, err)
assert.Contains(t, err.Error(), "no broker configured for eventType events.vet.appointments in SonataFlow vet")
assert.NoError(t, err)
assert.Nil(t, triggers)
}

Expand Down
33 changes: 0 additions & 33 deletions internal/controller/profiles/preview/deployment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package preview

import (
"context"
"fmt"

"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/knative"
v1 "k8s.io/api/core/v1"
Expand All @@ -30,7 +29,6 @@ import (
"github.com/apache/incubator-kie-kogito-serverless-operator/api"
operatorapi "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/platform"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/platform/services"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/profiles/common"
"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/profiles/common/constants"
"github.com/apache/incubator-kie-kogito-serverless-operator/utils"
Expand Down Expand Up @@ -58,11 +56,6 @@ func (d *DeploymentReconciler) reconcileWithImage(ctx context.Context, workflow
return reconcile.Result{Requeue: false}, nil, err
}

// Checks if the workflow has sink configured.
if requires, err := d.ensureKnativeSinkConfigured(workflow); requires || err != nil {
return reconcile.Result{Requeue: false}, nil, err
}

// Ensure objects
result, objs, err := d.ensureObjects(ctx, workflow, image)
if err != nil || result.Requeue {
Expand Down Expand Up @@ -99,32 +92,6 @@ func (d *DeploymentReconciler) ensureKnativeServingRequired(workflow *operatorap
return false, nil
}

// if Knative Eventing is available, the workflow should have a sink configured, or the platform should have a broker defined
func (d *DeploymentReconciler) ensureKnativeSinkConfigured(workflow *operatorapi.SonataFlow) (bool, error) {
avail, err := knative.GetKnativeAvailability(d.Cfg)
if err != nil {
return true, err
}
if !avail.Eventing {
return false, nil
}
platform, err := platform.GetActivePlatform(context.TODO(), d.C, workflow.Namespace)
if err != nil {
return true, err
}
sink, err := knative.GetWorkflowSink(workflow, platform)
if err != nil {
return true, err
}
if sink == nil && (services.IsDataIndexEnabled(platform) || services.IsJobServiceEnabled(platform)) {
d.Recorder.Eventf(workflow, v1.EventTypeWarning,
"KnativeSinkNotConfigured",
"Failed to deploy workflow. No sink configured in the workflow or the platform when Job Service or Data Index Service is enabled.")
return true, fmt.Errorf("no sink configured in the workflow or the platform when Job Service or Data Index Service is enabled")
}
return false, nil
}

func (d *DeploymentReconciler) ensureObjects(ctx context.Context, workflow *operatorapi.SonataFlow, image string) (reconcile.Result, []client.Object, error) {
pl, _ := platform.GetActivePlatform(ctx, d.C, workflow.Namespace)
userPropsCM, _, err := d.ensurers.userPropsConfigMap.Ensure(ctx, workflow)
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/clusterplatform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ var _ = Describe("Cluster Platform Use Cases :: ", Label("cluster"), Ordered, fu
Expect(err).NotTo(HaveOccurred())
},
Entry("without services configured", test.GetPathFromE2EDirectory("platform", "noservices"), metadata.GitOpsProfile.String(), ephemeral, false),
Entry("with services configured", test.GetPathFromE2EDirectory("platform", "services"), metadata.GitOpsProfile.String(), "ephemeral-with-workflow", true),
Entry("with services configured and platform broker", test.GetPathFromE2EDirectory("platform", "services"), metadata.GitOpsProfile.String(), "ephemeral-with-workflow", true),
Entry("with services configured and no broker", test.GetPathFromE2EDirectory("platform", "services"), metadata.GitOpsProfile.String(), "ephemeral-with-workflow-no-broker", true),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this test does not have a SonataFlowClusterPlatform, I think the e2e label might be incorrect.

)

DescribeTable("against a platform in a separate namespace", func(testcaseDir string, profile string, persistenceType string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

apiVersion: sonataflow.org/v1alpha08
kind: SonataFlowPlatform
metadata:
name: sonataflow-platform
spec:
build:
config:
strategyOptions:
KanikoBuildCacheEnabled: "true"
services:
dataIndex:
enabled: true
jobService:
enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- 02-sonataflow_platform.yaml
- sonataflow/03-sonataflow_callbackstatetimeouts.sw.yaml

sortOptions:
order: fifo

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

apiVersion: sonataflow.org/v1alpha08
kind: SonataFlow
metadata:
name: callbackstatetimeouts
annotations:
sonataflow.org/description: Callback State Timeouts Example k8s
sonataflow.org/version: 0.0.1
sonataflow.org/profile: gitops
spec:
podTemplate:
replicas: 0
container:
image: replaceme
flow:
start: PrintStartMessage
events:
- name: callbackEvent
source: ''
type: callback_event_type
functions:
- name: systemOut
type: custom
operation: sysout
states:
- name: PrintStartMessage
type: operation
actions:
- name: printSystemOut
functionRef:
refName: systemOut
arguments:
message: "${\"callback-state-timeouts: \" + $WORKFLOW.instanceId + \" has started.\"}"
transition: CallbackState
- name: CallbackState
type: callback
action:
name: callbackAction
functionRef:
refName: systemOut
arguments:
message: "${\"callback-state-timeouts: \" + $WORKFLOW.instanceId + \" has executed the callbackFunction.\"}"
eventRef: callbackEvent
transition: CheckEventArrival
timeouts:
eventTimeout: PT30S
- name: CheckEventArrival
type: switch
dataConditions:
- condition: "${ .eventData != null }"
transition: EventArrived
defaultCondition:
transition: EventNotArrived
- name: EventArrived
type: inject
data:
exitMessage: "The callback event has arrived."
transition: PrintExitMessage
- name: EventNotArrived
type: inject
data:
exitMessage: "The callback event has not arrived, and the timeout has overdue."
transition: PrintExitMessage
- name: PrintExitMessage
type: operation
actions:
- name: printSystemOut
functionRef:
refName: systemOut
arguments:
message: "${\"callback-state-timeouts: \" + $WORKFLOW.instanceId + \" has finalized. \" + .exitMessage + \" eventData: \" + .eventData}"
end: true
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- 00-broker.yaml
- 02-sonataflow_platform.yaml
- sonataflow/03-sonataflow_callbackstatetimeouts.sw.yaml

Expand Down
Loading