From 4aeb393d0df236525ae794ac8d55a9d97c5dd0f6 Mon Sep 17 00:00:00 2001 From: Andreas Bergmeier <51448674+AndreasBergmeier6176@users.noreply.github.com> Date: Wed, 22 Jan 2025 15:02:00 +0100 Subject: [PATCH] fix: Add Dockerfile for digest calculation (#9666) feat: Add Dockerfile to digest calculation Previously changes in Dockerfile did not lead to a new artifact being built. --- pkg/skaffold/tag/input_digest.go | 12 +++++ pkg/skaffold/tag/input_digest_test.go | 72 +++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/pkg/skaffold/tag/input_digest.go b/pkg/skaffold/tag/input_digest.go index 36b4674cc80..a0d7d7acf02 100644 --- a/pkg/skaffold/tag/input_digest.go +++ b/pkg/skaffold/tag/input_digest.go @@ -57,6 +57,18 @@ func (t *inputDigestTagger) GenerateTag(ctx context.Context, image latest.Artifa return "", err } + if image.DockerArtifact != nil { + srcFiles = append(srcFiles, image.DockerArtifact.DockerfilePath) + } + + if image.KanikoArtifact != nil { + srcFiles = append(srcFiles, image.KanikoArtifact.DockerfilePath) + } + + if image.CustomArtifact != nil && image.CustomArtifact.Dependencies != nil && image.CustomArtifact.Dependencies.Dockerfile != nil { + srcFiles = append(srcFiles, image.CustomArtifact.Dependencies.Dockerfile.Path) + } + // must sort as hashing is sensitive to the order in which files are processed sort.Strings(srcFiles) for _, d := range srcFiles { diff --git a/pkg/skaffold/tag/input_digest_test.go b/pkg/skaffold/tag/input_digest_test.go index 64762975ebf..dc76985ce38 100644 --- a/pkg/skaffold/tag/input_digest_test.go +++ b/pkg/skaffold/tag/input_digest_test.go @@ -17,10 +17,17 @@ limitations under the License. package tag import ( + "context" + "io" "os" "path/filepath" "testing" + "github.com/google/go-cmp/cmp" + + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/graph" + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/runner/runcontext" + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest" "github.com/GoogleContainerTools/skaffold/v2/testutil" ) @@ -81,3 +88,68 @@ func TestInputDigest(t *testing.T) { t.CheckFalse(hash1 == hash2) }) } +func TestGenerateTag(t *testing.T) { + testutil.Run(t, "CompareTagWithAndWithoutDockerfile", func(t *testutil.T) { + runCtx := &runcontext.RunContext{} + dockerfile1Path := filepath.Join(t.TempDir(), "Dockerfile1") + dockerfile2Path := filepath.Join(t.TempDir(), "Dockerfile2") + + f, err := os.Create(dockerfile1Path) + if err != nil { + panic(err) + } + defer f.Close() + + _, err = io.WriteString(f, ` +FROM busybox + +CMD [ "ps", "faux" ] +`) + if err != nil { + panic(err) + } + + f, err = os.Create(dockerfile2Path) + if err != nil { + panic(err) + } + defer f.Close() + + _, err = io.WriteString(f, ` +FROM busybox + +CMD [ "true" ] +`) + if err != nil { + panic(err) + } + + digestExample, _ := NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts())) + tag1, err := digestExample.GenerateTag(context.Background(), latest.Artifact{ + ArtifactType: latest.ArtifactType{ + DockerArtifact: &latest.DockerArtifact{ + DockerfilePath: dockerfile1Path, + }, + }, + }) + if err != nil { + t.Fatalf("Generate first tag failed: %v", err) + } + + digestExample, _ = NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts())) + tag2, err := digestExample.GenerateTag(context.Background(), latest.Artifact{ + ArtifactType: latest.ArtifactType{ + DockerArtifact: &latest.DockerArtifact{ + DockerfilePath: dockerfile2Path, + }, + }, + }) + if err != nil { + t.Fatalf("Generate second tag failed: %v", err) + } + + if diff := cmp.Diff(tag1, tag2); diff == "" { + t.Error("Tag does not differ between first and second Dockerfile") + } + }) +}