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

Split idtools to an internal package and package to be moved #49087

Merged
merged 2 commits into from
Jan 7, 2025

Conversation

dmcgowan
Copy link
Member

@dmcgowan dmcgowan commented Dec 13, 2024

The core part of utils which is used by pkg/archive and externally is kept in pkg/idtools and the rest moved to internal/usergroup. pkg/idtools should be ready to move to github.com/moby/sys after this.

@dmcgowan dmcgowan requested a review from tonistiigi as a code owner December 13, 2024 06:40
@dmcgowan dmcgowan force-pushed the split-idtools-internal branch 2 times, most recently from 7c4962e to d854eb6 Compare December 13, 2024 07:02
@thaJeztah thaJeztah added status/2-code-review kind/refactor PR's that refactor, or clean-up code area/go-sdk Changes affecting the Go SDK labels Dec 13, 2024
@thaJeztah thaJeztah added this to the 28.0.0 milestone Dec 13, 2024
pkg/idtools/idtools.go Outdated Show resolved Hide resolved
Comment on lines 222 to 225
// LoadIdentityMapping takes a requested username and
// using the data from /etc/sub{uid,gid} ranges, creates the
// proper uid and gid remapping ranges for that user/group pair
func LoadIdentityMapping(name string) (IdentityMapping, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a idtools_deprecated_unix.go file with aliases for the exported functions, and deprecate those, or won't that work due to a circular import in that case? (we might still be able to after idtools is moved to a separate module.

// LoadIdentityMapping takes a requested username and
// using the data from /etc/sub{uid,gid} ranges, creates the
// proper uid and gid remapping ranges for that user/group pair.
//
// Deprecated: this function was only used internally and will be removed in the next release.
func LoadIdentityMapping(name string) (IdentityMapping, error) {
	return usergroup. LoadIdentityMapping(name)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Can add back later if we need. Who is that deprecation message for though? I think we could have documentation about what is breaking with each release but if we require stretching every refactor to a two release cycle then we won't it will make doing all the valuable way more time consuming. IMO we are already doing major version bumps and we should cleanup as much as we can in v28, switch to go.mod, then actually semver the go sdk.

Copy link
Member

Choose a reason for hiding this comment

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

Who is that deprecation message for though?

Always hard to tell.

Screenshot 2024-12-13 at 18 00 04

The Deprecated annotations are effectively the documentation here, and those are picked up by linters and IDEs, so help users find their replacement. Updating references is not always under control by the user, because go modules may force them to use dependencies that perhaps have not yet switched over. We for sure won't be able to do this for all changes, but if it's only adding a file with aliases, that's not a ton of things to maintain, and can go a long way not having to deal with the aftermath.

Copy link
Member

Choose a reason for hiding this comment

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

☝️ OK: identified at least one; BuildKit breaks because of this;

0.137 + xx-go build '-gcflags=' -ldflags '-X github.com/moby/buildkit/version.Version=v0.18.0-55-g80600495c -X github.com/moby/buildkit/version.Revision=80600495cbc8c0c439d71fc77f146cb26fabcfa5 -X github.com/moby/buildkit/version.Package=github.com/moby/buildkit -extldflags '"'"'-static'"'" -tags 'osusergo netgo static_build seccomp ' -o /usr/bin/buildkitd ./cmd/buildkitd
71.51 # github.com/moby/buildkit/cmd/buildkitd
71.51 cmd/buildkitd/util_linux.go:25:27: undefined: idtools.LoadIdentityMapping

pkg/idtools/idtools_unix.go Outdated Show resolved Hide resolved
pkg/idtools/idtools_windows.go Outdated Show resolved Hide resolved
internal/usergroup/const_windows.go Show resolved Hide resolved
internal/usergroup/parser.go Fixed Show fixed Hide fixed
internal/usergroup/parser.go Fixed Show fixed Hide fixed
@thaJeztah thaJeztah mentioned this pull request Dec 21, 2024
74 tasks
Comment on lines 11 to 26
func mkdirAs(path string, _ os.FileMode, _ Identity, _, _ bool) error {
return system.MkdirAll(path, 0)
return os.MkdirAll(path, 0)
}
Copy link
Member

Choose a reason for hiding this comment

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

I went looking if the system.MkdirAll was needed at all anywhere (for the special handling of windows GUID volume paths), and it looks like it's no longer needed since go1.22, so I opened a PR to deprecate it, and remove its use;

☝️ this was the last use go pkg/system in BuildKit, so removing this line makes BuildKit no longer depend on it.

Copy link
Member

Choose a reason for hiding this comment

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

Did a quick rebase after that one was merged

@dmcgowan
Copy link
Member Author

dmcgowan commented Jan 4, 2025

I added back an implementation of LoadIdentityMapping which directly uses github.com/moby/sys/user. I kept the original internal one since that has the getent logic that Docker uses to load. If we want other projects to use that logic then we should add an implementation in github.com/moby/sys/user.

I think we should probably copy the package over directly to github.com/moby/sys/user (or a subpackage of it like usermap or something). Then consolidate the mapping types IDMap, Identity, IdentityMapping to github.com/moby/sys/user types.

pkg/idtools/idtools_unix.go Outdated Show resolved Hide resolved
pkg/idtools/idtools_windows.go Outdated Show resolved Hide resolved
pkg/idtools/idtools_unix.go Outdated Show resolved Hide resolved
pkg/idtools/idtools_unix.go Show resolved Hide resolved
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

Gave it a try what it'd look like with changing the order of things;

Comment on lines 143 to 157
uidstr := strconv.Itoa(usr.Uid)
rangeList, err := user.ParseSubIDFileFilter(path, func(sid user.SubID) bool {
return sid.Name == uidstr
})
if err != nil {
return nil, err
}
if len(rangeList) == 0 {
rangeList, err = parseSubuid(usr.Name)
rangeList, err = user.ParseSubIDFileFilter(path, func(sid user.SubID) bool {
return sid.Name == usr.Name
})
if err != nil {
return nil, err
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Had to double-check this one, because the first filter was matching numeric Uid with SubID.Name, but looks like moby/sys/user does the same, and checking the man-page; https://man7.org/linux/man-pages/man5/subuid.5.html

Each line in /etc/subuid contains a user name and a range of
subordinate user ids that user is allowed to use. This is
specified with three fields delimited by colons (“:”). These
fields are:

• login name or UID
• numerical subordinate user ID
• numerical subordinate user ID count

One thing we could do is to take the same approach as is used there, and to combine them to a single filter;

func currentUserSubIDs(fileName string) ([]SubID, error) {
u, err := CurrentUser()
if err != nil {
return nil, err
}
filter := func(entry SubID) bool {
return entry.Name == u.Name || entry.Name == strconv.Itoa(u.Uid)
}
return ParseSubIDFileFilter(fileName, filter)
}

pkg/idtools/idtools_windows.go Outdated Show resolved Hide resolved
internal/usergroup/parser_test.go Outdated Show resolved Hide resolved
internal/usergroup/parser.go Outdated Show resolved Hide resolved
internal/usergroup/parser.go Outdated Show resolved Hide resolved
internal/usergroup/add_linux.go Outdated Show resolved Hide resolved
Separare idtools functionality that is used internally from the
functionlality used by importers. The `pkg/idtools` package is now
much smaller and more generic.

Signed-off-by: Derek McGowan <derek@mcg.dev>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Derek McGowan <derek@mcg.dev>
@dmcgowan dmcgowan force-pushed the split-idtools-internal branch from 0f356a5 to 3fa5e7e Compare January 7, 2025 19:21
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

@thaJeztah thaJeztah merged commit 6c523af into moby:master Jan 7, 2025
143 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/go-sdk Changes affecting the Go SDK kind/refactor PR's that refactor, or clean-up code status/2-code-review
Projects
Development

Successfully merging this pull request may close these issues.

2 participants