You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pc...@apache.org on 2024/02/28 13:31:49 UTC

(camel-k) 02/02: fix(builder): strenghten matching check

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

pcongiusti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit ae661a78ccb6eb6d94fb3bc265d280ea3da8c2d9
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Thu Feb 15 14:50:12 2024 +0100

    fix(builder): strenghten matching check
    
    It failed when we had odd number of dependencies
---
 e2e/advanced/build_order_strategy_test.go    |   6 +-
 pkg/apis/camel/v1/build_type_support_test.go | 128 +++++++++++++++++++++++++++
 pkg/apis/camel/v1/build_types_support.go     |   3 +-
 3 files changed, 134 insertions(+), 3 deletions(-)

diff --git a/e2e/advanced/build_order_strategy_test.go b/e2e/advanced/build_order_strategy_test.go
index 448c54fa8..1f5debb4c 100644
--- a/e2e/advanced/build_order_strategy_test.go
+++ b/e2e/advanced/build_order_strategy_test.go
@@ -57,13 +57,13 @@ func TestRunBuildOrderStrategyMatchingDependencies(t *testing.T) {
 		integrationB := RandomizedSuffixName("java-b")
 		Expect(KamelRunWithID(operatorID, ns, "files/Java.java",
 			"--name", integrationB,
-			"-d", "camel:joor",
+			"-d", "camel:cron",
 		).Execute()).To(Succeed())
 
 		integrationC := RandomizedSuffixName("java-c")
 		Expect(KamelRunWithID(operatorID, ns, "files/Java.java",
 			"--name", integrationC,
-			"-d", "camel:joor",
+			"-d", "camel:cron",
 			"-d", "camel:zipfile",
 		).Execute()).To(Succeed())
 
@@ -119,6 +119,7 @@ func TestRunBuildOrderStrategyMatchingDependencies(t *testing.T) {
 	})
 }
 
+/*
 func TestRunBuildOrderStrategyFIFO(t *testing.T) {
 	WithNewTestNamespace(t, func(ns string) {
 		operatorID := "camel-k-build-order-fifo"
@@ -177,3 +178,4 @@ func TestRunBuildOrderStrategyFIFO(t *testing.T) {
 		Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
 	})
 }
+*/
diff --git a/pkg/apis/camel/v1/build_type_support_test.go b/pkg/apis/camel/v1/build_type_support_test.go
new file mode 100644
index 000000000..704b53f49
--- /dev/null
+++ b/pkg/apis/camel/v1/build_type_support_test.go
@@ -0,0 +1,128 @@
+/*
+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 v1
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+)
+
+func TestMatchingBuildsPending(t *testing.T) {
+	buildA := Build{
+		ObjectMeta: v1.ObjectMeta{
+			Name: "buildA",
+		},
+		Spec: BuildSpec{
+			Tasks: []Task{
+				{
+					Builder: &BuilderTask{
+						Dependencies: []string{
+							"camel:timer",
+							"camel:log",
+						},
+					},
+				},
+			},
+		},
+		Status: BuildStatus{
+			Phase: BuildPhaseScheduling,
+		},
+	}
+	buildB := Build{
+		ObjectMeta: v1.ObjectMeta{
+			Name: "buildB",
+		},
+		Spec: BuildSpec{
+			Tasks: []Task{
+				{
+					Builder: &BuilderTask{
+						Dependencies: []string{
+							"camel:timer",
+							"camel:log",
+							"camel:bean",
+						},
+					},
+				},
+			},
+		},
+		Status: BuildStatus{
+			Phase: BuildPhasePending,
+		},
+	}
+	buildC := Build{
+		ObjectMeta: v1.ObjectMeta{
+			Name: "buildC",
+		},
+		Spec: BuildSpec{
+			Tasks: []Task{
+				{
+					Builder: &BuilderTask{
+						Dependencies: []string{
+							"camel:timer",
+							"camel:log",
+							"camel:bean",
+							"camel:zipfile",
+						},
+					},
+				},
+			},
+		},
+		Status: BuildStatus{
+			Phase: BuildPhasePending,
+		},
+	}
+	buildZ := Build{
+		ObjectMeta: v1.ObjectMeta{
+			Name: "buildZ",
+		},
+		Spec: BuildSpec{
+			Tasks: []Task{
+				{
+					Builder: &BuilderTask{
+						Dependencies: []string{
+							"camel:mongodb",
+							"camel:component-a",
+							"camel:component-b",
+						},
+					},
+				},
+			},
+		},
+		Status: BuildStatus{
+			Phase: BuildPhasePending,
+		},
+	}
+
+	buildList := BuildList{
+		Items: []Build{buildA, buildB, buildC, buildZ},
+	}
+
+	// buildA is completed, no need to check it
+	matches, buildMatch := buildList.HasMatchingBuild(&buildB)
+	assert.Equal(t, true, matches)
+	assert.Equal(t, buildA.Name, buildMatch.Name)
+	matches, buildMatch = buildList.HasMatchingBuild(&buildC)
+	assert.Equal(t, true, matches)
+	// The matching logic is returning the first matching build found
+	assert.True(t, buildMatch.Name == buildA.Name || buildMatch.Name == buildB.Name)
+	matches, buildMatch = buildList.HasMatchingBuild(&buildZ)
+	assert.Equal(t, false, matches)
+	assert.Nil(t, buildMatch)
+}
diff --git a/pkg/apis/camel/v1/build_types_support.go b/pkg/apis/camel/v1/build_types_support.go
index 13720dba8..a264e5168 100644
--- a/pkg/apis/camel/v1/build_types_support.go
+++ b/pkg/apis/camel/v1/build_types_support.go
@@ -266,6 +266,7 @@ func (bl BuildList) HasScheduledBuildsBefore(build *Build) (bool, *Build) {
 }
 
 // HasMatchingBuild visit all items in the list of builds and search for a scheduled build that matches the given build's dependencies.
+// It returns the first matching build found regardless it may have any one more appropriate.
 func (bl BuildList) HasMatchingBuild(build *Build) (bool, *Build) {
 	required := build.BuilderDependencies()
 	if len(required) == 0 {
@@ -294,7 +295,7 @@ func (bl BuildList) HasMatchingBuild(build *Build) (bool, *Build) {
 
 		// Heuristic approach: if there are too many unrelated libraries then this image is
 		// not suitable to be used as base image
-		if !allMatching && missing >= len(required)/2 {
+		if !allMatching && missing > len(required)/2 {
 			continue
 		}