Skip to content

Commit

Permalink
bugfix(#5758): incremental image build should not considering the app…
Browse files Browse the repository at this point in the history
…lication jar when matching dependencies
  • Loading branch information
lsergio committed Aug 17, 2024
1 parent db8c0fa commit 7ef2d96
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pkg/builder/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -238,8 +239,17 @@ func findBestImage(images []v1.IntegrationKitStatus, artifacts []v1.Artifact) (v
bestImageCommonLibs := make(map[string]bool)

for _, image := range images {
nonLibArtifacts := 0
common := make(map[string]bool)

for _, artifact := range image.Artifacts {
// 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.
Expand All @@ -252,7 +262,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
Expand Down
87 changes: 87 additions & 0 deletions pkg/builder/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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",
},
},
},
Expand All @@ -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",
},
},
},
Expand All @@ -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",
},
},
},
Expand All @@ -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",
},
},
}}
Expand All @@ -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{
Expand All @@ -213,6 +231,7 @@ func TestFindBestImageNoExactMatch(t *testing.T) {
{
Checksum: "1",
ID: "artifact-1",
Target: "dependencies/lib/main/artifact-1.jar",
},
},
},
Expand All @@ -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",
},
},
},
Expand All @@ -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",
},
},
},
Expand All @@ -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",
},
},
}}
Expand All @@ -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{
Expand All @@ -301,6 +332,7 @@ func TestFindBestImageNoExactMatchBadChecksum(t *testing.T) {
{
Checksum: "1",
ID: "artifact-1",
Target: "dependencies/lib/main/artifact-1.jar",
},
},
},
Expand All @@ -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",
},
},
},
Expand All @@ -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",
},
},
},
Expand All @@ -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",
},
},
}}
Expand Down Expand Up @@ -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"])
}

0 comments on commit 7ef2d96

Please sign in to comment.