Skip to content

Conversation

reingart
Copy link

@reingart reingart commented Sep 9, 2025

Fixes: #9826

Description

Honor target in input digest tagger: add the target to the input list so it affects the final hash.

Unit test were included (different targets and no target)

User facing changes

Tags will change if using input digest and target (if empty, same tag will be generated)

@reingart reingart requested a review from a team as a code owner September 9, 2025 23:35
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @reingart, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an oversight in the input digest tagger where the build target was not factored into the generated image tag. By including the target in the hashing process, it ensures that unique images produced from the same Dockerfile but with different build targets will now correctly receive distinct tags, improving tag accuracy and reliability.

Highlights

  • Input Digest Tagger Logic: The inputDigestTagger now correctly incorporates the target field from DockerArtifact and KanikoArtifact into the input list used for hash generation. This ensures that images built with different targets will produce distinct digest tags.
  • Test Coverage: New unit tests (TestGenerateTag_DifferentTarget and TestGenerateTag_NoTarget) have been added to validate the updated tag generation logic for scenarios involving different targets and the absence of a target.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly fixes an issue where the target for Docker and Kaniko artifacts was not being included in the input digest calculation for image tagging. The change ensures that different build targets produce different tags, as expected. The included unit tests validate this new behavior. I have provided a few suggestions to improve code maintainability by reducing duplication in the implementation, and to enhance the correctness and clarity of the new tests.

Comment on lines +157 to +195
func TestGenerateTag_DifferentTarget(t *testing.T) {
runCtx := &runcontext.RunContext{}
dockerfilePath := filepath.Join(t.TempDir(), "Dockerfile")
if err := os.WriteFile(dockerfilePath, []byte("FROM busybox\nCMD [\"ps\", \"faux\"]\n"), 0644); err != nil {
t.Fatalf("failed to write dockerfile: %v", err)
}

digestExample, _ := NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts()))
tag1, err := digestExample.GenerateTag(context.Background(), latest.Artifact{
Workspace: t.TempDir(),
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: dockerfilePath,
Target: "target1",
},
},
})
if err != nil {
t.Fatalf("GenerateTag failed for target1: %v", err)
}

digestExample, _ = NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts()))
tag2, err := digestExample.GenerateTag(context.Background(), latest.Artifact{
Workspace: t.TempDir(),
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: dockerfilePath,
Target: "target2",
},
},
})
if err != nil {
t.Fatalf("GenerateTag failed for target2: %v", err)
}

if tag1 == tag2 {
t.Errorf("expected different tags for different targets, got same: %s", tag1)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This test has a couple of issues in its setup:

  1. The inputDigestTagger is instantiated twice unnecessarily. The tagger instance can be reused for generating both tags since the operation is stateless for these calls.
  2. The workspace directory is not handled correctly. t.TempDir() is called multiple times, creating a new directory each time. The Dockerfile is in one temporary directory, while the artifacts are configured with different temporary directories as their workspace. This causes filepath.Rel to fail in fileHasher, which then falls back to using absolute paths. For better test hermeticity, you should create a single temporary directory at the start of the test and use it for both the Dockerfile and the artifact workspaces.
func TestGenerateTag_DifferentTarget(t *testing.T) {
	runCtx := &runcontext.RunContext{}
	dir := t.TempDir()
	dockerfilePath := filepath.Join(dir, "Dockerfile")
	if err := os.WriteFile(dockerfilePath, []byte("FROM busybox\nCMD [\"ps\", \"faux\"]\n"), 0644); err != nil {
		t.Fatalf("failed to write dockerfile: %v", err)
	}

	digestExample, _ := NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts()))
	tag1, err := digestExample.GenerateTag(context.Background(), latest.Artifact{
		Workspace: dir,
		ArtifactType: latest.ArtifactType{
			DockerArtifact: &latest.DockerArtifact{
				DockerfilePath: dockerfilePath,
				Target:         "target1",
			},
		},
	})
	if err != nil {
		t.Fatalf("GenerateTag failed for target1: %v", err)
	}

	tag2, err := digestExample.GenerateTag(context.Background(), latest.Artifact{
		Workspace: dir,
		ArtifactType: latest.ArtifactType{
			DockerArtifact: &latest.DockerArtifact{
				DockerfilePath: dockerfilePath,
				Target:         "target2",
			},
		},
	})
	if err != nil {
		t.Fatalf("GenerateTag failed for target2: %v", err)
	}

	if tag1 == tag2 {
		t.Errorf("expected different tags for different targets, got same: %s", tag1)
	}
}

