-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix target in digest tagger generation #9826 #9867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix target in digest tagger generation #9826 #9867
Conversation
Add the target to the input list so it affects the final hash. Closes GoogleContainerTools#9826
There was a problem hiding this 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
-
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. ↩
There was a problem hiding this 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.
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test has a couple of issues in its setup:
- The
inputDigestTagger
is instantiated twice unnecessarily. The tagger instance can be reused for generating both tags since the operation is stateless for these calls. - 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 causesfilepath.Rel
to fail infileHasher
, 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)
}
}
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") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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")
}
}
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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) | |
} | |
} |
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)