Skip to content

Make inputDigest hash calculation independent of workspace path #6522

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

Merged
merged 3 commits into from
Sep 8, 2021

Conversation

yhrn
Copy link
Contributor

@yhrn yhrn commented Aug 26, 2021

Description

The point of this change is to ensure that digest tag calculation is always independent of the workspace location. Put differently, with this change the same tag will be calculated for an image regardless of if it is defined directly in the skaffold configuration passed to skaffold or in a in another configuration referenced from the requires section. Previously the latter situation would result in absolute paths being used resulting in a different digest.

@yhrn yhrn requested a review from a team as a code owner August 26, 2021 15:25
@yhrn yhrn requested a review from briandealwis August 26, 2021 15:25
@google-cla
Copy link

google-cla bot commented Aug 26, 2021

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no label Aug 26, 2021
@milowe
Copy link
Contributor

milowe commented Aug 26, 2021

@googlebot I fixed it.

@google-cla
Copy link

google-cla bot commented Aug 26, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@milowe
Copy link
Contributor

milowe commented Aug 26, 2021

@googlebot I consent.

@google-cla google-cla bot added cla: yes and removed cla: no labels Aug 26, 2021
@codecov
Copy link

codecov bot commented Aug 26, 2021

Codecov Report

Merging #6522 (158c7c9) into main (997edc8) will increase coverage by 0.00%.
The diff coverage is 50.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #6522   +/-   ##
=======================================
  Coverage   70.43%   70.43%           
=======================================
  Files         515      515           
  Lines       23127    23147   +20     
=======================================
+ Hits        16289    16304   +15     
- Misses       5780     5784    +4     
- Partials     1058     1059    +1     
Impacted Files Coverage Δ
pkg/skaffold/tag/input_digest.go 32.55% <50.00%> (+0.05%) ⬆️
pkg/skaffold/event/v2/event.go 84.58% <0.00%> (-0.06%) ⬇️
cmd/skaffold/skaffold.go 0.00% <0.00%> (ø)
pkg/diag/validator/pod.go 1.32% <0.00%> (ø)
pkg/skaffold/event/v2/build.go 100.00% <0.00%> (ø)
pkg/skaffold/event/v2/deploy.go 100.00% <0.00%> (ø)
pkg/skaffold/event/v2/logger.go 76.27% <0.00%> (ø)
pkg/skaffold/build/ko/builder.go 100.00% <0.00%> (ø)
pkg/skaffold/event/v2/status_check.go 85.45% <0.00%> (ø)
.../skaffold/kubernetes/status/resource/deployment.go 83.85% <0.00%> (ø)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d3839f4...158c7c9. Read the comment docs.

Copy link
Member

@briandealwis briandealwis left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Comment on lines 94 to 98
if workspacePath == "." {
h.Write([]byte(filepath.Clean(path)))
} else {
h.Write([]byte(filepath.Clean(strings.Replace(path, workspacePath+string(os.PathSeparator), "", 1))))
}
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can change that but it would mean that we'd (try to) use a relative path even if the file is outside of the workspace. Not sure if that is what we want or if it is even a possible case? Up to you.

Copy link
Member

Choose a reason for hiding this comment

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

Files should typically be within the workspace. filepath.Rel() returns an error if the file could not be made relative, so we could try to relativize and otherwise use the fullpath.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, makes sense. I'll change that shortly.

Comment on lines +31 to +34
cwdBackup, err := os.Getwd()
t.RequireNoError(err)
t.RequireNoError(os.Chdir(dir))
defer func() { t.RequireNoError(os.Chdir(cwdBackup)) }()
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem necessary. You could create two directories with the same file and validate that they return the same hash.

Copy link
Contributor Author

@yhrn yhrn Aug 26, 2021

Choose a reason for hiding this comment

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

Then we wouldn't be able to verify that a relative path to the workspace and it files yields the same result as absolute paths (see the if clause in the function under test). In order to find the file and hash its contents in the relative path case we need to change cwd here.

Copy link
Member

Choose a reason for hiding this comment

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

If we create d1 and d2 with the same contents, then the fileHash of both should be equal. Currently this would fail since we're hashing the full paths of the files in d1 and d2.

Typically Skaffold doesn't change the working directory — it rather executes programs with a different working directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The scenario that broke us before was that when calculating the tag for an image defined in the "root" skaffold config all paths were relative to the workspace directory. Even the workspace directory path was relative, i.e. just ".". When calculating the same image tag when imported via the requires section all paths, including the workspace path, were absolute. For the calculation to work at all in the first case cwd must be the directory of the skaffold config or we can't read the file contents.

So this is the scenario I'm trying to test here, that providing paths relative to the workspace directory to the digest calculation will yield the same result as when providing absolute paths. And I can't come up with any other way of getting the relative calculation to work without changing cwd.

I can add another test case that verifies that calculating for two different absolute paths works as expected to if you want.

Copy link
Contributor Author

@yhrn yhrn Sep 2, 2021

Choose a reason for hiding this comment

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

I added more tests, including one that tests two different absolute paths and would have failed without the fix.

I would rather leave the test we are discussing here as it is a different case that is closer to the situation where this hit us initially, but if you feel that it should be removed I can do so.

Copy link
Member

@briandealwis briandealwis left a comment

Choose a reason for hiding this comment

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

Thanks @yhrn!

@briandealwis briandealwis merged commit df7751d into GoogleContainerTools:main Sep 8, 2021
@yhrn yhrn deleted the fix-requires-digest branch September 8, 2021 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants