Skip to content

Commit

Permalink
vendor: switch from idtools to moby/sys/user
Browse files Browse the repository at this point in the history
Convert usages of `github.com/docker/docker/pkg/idtools` to
`github.com/moby/sys/user` in order to break the dependency between
buildkit and docker.

Signed-off-by: Jonathan A. Sternberg <[email protected]>
  • Loading branch information
jsternberg committed Feb 28, 2025
1 parent 478f176 commit 1ba5aa4
Show file tree
Hide file tree
Showing 40 changed files with 500 additions and 205 deletions.
6 changes: 3 additions & 3 deletions cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/containerd/containerd/v2/pkg/gc"
"github.com/containerd/containerd/v2/pkg/labels"
cerrdefs "github.com/containerd/errdefs"
"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/cache/metadata"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/identity"
Expand All @@ -25,6 +24,7 @@ import (
"github.com/moby/buildkit/util/disk"
"github.com/moby/buildkit/util/flightcontrol"
"github.com/moby/buildkit/util/progress"
"github.com/moby/sys/user"
digest "github.com/opencontainers/go-digest"
imagespecidentity "github.com/opencontainers/image-spec/identity"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -61,7 +61,7 @@ type Accessor interface {

New(ctx context.Context, parent ImmutableRef, s session.Group, opts ...RefOption) (MutableRef, error)
GetMutable(ctx context.Context, id string, opts ...RefOption) (MutableRef, error) // Rebase?
IdentityMapping() *idtools.IdentityMapping
IdentityMapping() *user.IdentityMapping
Merge(ctx context.Context, parents []ImmutableRef, pg progress.Controller, opts ...RefOption) (ImmutableRef, error)
Diff(ctx context.Context, lower, upper ImmutableRef, pg progress.Controller, opts ...RefOption) (ImmutableRef, error)
}
Expand Down Expand Up @@ -337,7 +337,7 @@ func (cm *cacheManager) init(ctx context.Context) error {
}

// IdentityMapping returns the userns remapping used for refs
func (cm *cacheManager) IdentityMapping() *idtools.IdentityMapping {
func (cm *cacheManager) IdentityMapping() *user.IdentityMapping {
return cm.Snapshotter.IdentityMapping()
}

Expand Down
6 changes: 3 additions & 3 deletions cache/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/containerd/containerd/v2/core/snapshots"
"github.com/containerd/containerd/v2/pkg/labels"
cerrdefs "github.com/containerd/errdefs"
"github.com/docker/docker/pkg/idtools"
"github.com/hashicorp/go-multierror"
"github.com/moby/buildkit/cache/config"
"github.com/moby/buildkit/identity"
Expand All @@ -33,6 +32,7 @@ import (
rootlessmountopts "github.com/moby/buildkit/util/rootless/mountopts"
"github.com/moby/buildkit/util/winlayers"
"github.com/moby/sys/mountinfo"
"github.com/moby/sys/user"
"github.com/moby/sys/userns"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -48,7 +48,7 @@ type Ref interface {
Mountable
RefMetadata
Release(context.Context) error
IdentityMapping() *idtools.IdentityMapping
IdentityMapping() *user.IdentityMapping
DescHandler(digest.Digest) *DescHandler
}

Expand Down Expand Up @@ -309,7 +309,7 @@ func (cr *cacheRecord) isLazy(ctx context.Context) (bool, error) {
return false, nil
}

func (cr *cacheRecord) IdentityMapping() *idtools.IdentityMapping {
func (cr *cacheRecord) IdentityMapping() *user.IdentityMapping {
return cr.cm.IdentityMapping()
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/buildkitd/util_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package main
import (
"strings"

"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/sys/user"
"github.com/pkg/errors"
)

func parseIdentityMapping(str string) (*idtools.IdentityMapping, error) {
func parseIdentityMapping(str string) (*user.IdentityMapping, error) {
if str == "" {
return nil, nil
}
Expand All @@ -22,7 +22,7 @@ func parseIdentityMapping(str string) (*idtools.IdentityMapping, error) {

bklog.L.Debugf("user namespaces: ID ranges will be mapped to subuid ranges of: %s", username)

mappings, err := idtools.LoadIdentityMapping(username)
mappings, err := user.LoadIdentityMapping(username)
if err != nil {
return nil, errors.Wrap(err, "failed to create ID mappings")
}
Expand Down
2 changes: 1 addition & 1 deletion executor/containerdexecutor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (w *containerdExecutor) Run(ctx context.Context, id string, root executor.M
defer releasers()
}

if err := w.ensureCWD(ctx, details, meta); err != nil {
if err := w.ensureCWD(details, meta); err != nil {
return nil, err
}

Expand Down
11 changes: 3 additions & 8 deletions executor/containerdexecutor/executor_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"github.com/containerd/containerd/v2/core/mount"
containerdoci "github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/continuity/fs"
"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/executor/oci"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/network"
rootlessspecconv "github.com/moby/buildkit/util/rootless/specconv"
"github.com/moby/sys/user"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -101,7 +101,7 @@ func (w *containerdExecutor) prepareExecutionEnv(ctx context.Context, rootMount
return resolvConf, hostsFile, releaseAll, nil
}

func (w *containerdExecutor) ensureCWD(_ context.Context, details *containerState, meta executor.Meta) error {
func (w *containerdExecutor) ensureCWD(details *containerState, meta executor.Meta) error {
newp, err := fs.RootPath(details.rootfsPath, meta.Cwd)
if err != nil {
return errors.Wrapf(err, "working dir %s points to invalid target", newp)
Expand All @@ -112,13 +112,8 @@ func (w *containerdExecutor) ensureCWD(_ context.Context, details *containerStat
return err
}

identity := idtools.Identity{
UID: int(uid),
GID: int(gid),
}

if _, err := os.Stat(newp); err != nil {
if err := idtools.MkdirAllAndChown(newp, 0755, identity); err != nil {
if err := user.MkdirAllAndChown(newp, 0755, int(uid), int(gid)); err != nil {
return errors.Wrapf(err, "failed to create working directory %s", newp)
}
}
Expand Down
14 changes: 4 additions & 10 deletions executor/containerdexecutor/executor_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
ctd "github.com/containerd/containerd/v2/client"
containerdoci "github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/continuity/fs"
"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/executor/oci"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/network"
"github.com/moby/buildkit/util/windows"
"github.com/moby/sys/user"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -48,13 +47,7 @@ func (w *containerdExecutor) prepareExecutionEnv(ctx context.Context, rootMount
return "", "", releaseAll, nil
}

func (w *containerdExecutor) ensureCWD(ctx context.Context, details *containerState, meta executor.Meta) (err error) {
// TODO(gabriel-samfira): Use a snapshot?
identity, err := windows.ResolveUsernameToSID(ctx, w, details.rootMounts, meta.User)
if err != nil {
return errors.Wrap(err, "getting user SID")
}

func (w *containerdExecutor) ensureCWD(details *containerState, meta executor.Meta) (err error) {
lm := snapshot.LocalMounterWithMounts(details.rootMounts)
rootfsPath, err := lm.Mount()
if err != nil {
Expand All @@ -68,7 +61,8 @@ func (w *containerdExecutor) ensureCWD(ctx context.Context, details *containerSt
}

if _, err := os.Stat(newp); err != nil {
if err := idtools.MkdirAllAndChown(newp, 0755, identity); err != nil {
// uid and gid are not used on windows.
if err := user.MkdirAllAndChown(newp, 0755, 0, 0); err != nil {
return errors.Wrapf(err, "failed to create working directory %s", newp)
}
}
Expand Down
4 changes: 2 additions & 2 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"syscall"

"github.com/containerd/containerd/v2/core/mount"
"github.com/docker/docker/pkg/idtools"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/sys/user"
)

type Meta struct {
Expand All @@ -33,7 +33,7 @@ type Meta struct {

type MountableRef interface {
Mount() ([]mount.Mount, func() error, error)
IdentityMapping() *idtools.IdentityMapping
IdentityMapping() *user.IdentityMapping
}

type Mountable interface {
Expand Down
10 changes: 5 additions & 5 deletions executor/oci/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"os"
"path/filepath"

"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/identity"
"github.com/moby/sys/user"
"github.com/pkg/errors"
)

const defaultHostname = "buildkitsandbox"

func GetHostsFile(ctx context.Context, stateDir string, extraHosts []executor.HostIP, idmap *idtools.IdentityMapping, hostname string) (string, func(), error) {
func GetHostsFile(ctx context.Context, stateDir string, extraHosts []executor.HostIP, idmap *user.IdentityMapping, hostname string) (string, func(), error) {
if len(extraHosts) != 0 || hostname != defaultHostname {
return makeHostsFile(stateDir, extraHosts, idmap, hostname)
}
Expand All @@ -30,7 +30,7 @@ func GetHostsFile(ctx context.Context, stateDir string, extraHosts []executor.Ho
return filepath.Join(stateDir, "hosts"), func() {}, nil
}

func makeHostsFile(stateDir string, extraHosts []executor.HostIP, idmap *idtools.IdentityMapping, hostname string) (string, func(), error) {
func makeHostsFile(stateDir string, extraHosts []executor.HostIP, idmap *user.IdentityMapping, hostname string) (string, func(), error) {
p := filepath.Join(stateDir, "hosts")
if len(extraHosts) != 0 || hostname != defaultHostname {
p += "." + identity.NewID()
Expand Down Expand Up @@ -60,8 +60,8 @@ func makeHostsFile(stateDir string, extraHosts []executor.HostIP, idmap *idtools
}

if idmap != nil {
root := idmap.RootPair()
if err := os.Chown(tmpPath, root.UID, root.GID); err != nil {
uid, gid := idmap.RootPair()
if err := os.Chown(tmpPath, uid, gid); err != nil {
return "", nil, errors.WithStack(err)
}
}
Expand Down
16 changes: 9 additions & 7 deletions executor/oci/resolvconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import (
"path/filepath"

"github.com/docker/docker/libnetwork/resolvconf"
"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/flightcontrol"
"github.com/moby/sys/user"
"github.com/pkg/errors"
)

var g flightcontrol.Group[struct{}]
var notFirstRun bool
var lastNotEmpty bool
var (
g flightcontrol.Group[struct{}]
notFirstRun bool
lastNotEmpty bool
)

// overridden by tests
var resolvconfPath = func(netMode pb.NetMode) string {
Expand All @@ -36,7 +38,7 @@ type DNSConfig struct {
SearchDomains []string
}

func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.IdentityMapping, dns *DNSConfig, netMode pb.NetMode) (string, error) {
func GetResolvConf(ctx context.Context, stateDir string, idmap *user.IdentityMapping, dns *DNSConfig, netMode pb.NetMode) (string, error) {
p := filepath.Join(stateDir, "resolv.conf")
if netMode == pb.NetMode_HOST {
p = filepath.Join(stateDir, "resolv-host.conf")
Expand Down Expand Up @@ -116,8 +118,8 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
}

if idmap != nil {
root := idmap.RootPair()
if err := os.Chown(tmpPath, root.UID, root.GID); err != nil {
uid, gid := idmap.RootPair()
if err := os.Chown(tmpPath, uid, gid); err != nil {
return struct{}{}, errors.WithStack(err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions executor/oci/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/docker/docker/pkg/idtools"
"github.com/mitchellh/hashstructure/v2"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/snapshot"
Expand All @@ -22,6 +21,7 @@ import (
rootlessmountopts "github.com/moby/buildkit/util/rootless/mountopts"
"github.com/moby/buildkit/util/system"
traceexec "github.com/moby/buildkit/util/tracing/exec"
"github.com/moby/sys/user"
"github.com/moby/sys/userns"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/selinux/go-selinux"
Expand Down Expand Up @@ -61,7 +61,7 @@ func (pm ProcessMode) String() string {

// GenerateSpec generates spec using containerd functionality.
// opts are ignored for s.Process, s.Hostname, and s.Mounts .
func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mount, id, resolvConf, hostsFile string, namespace network.Namespace, cgroupParent string, processMode ProcessMode, idmap *idtools.IdentityMapping, apparmorProfile string, selinuxB bool, tracingSocket string, cdiManager *cdidevices.Manager, opts ...oci.SpecOpts) (*specs.Spec, func(), error) {
func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mount, id, resolvConf, hostsFile string, namespace network.Namespace, cgroupParent string, processMode ProcessMode, idmap *user.IdentityMapping, apparmorProfile string, selinuxB bool, tracingSocket string, cdiManager *cdidevices.Manager, opts ...oci.SpecOpts) (*specs.Spec, func(), error) {
c := &containers.Container{
ID: id,
}
Expand Down
4 changes: 2 additions & 2 deletions executor/oci/spec_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/continuity/fs"
"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/solver/llbsolver/cdidevices"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/sys/user"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
Expand All @@ -27,7 +27,7 @@ func generateProcessModeOpts(mode ProcessMode) ([]oci.SpecOpts, error) {
return nil, nil
}

func generateIDmapOpts(idmap *idtools.IdentityMapping) ([]oci.SpecOpts, error) {
func generateIDmapOpts(idmap *user.IdentityMapping) ([]oci.SpecOpts, error) {
if idmap == nil {
return nil, nil
}
Expand Down
4 changes: 2 additions & 2 deletions executor/oci/spec_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/continuity/fs"
"github.com/docker/docker/pkg/idtools"
"github.com/moby/buildkit/solver/llbsolver/cdidevices"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/sys/user"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -35,7 +35,7 @@ func generateProcessModeOpts(mode ProcessMode) ([]oci.SpecOpts, error) {
return nil, nil
}

func generateIDmapOpts(idmap *idtools.IdentityMapping) ([]oci.SpecOpts, error) {
func generateIDmapOpts(idmap *user.IdentityMapping) ([]oci.SpecOpts, error) {
if idmap == nil {
return nil, nil
}
Expand Down
14 changes: 7 additions & 7 deletions executor/oci/spec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"github.com/containerd/containerd/v2/pkg/oci"
cdseccomp "github.com/containerd/containerd/v2/pkg/seccomp"
"github.com/containerd/continuity/fs"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/profiles/seccomp"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/solver/llbsolver/cdidevices"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/entitlements/security"
"github.com/moby/sys/user"
specs "github.com/opencontainers/runtime-spec/specs-go"
selinux "github.com/opencontainers/selinux/go-selinux"
"github.com/opencontainers/selinux/go-selinux/label"
Expand Down Expand Up @@ -106,7 +106,7 @@ func generateProcessModeOpts(mode ProcessMode) ([]oci.SpecOpts, error) {
return nil, nil
}

func generateIDmapOpts(idmap *idtools.IdentityMapping) ([]oci.SpecOpts, error) {
func generateIDmapOpts(idmap *user.IdentityMapping) ([]oci.SpecOpts, error) {
if idmap == nil {
return nil, nil
}
Expand All @@ -115,13 +115,13 @@ func generateIDmapOpts(idmap *idtools.IdentityMapping) ([]oci.SpecOpts, error) {
}, nil
}

func specMapping(s []idtools.IDMap) []specs.LinuxIDMapping {
func specMapping(s []user.IDMap) []specs.LinuxIDMapping {
var ids []specs.LinuxIDMapping
for _, item := range s {
ids = append(ids, specs.LinuxIDMapping{
HostID: uint32(item.HostID),
ContainerID: uint32(item.ContainerID),
Size: uint32(item.Size),
HostID: uint32(item.ParentID),
ContainerID: uint32(item.ID),
Size: uint32(item.Count),
})
}
return ids
Expand Down Expand Up @@ -286,7 +286,7 @@ func cgroupV2NamespaceSupported() bool {
}

func sub(m mount.Mount, subPath string) (mount.Mount, func() error, error) {
var retries = 10
retries := 10
root := m.Source
for {
src, err := fs.RootPath(root, subPath)
Expand Down
Loading

0 comments on commit 1ba5aa4

Please sign in to comment.