Skip to content
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

Add path handling functions #3907

Merged
merged 2 commits into from
May 31, 2023

Conversation

gabriel-samfira
Copy link
Collaborator

@gabriel-samfira gabriel-samfira commented May 26, 2023

There are several places throughout the code where we repeat the same normalization patterns for paths. This change adds a couple of helper functions and makes CheckSystemDriveAndRemoveDriveLetter() on all platforms.

Additionally, all functions accept a platform flag that indicates the target OS we're building the image for. Decisions are made based on this flag instead of checking the platform on which buildkit is running.

This change was split from #3322. The functions added here are not yet called anywhere in the code. A subsequent PR which will depend on this one will be proposed soon with the bits that make use of these functions.

Comment on lines 158 to 178
if platform == "" {
platform = runtime.GOOS
}

if platform != "windows" {
return path, nil
}
Copy link
Member

Choose a reason for hiding this comment

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

I think the previous impl was better with CheckSystemDriveAndRemoveDriveLetter(path string) in path_unix.go and path_windows.go files. Can we keep that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Windows hosts can theoretically build Linux images, as it also has support for LCOW (Linux Containers on Windows). We should also be able to build Windows images on Linux. It's a good idea to use the target platform as an indicator of how to handle paths (as well as other things).

In short, the platform we're running on is not necessarily the platform we're building for.

@tonistiigi expressed a similar concern here: #3322 (comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There was a similar discussion in containerd. Which is why, in that instance, we ended up using the image.ImageSpec.OS as an indicator of how to handle the volumes when copying existing contents.

@tonistiigi tonistiigi added this to the v0.12.0 milestone May 29, 2023
Copy link
Member

@tonistiigi tonistiigi left a comment

Choose a reason for hiding this comment

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

Why are the tests OS specific? I thought the functions work the same independently from the current platform based on the input platform.

util/system/path.go Outdated Show resolved Hide resolved
util/system/path.go Outdated Show resolved Hide resolved
util/system/path.go Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@gabriel-samfira gabriel-samfira left a comment

Choose a reason for hiding this comment

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

Why are the tests OS specific? I thought the functions work the same independently from the current platform based on the input platform.

Good point. I will move them to a non OS specific file. Thanks for the review!

There are several places throughout the code where we repeat the same
normalization patterns for paths. This change adds a couple of helper
functions and makes CheckSystemDriveAndRemoveDriveLetter() on all
platforms.

Additionally, all functions accept a platform flag that indicates the
target OS we're building the image for. Decissions are made based on
this flag instead of checking the platform on which buildkit is running.

Signed-off-by: Gabriel Adrian Samfira <[email protected]>
@gabriel-samfira
Copy link
Collaborator Author

@tonistiigi changes made. PTAL.

// - optionally keep the trailing slashes on paths
// - paths are returned using forward slashes
func NormalizePath(parent, newPath, inputOS string, keepSlash bool) (string, error) {
if inputOS == "" {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to make os optional? Can't the caller just always pass it?

Copy link
Collaborator Author

@gabriel-samfira gabriel-samfira May 30, 2023

Choose a reason for hiding this comment

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

I can have the functions err out if it's empty if you prefer. The caller can of course, always pass it, even if they just pass the value of runtime.GOOS.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Was not sure what the preference was 😄 .

Copy link
Member

Choose a reason for hiding this comment

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

I'm also fine with just having unix as default like the toSlash etc do. Just having the behavior be different based on runtime.GOOS looks like something that can easily break in the future without we realizing it.

Make sure the behavior is consistent (eg. IsAbs is different atm).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fair enough. I set linux as the default.

Signed-off-by: Gabriel Adrian Samfira <[email protected]>
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