ricardozanini commented on code in PR #2911: URL: https://github.com/apache/incubator-kie-tools/pull/2911#discussion_r1960070494
########## packages/sonataflow-operator/internal/controller/profiles/common/helper.go: ########## @@ -0,0 +1,125 @@ +// 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. + +package common + +import ( + "context" + "fmt" + + v2 "github.com/cloudevents/sdk-go/v2" + "k8s.io/klog/v2" + eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1" + "sigs.k8s.io/controller-runtime/pkg/client" + + operatorapi "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/v1alpha08" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/knative" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/platform" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/platform/services" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/profiles/common/constants" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/log" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/utils" +) + +// GetDataIndexPlatform returns the SonataFlowPlatform that declares the DataIndex service currently configured and +// enabled for the given workflow, if any. +func GetDataIndexPlatform(ctx context.Context, cli client.Client, workflow *operatorapi.SonataFlow) (*operatorapi.SonataFlowPlatform, error) { + var sfp *operatorapi.SonataFlowPlatform + var err error + + if sfp, err = platform.GetActivePlatform(ctx, cli, workflow.Namespace, false); err != nil { + return nil, err + } + if sfp == nil { + klog.V(log.I).Infof("No active platform was found for workflow: %s, namespace: %s. ", workflow.Name, workflow.Namespace) + return nil, nil + } + diHandler := services.NewDataIndexHandler(sfp) + if diHandler.IsServiceEnabledInSpec() { + return sfp, nil + } else { + // No DI enabled in current SFP, look for a potential SFCP + klog.V(log.I).Infof("DataIndex is not enabled for workflow: %s, platform: %s, namespace: %s. Looking if a cluster platform exists.", + workflow.Name, sfp.Name, workflow.Namespace) + if sfp, err = knative.GetRemotePlatform(sfp); err != nil { + return nil, err + } + if sfp == nil { + klog.V(log.I).Infof("No cluster platform was found for workflow: %s, namespace: %s", workflow.Name, workflow.Namespace) + return nil, nil + } + diHandler = services.NewDataIndexHandler(sfp) + if diHandler.IsServiceEnabledInSpec() { + return sfp, nil + } + return nil, nil Review Comment: Why not a LookupGlobalPlatform instead? That would perform the lookup for a local platform, if not found, a global one based on `opts` like `WithDataIndexEnabled`. ########## packages/sonataflow-operator/internal/controller/profiles/common/helper.go: ########## @@ -0,0 +1,125 @@ +// 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. + +package common + +import ( + "context" + "fmt" + + v2 "github.com/cloudevents/sdk-go/v2" + "k8s.io/klog/v2" + eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1" + "sigs.k8s.io/controller-runtime/pkg/client" + + operatorapi "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/v1alpha08" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/knative" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/platform" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/platform/services" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/profiles/common/constants" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/log" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/utils" +) + +// GetDataIndexPlatform returns the SonataFlowPlatform that declares the DataIndex service currently configured and +// enabled for the given workflow, if any. +func GetDataIndexPlatform(ctx context.Context, cli client.Client, workflow *operatorapi.SonataFlow) (*operatorapi.SonataFlowPlatform, error) { + var sfp *operatorapi.SonataFlowPlatform + var err error + + if sfp, err = platform.GetActivePlatform(ctx, cli, workflow.Namespace, false); err != nil { + return nil, err + } + if sfp == nil { + klog.V(log.I).Infof("No active platform was found for workflow: %s, namespace: %s. ", workflow.Name, workflow.Namespace) + return nil, nil + } + diHandler := services.NewDataIndexHandler(sfp) + if diHandler.IsServiceEnabledInSpec() { + return sfp, nil + } else { + // No DI enabled in current SFP, look for a potential SFCP + klog.V(log.I).Infof("DataIndex is not enabled for workflow: %s, platform: %s, namespace: %s. Looking if a cluster platform exists.", + workflow.Name, sfp.Name, workflow.Namespace) + if sfp, err = knative.GetRemotePlatform(sfp); err != nil { + return nil, err + } + if sfp == nil { + klog.V(log.I).Infof("No cluster platform was found for workflow: %s, namespace: %s", workflow.Name, workflow.Namespace) + return nil, nil + } + diHandler = services.NewDataIndexHandler(sfp) + if diHandler.IsServiceEnabledInSpec() { + return sfp, nil + } + return nil, nil + } +} + +// GetDataIndexBroker returns the broker being used by the DataIndex configured in the given SPF if any. +// Validation is performed to check that the Broker, when configured, is Ready. +func GetDataIndexBroker(sfp *operatorapi.SonataFlowPlatform) (*eventingv1.Broker, error) { + diHandler := services.NewDataIndexHandler(sfp) + brokerDest := diHandler.GetServiceSource() + if brokerDest != nil && len(brokerDest.Ref.Name) > 0 { + brokerNamespace := brokerDest.Ref.Namespace + if len(brokerNamespace) == 0 { + brokerNamespace = sfp.Namespace + } + klog.V(log.I).Infof("Broker: %s, namespace: %s is configured for DataIndex: %s in platform: %s, namespace: %s", + brokerDest.Ref.Name, brokerNamespace, diHandler.GetServiceName(), sfp.Name, sfp.Namespace) + if broker, err := knative.ValidateBroker(brokerDest.Ref.Name, brokerNamespace); err != nil { + return nil, err + } else { + return broker, nil + } + } + return nil, nil +} Review Comment: I'd also migrate these platform-related funcs to `internal/controller/platform`. ########## packages/sonataflow-operator/internal/controller/workflowdef/events.go: ########## @@ -0,0 +1,53 @@ +// 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. + +package workflowdef + +import ( + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/metadata" + + operatorapi "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/v1alpha08" + + cloudevents "github.com/cloudevents/sdk-go/v2" +) + +const SonataFlowOperatorSource = "sonataflow.org/operator" Review Comment: We have the domain constant in the `api` module. ########## packages/sonataflow-operator/internal/controller/profiles/preview/deployment_handler.go: ########## @@ -189,3 +203,40 @@ func (d *DeploymentReconciler) deploymentModelMutateVisitors( common.RestoreDeploymentVolumeAndVolumeMountMutateVisitor(), common.RolloutDeploymentIfCMChangedMutateVisitor(workflow, userPropsCM, managedPropsCM)} } + +func (d *DeploymentReconciler) notifyStatusUpdate(ctx context.Context, workflow *operatorapi.SonataFlow, + previousStatus operatorapi.SonataFlowStatus) error { + var err error + var sfp *operatorapi.SonataFlowPlatform + previousRunningCondition := previousStatus.GetCondition(api.RunningConditionType) + currentRunningCondition := workflow.Status.GetCondition(api.RunningConditionType) + + if previousRunningCondition == nil { + previousRunningCondition = currentRunningCondition + } + + if previousRunningCondition.Status != currentRunningCondition.Status || previousStatus.LastTimeStatusNotified == nil { + available := currentRunningCondition.IsTrue() + if sfp, err = common.GetDataIndexPlatform(ctx, d.C, workflow); err != nil { + return err + } + if sfp == nil { + klog.V(log.I).Infof("No DataIndex containing platform was found for workflow: %s, namespace: %s, to send the workflow definition status change event.", + workflow.Name, workflow.Namespace) + } else { + evt := workflowdef.NewWorkflowDefinitionAvailabilityEvent(workflow, workflowdef.SonataFlowOperatorSource, properties.GetWorkflowEndpointUrl(workflow), available) + if err = common.SendWorkflowDefinitionEvent(ctx, workflow, sfp, evt); err != nil { Review Comment: This needs to be an async call, right? Otherwise, we will hang our reconciliation routine. ########## packages/sonataflow-operator/internal/controller/profiles/preview/deployment_handler.go: ########## @@ -73,6 +83,10 @@ func (d *DeploymentReconciler) reconcileWithImage(ctx context.Context, workflow if _, err := d.PerformStatusUpdate(ctx, workflow); err != nil { return reconcile.Result{Requeue: false}, nil, err } + + if err := d.notifyStatusUpdate(ctx, workflow, previousStatus); err != nil { + return reconcile.Result{Requeue: false}, nil, err + } Review Comment: The `metadata.generation` doesn't change when we do a status change in the CR? If so, couldn't we use that field instead? ########## packages/sonataflow-operator/internal/controller/sonataflow_controller.go: ########## @@ -146,8 +196,25 @@ func (r *SonataFlowReconciler) cleanupTriggers(ctx context.Context, workflow *op return err } } - controllerutil.RemoveFinalizer(workflow, constants.TriggerFinalizer) - return r.Client.Update(ctx, workflow) + return nil +} + +func (r *SonataFlowReconciler) workflowDeletion(ctx context.Context, workflow *operatorapi.SonataFlow) error { + var err error + var sfp *operatorapi.SonataFlowPlatform + + if sfp, err = common.GetDataIndexPlatform(ctx, r.Client, workflow); err != nil { + return err + } + if sfp == nil { + klog.V(log.I).Infof("No DataIndex containing platform was found for workflow: %s, namespace: %s, for un-deployment notification.", workflow.Name, workflow.Namespace) + } else { + evt := workflowdef.NewWorkflowDefinitionAvailabilityEvent(workflow, workflowdef.SonataFlowOperatorSource, properties.GetWorkflowEndpointUrl(workflow), false) + if err = common.SendWorkflowDefinitionEvent(ctx, workflow, sfp, evt); err != nil { Review Comment: We can use channels to perform async and report the result to the k8s events api. ########## packages/sonataflow-operator/internal/controller/profiles/common/helper.go: ########## @@ -0,0 +1,125 @@ +// 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. + +package common + +import ( + "context" + "fmt" + + v2 "github.com/cloudevents/sdk-go/v2" + "k8s.io/klog/v2" + eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1" + "sigs.k8s.io/controller-runtime/pkg/client" + + operatorapi "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/v1alpha08" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/knative" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/platform" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/platform/services" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/profiles/common/constants" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/log" + "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/utils" +) + +// GetDataIndexPlatform returns the SonataFlowPlatform that declares the DataIndex service currently configured and +// enabled for the given workflow, if any. +func GetDataIndexPlatform(ctx context.Context, cli client.Client, workflow *operatorapi.SonataFlow) (*operatorapi.SonataFlowPlatform, error) { + var sfp *operatorapi.SonataFlowPlatform + var err error + + if sfp, err = platform.GetActivePlatform(ctx, cli, workflow.Namespace, false); err != nil { + return nil, err + } + if sfp == nil { + klog.V(log.I).Infof("No active platform was found for workflow: %s, namespace: %s. ", workflow.Name, workflow.Namespace) + return nil, nil + } + diHandler := services.NewDataIndexHandler(sfp) + if diHandler.IsServiceEnabledInSpec() { + return sfp, nil + } else { + // No DI enabled in current SFP, look for a potential SFCP + klog.V(log.I).Infof("DataIndex is not enabled for workflow: %s, platform: %s, namespace: %s. Looking if a cluster platform exists.", + workflow.Name, sfp.Name, workflow.Namespace) + if sfp, err = knative.GetRemotePlatform(sfp); err != nil { + return nil, err + } + if sfp == nil { + klog.V(log.I).Infof("No cluster platform was found for workflow: %s, namespace: %s", workflow.Name, workflow.Namespace) + return nil, nil + } + diHandler = services.NewDataIndexHandler(sfp) + if diHandler.IsServiceEnabledInSpec() { + return sfp, nil + } + return nil, nil + } +} + +// GetDataIndexBroker returns the broker being used by the DataIndex configured in the given SPF if any. +// Validation is performed to check that the Broker, when configured, is Ready. +func GetDataIndexBroker(sfp *operatorapi.SonataFlowPlatform) (*eventingv1.Broker, error) { + diHandler := services.NewDataIndexHandler(sfp) + brokerDest := diHandler.GetServiceSource() + if brokerDest != nil && len(brokerDest.Ref.Name) > 0 { + brokerNamespace := brokerDest.Ref.Namespace + if len(brokerNamespace) == 0 { + brokerNamespace = sfp.Namespace + } + klog.V(log.I).Infof("Broker: %s, namespace: %s is configured for DataIndex: %s in platform: %s, namespace: %s", + brokerDest.Ref.Name, brokerNamespace, diHandler.GetServiceName(), sfp.Name, sfp.Namespace) + if broker, err := knative.ValidateBroker(brokerDest.Ref.Name, brokerNamespace); err != nil { + return nil, err + } else { + return broker, nil + } + } + return nil, nil +} + +func SendWorkflowDefinitionEvent(ctx context.Context, workflow *operatorapi.SonataFlow, sfp *operatorapi.SonataFlowPlatform, evt *v2.Event) error { Review Comment: nipitck - I would add this to a `workflowdef_event.go` file instead. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
