This is an automated email from the ASF dual-hosted git repository.

wmedvedeo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-tools.git


The following commit(s) were added to refs/heads/main by this push:
     new d3f639fefb6 incubator-kie-tools-3072: [sonataflow-operator] Cleanup 
outdated knative revisions only if the workflow has, or inherit from the SFP, a 
Sink configuration (#3073)
d3f639fefb6 is described below

commit d3f639fefb6eedf15945e5898754234b862bddd5
Author: Walter Medvedeo <[email protected]>
AuthorDate: Thu Apr 17 19:02:59 2025 +0200

    incubator-kie-tools-3072: [sonataflow-operator] Cleanup outdated knative 
revisions only if the workflow has, or inherit from the SFP, a Sink 
configuration (#3073)
---
 .../internal/controller/knative/knative.go         | 10 ++++-
 .../internal/controller/profiles/common/utils.go   | 44 ++++++++++++++++++++++
 .../controller/profiles/gitops/states_gitops.go    |  8 +---
 .../controller/profiles/preview/states_preview.go  |  6 +--
 4 files changed, 55 insertions(+), 13 deletions(-)

diff --git 
a/packages/sonataflow-operator/internal/controller/knative/knative.go 
b/packages/sonataflow-operator/internal/controller/knative/knative.go
index 492fdc8380b..47d0b8fe461 100644
--- a/packages/sonataflow-operator/internal/controller/knative/knative.go
+++ b/packages/sonataflow-operator/internal/controller/knative/knative.go
@@ -365,7 +365,7 @@ func GetSinkURI(destination duckv1.Destination) (*apis.URL, 
error) {
 // service revisions that weren't properly initialized (i.e. doesn't have the 
K_SINK injected) will be cleaned-up.
 // Note that revisions in that situation are not valid, since workflows 
without the K_SINK injected will never pass
 // the health checks, etc.
-func CleanupOutdatedRevisions(ctx context.Context, cfg *rest.Config, workflow 
*operatorapi.SonataFlow) error {
+func CleanupOutdatedRevisions(ctx context.Context, cfg *rest.Config, workflow 
*operatorapi.SonataFlow, platform *operatorapi.SonataFlowPlatform) error {
        if !workflow.IsKnativeDeployment() {
                return nil
        }
@@ -376,6 +376,14 @@ func CleanupOutdatedRevisions(ctx context.Context, cfg 
*rest.Config, workflow *o
        if !avail.Serving || !avail.Eventing {
                return nil
        }
+       sink, err := GetWorkflowSink(workflow, platform)
+       if err != nil {
+               return fmt.Errorf("failed to get workflow sink for workflow : 
%s, namespace: %s : %v", workflow.Name, workflow.Namespace, err)
+       }
+       if sink == nil {
+               // no sink?, then no SinkBinding required/created for this 
workflow, and thus no dangling revisions to clean.
+               return nil
+       }
        injected, err := CheckKSinkInjected(workflow.Name, workflow.Namespace)
        if err != nil {
                return err
diff --git 
a/packages/sonataflow-operator/internal/controller/profiles/common/utils.go 
b/packages/sonataflow-operator/internal/controller/profiles/common/utils.go
new file mode 100644
index 00000000000..c1c62f1ba42
--- /dev/null
+++ b/packages/sonataflow-operator/internal/controller/profiles/common/utils.go
@@ -0,0 +1,44 @@
+// 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"
+
+       "k8s.io/client-go/rest"
+
+       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/utils"
+)
+
+// CleanupOutdatedRevisions helper function to remove the outdated revisions 
for a workflow deployed with the knative
+// deploymentMode.
+// TODO: Refactor when we can remove dependency between properties.go -> 
knative.go, and k8s.go -> knative.go.
+func CleanupOutdatedRevisions(ctx context.Context, cfg *rest.Config, workflow 
*operatorapi.SonataFlow) error {
+       sfp, err := platform.GetActivePlatform(ctx, utils.GetClient(), 
workflow.Namespace, false)
+       if err != nil {
+               return fmt.Errorf("failed to get active platform to clean 
workflow outdated revisions, workflow: %s, namespace: %s : %v", workflow.Name, 
workflow.Namespace, err)
+       }
+       if err := knative.CleanupOutdatedRevisions(ctx, cfg, workflow, sfp); 
err != nil {
+               return fmt.Errorf("failed to cleanup workflow outdated 
revisions, workflow: %s, namespace: %s - %v", workflow.Name, 
workflow.Namespace, err)
+       }
+       return nil
+}
diff --git 
a/packages/sonataflow-operator/internal/controller/profiles/gitops/states_gitops.go
 
b/packages/sonataflow-operator/internal/controller/profiles/gitops/states_gitops.go
index 62e5a820a6e..aa980db72da 100644
--- 
a/packages/sonataflow-operator/internal/controller/profiles/gitops/states_gitops.go
+++ 
b/packages/sonataflow-operator/internal/controller/profiles/gitops/states_gitops.go
@@ -19,9 +19,6 @@ package gitops
 
 import (
        "context"
-       "fmt"
-
-       
"github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/knative"
 
        ctrl "sigs.k8s.io/controller-runtime"
        "sigs.k8s.io/controller-runtime/pkg/client"
@@ -73,8 +70,5 @@ func (f *followDeployWorkflowState) Do(ctx context.Context, 
workflow *operatorap
 
 func (f *followDeployWorkflowState) PostReconcile(ctx context.Context, 
workflow *operatorapi.SonataFlow) error {
        // Clean up the outdated Knative revisions, if any
-       if err := knative.CleanupOutdatedRevisions(ctx, f.Cfg, workflow); err 
!= nil {
-               return fmt.Errorf("failied to cleanup workflow outdated 
revisions, workflow: %s, namespace: %s - %v", workflow.Name, 
workflow.Namespace, err)
-       }
-       return nil
+       return common.CleanupOutdatedRevisions(ctx, f.Cfg, workflow)
 }
diff --git 
a/packages/sonataflow-operator/internal/controller/profiles/preview/states_preview.go
 
b/packages/sonataflow-operator/internal/controller/profiles/preview/states_preview.go
index e53bae1528e..43849d29f81 100644
--- 
a/packages/sonataflow-operator/internal/controller/profiles/preview/states_preview.go
+++ 
b/packages/sonataflow-operator/internal/controller/profiles/preview/states_preview.go
@@ -35,7 +35,6 @@ import (
        "github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api"
        operatorapi 
"github.com/apache/incubator-kie-tools/packages/sonataflow-operator/api/v1alpha08"
        
"github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/builder"
-       
"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/profiles/common"
        
"github.com/apache/incubator-kie-tools/packages/sonataflow-operator/internal/controller/profiles/common/constants"
@@ -229,10 +228,7 @@ func (h *deployWithBuildWorkflowState) Do(ctx 
context.Context, workflow *operato
 
 func (h *deployWithBuildWorkflowState) PostReconcile(ctx context.Context, 
workflow *operatorapi.SonataFlow) error {
        // Clean up the outdated Knative revisions, if any
-       if err := knative.CleanupOutdatedRevisions(ctx, h.Cfg, workflow); err 
!= nil {
-               return fmt.Errorf("failied to cleanup workflow outdated 
revisions, workflow: %s, namespace: %s - %v", workflow.Name, 
workflow.Namespace, err)
-       }
-       return nil
+       return common.CleanupOutdatedRevisions(ctx, h.Cfg, workflow)
 }
 
 // isWorkflowChanged checks whether the contents of .spec.flow of the given 
workflow has changed.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to