From f72f1cb985633ca37941370478ab8ddf4b7631ce Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 11:00:21 +0100 Subject: [PATCH 01/13] Add an integration test to reproduce #2892 --- integration/integration_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/integration/integration_test.go b/integration/integration_test.go index ab59771652..55d2c5f4c7 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -636,6 +636,20 @@ func TestCache(t *testing.T) { } } +// Attempt to warm an image two times : first time should populate the cache, second time should find the image in the cache. +func TestWarmerTwice(t *testing.T) { + // Start a sleeping warmer container + dockerRunFlags := []string{"run", "--net=host"} + dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = append(dockerRunFlags, WarmerImage, + "--entrypoint=/bin/sh", + "--memory=16m", "--memory-swappiness=0", + "-c", + "/kaniko/warmer -i debian:trixie-slim ; /kaniko/warmer -i debian:trixie-slim") + warmTwiceCmd := exec.Command("docker", dockerRunFlags...) + _ := RunCommand(warmTwiceCmd, t) +} + func verifyBuildWith(t *testing.T, cache, dockerfile string) { args := []string{} if strings.HasPrefix(dockerfile, "Dockerfile_test_cache_copy") { From 1f41e286fd8185fe33a9bbf0ff83cae862239e92 Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 13:56:04 +0100 Subject: [PATCH 02/13] Fix go compilation --- integration/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index 55d2c5f4c7..ff686735b7 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -647,7 +647,7 @@ func TestWarmerTwice(t *testing.T) { "-c", "/kaniko/warmer -i debian:trixie-slim ; /kaniko/warmer -i debian:trixie-slim") warmTwiceCmd := exec.Command("docker", dockerRunFlags...) - _ := RunCommand(warmTwiceCmd, t) + _ = RunCommand(warmTwiceCmd, t) } func verifyBuildWith(t *testing.T, cache, dockerfile string) { From a4ce1ad4bee9f747b82b96f6fc7be7b0e2b662b9 Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 14:14:08 +0100 Subject: [PATCH 03/13] Fix docker run cmd --- integration/integration_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index ff686735b7..bcd6f50568 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -641,9 +641,10 @@ func TestWarmerTwice(t *testing.T) { // Start a sleeping warmer container dockerRunFlags := []string{"run", "--net=host"} dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) - dockerRunFlags = append(dockerRunFlags, WarmerImage, + dockerRunFlags = append(dockerRunFlags, "--entrypoint=/bin/sh", "--memory=16m", "--memory-swappiness=0", + WarmerImage, "-c", "/kaniko/warmer -i debian:trixie-slim ; /kaniko/warmer -i debian:trixie-slim") warmTwiceCmd := exec.Command("docker", dockerRunFlags...) From 93eff388cbce21e01be325c9888dcc58119f098e Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 15:09:38 +0100 Subject: [PATCH 04/13] Fixing entrypoint --- integration/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index bcd6f50568..e08b461894 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -642,7 +642,7 @@ func TestWarmerTwice(t *testing.T) { dockerRunFlags := []string{"run", "--net=host"} dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) dockerRunFlags = append(dockerRunFlags, - "--entrypoint=/bin/sh", + "--entrypoint=/busybox/sh", "--memory=16m", "--memory-swappiness=0", WarmerImage, "-c", From ee9720a5bd0336c9341d8b843a9a42b5c0debdc4 Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 16:11:37 +0100 Subject: [PATCH 05/13] Test warmer with cache in a volume. --- integration/integration_test.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index e08b461894..d389cf29ec 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -638,17 +638,22 @@ func TestCache(t *testing.T) { // Attempt to warm an image two times : first time should populate the cache, second time should find the image in the cache. func TestWarmerTwice(t *testing.T) { + _, ex, _, _ := runtime.Caller(0) + cwd := filepath.Dir(ex + "/cache") + // Start a sleeping warmer container dockerRunFlags := []string{"run", "--net=host"} dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) dockerRunFlags = append(dockerRunFlags, - "--entrypoint=/busybox/sh", - "--memory=16m", "--memory-swappiness=0", + "--memory=16m", + "-v", cwd + ":/cache" WarmerImage, - "-c", - "/kaniko/warmer -i debian:trixie-slim ; /kaniko/warmer -i debian:trixie-slim") - warmTwiceCmd := exec.Command("docker", dockerRunFlags...) - _ = RunCommand(warmTwiceCmd, t) + "--cache-dir=/cache", + "-i", "debian:trixie-slim") + warmCmd := exec.Command("docker", dockerRunFlags...) + + _ = RunCommand(warmCmd, t) + _ = RunCommand(warmCmd, t) } func verifyBuildWith(t *testing.T, cache, dockerfile string) { From 71a384d0f3e0488c8f89bfe8db58744258870342 Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 16:16:53 +0100 Subject: [PATCH 06/13] Add missing comma --- integration/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index d389cf29ec..adf0796216 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -646,7 +646,7 @@ func TestWarmerTwice(t *testing.T) { dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) dockerRunFlags = append(dockerRunFlags, "--memory=16m", - "-v", cwd + ":/cache" + "-v", cwd + ":/cache", WarmerImage, "--cache-dir=/cache", "-i", "debian:trixie-slim") From f5d86b1981d3266458ca64575ba8e9dfec0e6dd2 Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 16:21:12 +0100 Subject: [PATCH 07/13] Fix imports --- integration/integration_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/integration/integration_test.go b/integration/integration_test.go index adf0796216..77109281d9 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -28,6 +28,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strconv" "strings" "testing" From f86bff67646e83f1daa604b8056f07637eab627e Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 16:35:40 +0100 Subject: [PATCH 08/13] Fix dir --- integration/integration_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index 77109281d9..4dd438b06f 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -640,8 +640,8 @@ func TestCache(t *testing.T) { // Attempt to warm an image two times : first time should populate the cache, second time should find the image in the cache. func TestWarmerTwice(t *testing.T) { _, ex, _, _ := runtime.Caller(0) - cwd := filepath.Dir(ex + "/cache") - + cwd := filepath.Dir(ex) + "/tmpCache" + // Start a sleeping warmer container dockerRunFlags := []string{"run", "--net=host"} dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) @@ -652,7 +652,7 @@ func TestWarmerTwice(t *testing.T) { "--cache-dir=/cache", "-i", "debian:trixie-slim") warmCmd := exec.Command("docker", dockerRunFlags...) - + _ = RunCommand(warmCmd, t) _ = RunCommand(warmCmd, t) } From 9e2351b6403bcf12d998219a59b1c15691efd9df Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 17:00:19 +0100 Subject: [PATCH 09/13] Add logs --- integration/integration_test.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index 4dd438b06f..670311dfe9 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -653,8 +653,17 @@ func TestWarmerTwice(t *testing.T) { "-i", "debian:trixie-slim") warmCmd := exec.Command("docker", dockerRunFlags...) - _ = RunCommand(warmCmd, t) - _ = RunCommand(warmCmd, t) + out, err := RunCommandWithoutTest(warmCmd, t) + if err != nil { + log.Fatal("Unable to perform first warming: ", err) + } + log.Printf("First warm output: %s", out) + + out, err = RunCommandWithoutTest(warmCmd, t) + if err != nil { + log.Fatal("Unable to perform second warming: ", err) + } + log.Printf("Second warm output: %s", out) } func verifyBuildWith(t *testing.T, cache, dockerfile string) { From dc8f84884bff2383f86bb196ef0e6cf5c284e5bc Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 17:04:17 +0100 Subject: [PATCH 10/13] fix --- integration/integration_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index 670311dfe9..c493dbd205 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -653,13 +653,13 @@ func TestWarmerTwice(t *testing.T) { "-i", "debian:trixie-slim") warmCmd := exec.Command("docker", dockerRunFlags...) - out, err := RunCommandWithoutTest(warmCmd, t) + out, err := RunCommandWithoutTest(warmCmd) if err != nil { log.Fatal("Unable to perform first warming: ", err) } log.Printf("First warm output: %s", out) - out, err = RunCommandWithoutTest(warmCmd, t) + out, err = RunCommandWithoutTest(warmCmd) if err != nil { log.Fatal("Unable to perform second warming: ", err) } From 438e41b3a661a61c9eeb91e4297e0b123d59f87a Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 17:15:32 +0100 Subject: [PATCH 11/13] Use test framework to log --- integration/integration_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index c493dbd205..157df981b0 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -651,19 +651,20 @@ func TestWarmerTwice(t *testing.T) { WarmerImage, "--cache-dir=/cache", "-i", "debian:trixie-slim") + warmCmd := exec.Command("docker", dockerRunFlags...) - out, err := RunCommandWithoutTest(warmCmd) if err != nil { - log.Fatal("Unable to perform first warming: ", err) + t.Fatalf("Unable to perform first warming: %s", err) } - log.Printf("First warm output: %s", out) + t.Logf("First warm output: %s", out) + warmCmd = exec.Command("docker", dockerRunFlags...) out, err = RunCommandWithoutTest(warmCmd) if err != nil { - log.Fatal("Unable to perform second warming: ", err) + t.Fatalf("Unable to perform second warming: %s", err) } - log.Printf("Second warm output: %s", out) + t.Logf("Second warm output: %s", out) } func verifyBuildWith(t *testing.T, cache, dockerfile string) { From 2f5dd6ab0d7fbc04d85af190adb04bdc84882c82 Mon Sep 17 00:00:00 2001 From: Maxime BOSSARD Date: Tue, 5 Dec 2023 21:47:45 +0100 Subject: [PATCH 12/13] Fix warmer failing if image already in cache. --- pkg/cache/warm.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/cache/warm.go b/pkg/cache/warm.go index 7b5e307e6a..589edcafa7 100644 --- a/pkg/cache/warm.go +++ b/pkg/cache/warm.go @@ -97,10 +97,11 @@ func warmToFile(cacheDir, img string, opts *config.WarmerOptions) error { digest, err := cw.Warm(img, opts) if err != nil { - if !IsAlreadyCached(err) { - logrus.Warnf("Error while trying to warm image: %v %v", img, err) + if IsAlreadyCached(err) { + logrus.Infof("Image already in cache: %v", img) + return nil } - + logrus.Warnf("Error while trying to warm image: %v %v", img, err) return err } From e15176c09d8041e635d364c1e42ec1c8ec9be857 Mon Sep 17 00:00:00 2001 From: mxBossard Date: Tue, 5 Dec 2023 22:11:21 +0100 Subject: [PATCH 13/13] Fix format. --- integration/integration_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index 157df981b0..653a1f91f0 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -647,11 +647,11 @@ func TestWarmerTwice(t *testing.T) { dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) dockerRunFlags = append(dockerRunFlags, "--memory=16m", - "-v", cwd + ":/cache", + "-v", cwd+":/cache", WarmerImage, "--cache-dir=/cache", "-i", "debian:trixie-slim") - + warmCmd := exec.Command("docker", dockerRunFlags...) out, err := RunCommandWithoutTest(warmCmd) if err != nil {