diff --git a/pkg/builder/image.go b/pkg/builder/image.go index 8523262187..617795e393 100644 --- a/pkg/builder/image.go +++ b/pkg/builder/image.go @@ -21,6 +21,7 @@ import ( "os" "path" "path/filepath" + "strings" "github.com/apache/camel-k/v2/pkg/util/io" "github.com/apache/camel-k/v2/pkg/util/log" @@ -230,16 +231,30 @@ func findBestImage(images []v1.IntegrationKitStatus, artifacts []v1.Artifact) (v return bestImage, nil } + log.Infof("Required Artifacts") requiredLibs := make(map[string]string, len(artifacts)) for _, entry := range artifacts { + log.Infof("%s - %s", entry.ID, entry.Checksum) requiredLibs[entry.ID] = entry.Checksum } bestImageCommonLibs := make(map[string]bool) for _, image := range images { + nonLibArtifacts := 0 common := make(map[string]bool) + log.Infof("Image Artifacts") + for _, artifact := range image.Artifacts { + log.Infof("%s - %s", artifact.ID, artifact.Checksum) + + // the application artifacts should not be considered as dependencies for image reuse + // otherwise, checksums would never match and we would always use the root image + if !strings.HasPrefix(artifact.Target, "dependencies/lib") { + nonLibArtifacts++ + continue + } + // If the Artifact's checksum is not defined we can't reliably determine if for some // reason the artifact has been changed but not the ID (as example for snapshots or // other generated jar) thus we do not take this artifact into account. @@ -252,7 +267,7 @@ func findBestImage(images []v1.IntegrationKitStatus, artifacts []v1.Artifact) (v } numCommonLibs := len(common) - surplus := len(image.Artifacts) - numCommonLibs + surplus := len(image.Artifacts) - numCommonLibs - nonLibArtifacts if surplus > 0 { // the base image cannot have extra libs that we don't need diff --git a/pkg/builder/image_test.go b/pkg/builder/image_test.go index 7fcfa7eb90..0014c48dc6 100644 --- a/pkg/builder/image_test.go +++ b/pkg/builder/image_test.go @@ -99,14 +99,17 @@ func TestFindBestImageExactMatch(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "2", ID: "artifact-2", + Target: "dependencies/lib/main/artifact-2.jar", }, { Checksum: "3", ID: "artifact-3", + Target: "dependencies/lib/main/artifact-3.jar", }, } iks := []v1.IntegrationKitStatus{ @@ -116,10 +119,12 @@ func TestFindBestImageExactMatch(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "3", ID: "artifact-3", + Target: "dependencies/lib/main/artifact-3.jar", }, }, }, @@ -129,14 +134,17 @@ func TestFindBestImageExactMatch(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "2", ID: "artifact-2", + Target: "dependencies/lib/main/artifact-2.jar", }, { Checksum: "3", ID: "artifact-3", + Target: "dependencies/lib/main/artifact-3.jar", }, }, }, @@ -146,18 +154,22 @@ func TestFindBestImageExactMatch(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "2", ID: "artifact-2", + Target: "dependencies/lib/main/artifact-2.jar", }, { Checksum: "3", ID: "artifact-3", + Target: "dependencies/lib/main/artifact-3.jar", }, { Checksum: "4", ID: "artifact-4", + Target: "dependencies/lib/main/artifact-4.jar", }, }, }, @@ -167,14 +179,17 @@ func TestFindBestImageExactMatch(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "2", ID: "artifact-2", + Target: "dependencies/lib/main/artifact-2.jar", }, { Checksum: "4", ID: "artifact-4", + Target: "dependencies/lib/main/artifact-4.jar", }, }, }} @@ -196,14 +211,17 @@ func TestFindBestImageNoExactMatch(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "2", ID: "artifact-2", + Target: "dependencies/lib/main/artifact-2.jar", }, { Checksum: "3", ID: "artifact-3", + Target: "dependencies/lib/main/artifact-3.jar", }, } iks := []v1.IntegrationKitStatus{ @@ -213,6 +231,7 @@ func TestFindBestImageNoExactMatch(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, }, }, @@ -222,10 +241,12 @@ func TestFindBestImageNoExactMatch(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "3", ID: "artifact-3", + Target: "dependencies/lib/main/artifact-3.jar", }, }, }, @@ -235,18 +256,22 @@ func TestFindBestImageNoExactMatch(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "2", ID: "artifact-2", + Target: "dependencies/lib/main/artifact-2.jar", }, { Checksum: "3", ID: "artifact-3", + Target: "dependencies/lib/main/artifact-3.jar", }, { Checksum: "4", ID: "artifact-4", + Target: "dependencies/lib/main/artifact-4.jar", }, }, }, @@ -256,14 +281,17 @@ func TestFindBestImageNoExactMatch(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "2", ID: "artifact-2", + Target: "dependencies/lib/main/artifact-2.jar", }, { Checksum: "4", ID: "artifact-4", + Target: "dependencies/lib/main/artifact-4.jar", }, }, }} @@ -284,14 +312,17 @@ func TestFindBestImageNoExactMatchBadChecksum(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "2", ID: "artifact-2", + Target: "dependencies/lib/main/artifact-2.jar", }, { Checksum: "3", ID: "artifact-3", + Target: "dependencies/lib/main/artifact-3.jar", }, } iks := []v1.IntegrationKitStatus{ @@ -301,6 +332,7 @@ func TestFindBestImageNoExactMatchBadChecksum(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, }, }, @@ -310,10 +342,12 @@ func TestFindBestImageNoExactMatchBadChecksum(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "bad-checksum", ID: "artifact-3", + Target: "dependencies/lib/main/artifact-3.jar", }, }, }, @@ -323,18 +357,22 @@ func TestFindBestImageNoExactMatchBadChecksum(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "2", ID: "artifact-2", + Target: "dependencies/lib/main/artifact-2.jar", }, { Checksum: "3", ID: "artifact-3", + Target: "dependencies/lib/main/artifact-3.jar", }, { Checksum: "4", ID: "artifact-4", + Target: "dependencies/lib/main/artifact-4.jar", }, }, }, @@ -344,14 +382,17 @@ func TestFindBestImageNoExactMatchBadChecksum(t *testing.T) { { Checksum: "1", ID: "artifact-1", + Target: "dependencies/lib/main/artifact-1.jar", }, { Checksum: "2", ID: "artifact-2", + Target: "dependencies/lib/main/artifact-2.jar", }, { Checksum: "4", ID: "artifact-4", + Target: "dependencies/lib/main/artifact-4.jar", }, }, }} @@ -455,3 +496,49 @@ func TestFindBestImageAllImagesWithSurplus(t *testing.T) { assert.Equal(t, "", bestImage.Image) assert.Empty(t, commonLibs) } + +func TestFindBestImageShouldIgnoreIntegrationArfifact(t *testing.T) { + requiredArtifacts := []v1.Artifact{ + { + Checksum: "sha1:eMo2eeyP9wfBixeIXvWJRneAxIY=", + ID: "io.github.crac.org-crac-0.1.3.jar", + Target: "dependencies/lib/boot/io.github.crac.org-crac-0.1.3.jar", + }, + { + Checksum: "sha1:bOYm5KjEIJKipCZTSMpiGReLm80=", + ID: "io.quarkus.quarkus-development-mode-spi-3.8.3.jar", + Target: "dependencies/lib/boot/io.quarkus.quarkus-development-mode-spi-3.8.3.jar", + }, + } + iks := []v1.IntegrationKitStatus{ + { + // the integration jar file should not be taken into account, so this should be a good match + Artifacts: []v1.Artifact{ + { + Checksum: "sha1:Nca4e5f6WohYvjBCigLINWbigAo=", + ID: "camel-k-integration-2.5.0-SNAPSHOT.jar", + Target: "dependencies/app/camel-k-integration-2.5.0-SNAPSHOT.jar", + }, + { + Checksum: "sha1:eMo2eeyP9wfBixeIXvWJRneAxIY=", + ID: "io.github.crac.org-crac-0.1.3.jar", + Target: "dependencies/lib/boot/io.github.crac.org-crac-0.1.3.jar", + }, + { + Checksum: "sha1:bOYm5KjEIJKipCZTSMpiGReLm80=", + ID: "io.quarkus.quarkus-development-mode-spi-3.8.3.jar", + Target: "dependencies/lib/boot/io.quarkus.quarkus-development-mode-spi-3.8.3.jar", + }, + }, + }, + } + + bestImage, commonLibs := findBestImage(iks, requiredArtifacts) + + assert.NotNil(t, bestImage) + assert.Equal(t, iks[0], bestImage) + assert.NotNil(t, commonLibs) + assert.Equal(t, 2, len(commonLibs)) + assert.True(t, commonLibs["io.github.crac.org-crac-0.1.3.jar"]) + assert.True(t, commonLibs["io.quarkus.quarkus-development-mode-spi-3.8.3.jar"]) +}