Comment on lines +197 to +220
func TestGenerateTag_NoTarget(t *testing.T) {
runCtx := &runcontext.RunContext{}
dockerfilePath := filepath.Join(t.TempDir(), "Dockerfile")
if err := os.WriteFile(dockerfilePath, []byte("FROM busybox\nCMD [\"ps\", \"faux\"]\n"), 0644); err != nil {
t.Fatalf("failed to write dockerfile: %v", err)
}

digestExample, _ := NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts()))
tag, err := digestExample.GenerateTag(context.Background(), latest.Artifact{
Workspace: t.TempDir(),
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: dockerfilePath,
// No Target field set
},
},
})
if err != nil {
t.Fatalf("GenerateTag failed when no target: %v", err)
}
if tag == "" {
t.Errorf("expected a non-empty tag when no target is set")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similar to TestGenerateTag_DifferentTarget, this test incorrectly uses t.TempDir(). It's called once to create the Dockerfile's directory and then again for the artifact's workspace, resulting in two different directories. This should be corrected to use a single directory for consistency and to ensure fileHasher can compute a relative path correctly.

func TestGenerateTag_NoTarget(t *testing.T) {
	runCtx := &runcontext.RunContext{}
	dir := t.TempDir()
	dockerfilePath := filepath.Join(dir, "Dockerfile")
	if err := os.WriteFile(dockerfilePath, []byte("FROM busybox\nCMD [\"ps\", \"faux\"]\n"), 0644); err != nil {
		t.Fatalf("failed to write dockerfile: %v", err)
	}

	digestExample, _ := NewInputDigestTagger(runCtx, graph.ToArtifactGraph(runCtx.Artifacts()))
	tag, err := digestExample.GenerateTag(context.Background(), latest.Artifact{
		Workspace: dir,
		ArtifactType: latest.ArtifactType{
			DockerArtifact: &latest.DockerArtifact{
				DockerfilePath: dockerfilePath,
				// No Target field set
			},
		},
	})
	if err != nil {
		t.Fatalf("GenerateTag failed when no target: %v", err)
	}
	if tag == "" {
		t.Errorf("expected a non-empty tag when no target is set")
	}
}

Comment on lines 60 to 72
if image.DockerArtifact != nil {
srcFiles = append(srcFiles, image.DockerArtifact.DockerfilePath)
if image.DockerArtifact.Target != "" {
inputs = append(inputs, image.DockerArtifact.Target)
}
}

if image.KanikoArtifact != nil {
srcFiles = append(srcFiles, image.KanikoArtifact.DockerfilePath)
if image.KanikoArtifact.Target != "" {
inputs = append(inputs, image.KanikoArtifact.Target)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for handling DockerArtifact and KanikoArtifact is identical, leading to code duplication. To improve maintainability, you can refactor this to avoid repetition.

Assuming that an artifact can only be of one type (e.g., either DockerArtifact or KanikoArtifact, but not both), which is suggested by the oneOf=artifact struct tag, you can use an if/else if structure to determine the dockerfilePath and target first, and then process them in a single block.

Suggested change
if image.DockerArtifact != nil {
srcFiles = append(srcFiles, image.DockerArtifact.DockerfilePath)
if image.DockerArtifact.Target != "" {
inputs = append(inputs, image.DockerArtifact.Target)
}
}
if image.KanikoArtifact != nil {
srcFiles = append(srcFiles, image.KanikoArtifact.DockerfilePath)
if image.KanikoArtifact.Target != "" {
inputs = append(inputs, image.KanikoArtifact.Target)
}
}
var dockerfilePath, target string
var hasDockerfileArtifact bool
if image.DockerArtifact != nil {
dockerfilePath = image.DockerArtifact.DockerfilePath
target = image.DockerArtifact.Target
hasDockerfileArtifact = true
} else if image.KanikoArtifact != nil {
dockerfilePath = image.KanikoArtifact.DockerfilePath
target = image.KanikoArtifact.Target
hasDockerfileArtifact = true
}
if hasDockerfileArtifact {
srcFiles = append(srcFiles, dockerfilePath)
if target != "" {
inputs = append(inputs, target)
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Skaffold tagger ignores Docker target when calculating image tags

1 participant