Skip to content

Commit

Permalink
Fix #560 - Check if the .spec.flow has changed before building
Browse files Browse the repository at this point in the history
Signed-off-by: Ricardo Zanini <[email protected]>
  • Loading branch information
ricardozanini committed Oct 31, 2024
1 parent ae7dde3 commit c12f3da
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
13 changes: 6 additions & 7 deletions internal/controller/profiles/preview/profile_preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func Test_deployWorkflowReconciliationHandler_handleObjects(t *testing.T) {
assert.Equal(t, serviceMonitor.Spec.Endpoints[0].Path, "/q/metrics")
}

func Test_GenerationAnnotationCheck(t *testing.T) {
func Test_WorkflowChangedCheck(t *testing.T) {
// we load a workflow with metadata.generation to 0
workflow := test.GetBaseSonataFlow(t.Name())
platform := test.GetBasePlatformInReadyPhase(t.Name())
Expand All @@ -199,15 +199,14 @@ func Test_GenerationAnnotationCheck(t *testing.T) {
assert.NotNil(t, result)
assert.Len(t, objects, 3)

// then we load a workflow with metadata.generation set to 1
// then we load the current workflow
workflowChanged := &operatorapi.SonataFlow{}
err = client.Get(context.TODO(), clientruntime.ObjectKeyFromObject(workflow), workflowChanged)
assert.NoError(t, err)
//we set the generation to 1
workflowChanged.Generation = int64(1)
err = client.Update(context.TODO(), workflowChanged)
assert.NoError(t, err)
// reconcile
//we change something within the flow
workflowChanged.Spec.Flow.AutoRetries = true

// reconcile -> the one in the k8s DB is different, so there's a change.
handler = &deployWithBuildWorkflowState{
StateSupport: fakeReconcilerSupport(client),
ensurers: NewObjectEnsurers(&common.StateSupport{C: client}),
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/profiles/preview/states_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package preview
import (
"context"
"fmt"
"reflect"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -36,7 +37,6 @@ import (
"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/log"
kubeutil "github.com/apache/incubator-kie-kogito-serverless-operator/utils/kubernetes"
)

type newBuilderState struct {
Expand Down Expand Up @@ -225,11 +225,11 @@ func (h *deployWithBuildWorkflowState) PostReconcile(ctx context.Context, workfl
return nil
}

// isWorkflowChanged marks the workflow status as unknown to require a new build reconciliation
// isWorkflowChanged checks whether the contents of .spec.flow of the given workflow has changed.
func (h *deployWithBuildWorkflowState) isWorkflowChanged(workflow *operatorapi.SonataFlow) bool {
generation := kubeutil.GetLastGeneration(workflow.Namespace, workflow.Name, h.C, context.TODO())
if generation > workflow.Status.ObservedGeneration {
return true
serverlessWorkflow := &operatorapi.SonataFlow{}
if err := h.C.Get(context.TODO(), client.ObjectKeyFromObject(workflow), serverlessWorkflow); err != nil {
klog.V(log.E).ErrorS(err, "unable to retrieve SonataFlow definition")
}
return false
return !reflect.DeepEqual(&serverlessWorkflow.Spec.Flow, &workflow.Spec.Flow)
}
46 changes: 46 additions & 0 deletions internal/controller/profiles/preview/states_preview_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 preview

import (
"testing"

"github.com/apache/incubator-kie-kogito-serverless-operator/internal/controller/profiles/common"
"github.com/apache/incubator-kie-kogito-serverless-operator/test"
"github.com/serverlessworkflow/sdk-go/v2/model"
"github.com/stretchr/testify/assert"
)

func Test_deployWithBuildWorkflowState_isWorkflowChanged(t *testing.T) {
workflow1 := test.GetBaseSonataFlow(t.Name())
workflow2 := test.GetBaseSonataFlow(t.Name())
deployWithBuildWorkflowState := &deployWithBuildWorkflowState{
StateSupport: &common.StateSupport{C: test.NewSonataFlowClientBuilder().WithRuntimeObjects(workflow1).Build()},
}

assert.False(t, deployWithBuildWorkflowState.isWorkflowChanged(workflow2))

// change workflow2
workflow2.Spec.Flow.Metadata = model.Metadata{
"string": model.Object{
StringValue: "test",
},
}

assert.True(t, deployWithBuildWorkflowState.isWorkflowChanged(workflow2))
}
22 changes: 0 additions & 22 deletions utils/kubernetes/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,11 @@
package kubernetes

import (
"context"
"strconv"

"k8s.io/klog/v2"

"sigs.k8s.io/controller-runtime/pkg/client"

operatorapi "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08"
"github.com/apache/incubator-kie-kogito-serverless-operator/log"
)

func getWorkflow(namespace string, name string, c client.Client, ctx context.Context) *operatorapi.SonataFlow {
serverlessWorkflowType := &operatorapi.SonataFlow{}
serverlessWorkflowType.Namespace = namespace
serverlessWorkflowType.Name = name
serverlessWorkflow := &operatorapi.SonataFlow{}
if err := c.Get(ctx, client.ObjectKeyFromObject(serverlessWorkflowType), serverlessWorkflow); err != nil {
klog.V(log.E).ErrorS(err, "unable to retrieve SonataFlow definition")
}
return serverlessWorkflow
}

func GetLastGeneration(namespace string, name string, c client.Client, ctx context.Context) int64 {
workflow := getWorkflow(namespace, name, c, ctx)
return workflow.Generation
}

// GetAnnotationAsBool returns the boolean value from the given annotation.
// If the annotation is not present or is there an error in the ParseBool conversion, returns false.
func GetAnnotationAsBool(object client.Object, key string) bool {
Expand Down

0 comments on commit c12f3da

Please sign in to comment.