@@ -618,11 +618,8 @@ func WithUIDGID(uid, gid uint32) SpecOpts {
618618func WithUserID (uid uint32 ) SpecOpts {
619619 return func (ctx context.Context , client Client , c * containers.Container , s * Spec ) (err error ) {
620620 setProcess (s )
621- if c .Snapshotter == "" && c .SnapshotKey == "" {
622- if ! isRootfsAbs (s .Root .Path ) {
623- return errors .Errorf ("rootfs absolute path is required" )
624- }
625- user , err := UserFromPath (s .Root .Path , func (u user.User ) bool {
621+ setUser := func (root string ) error {
622+ user , err := UserFromPath (root , func (u user.User ) bool {
626623 return u .Uid == int (uid )
627624 })
628625 if err != nil {
@@ -634,7 +631,12 @@ func WithUserID(uid uint32) SpecOpts {
634631 }
635632 s .Process .User .UID , s .Process .User .GID = uint32 (user .Uid ), uint32 (user .Gid )
636633 return nil
637-
634+ }
635+ if c .Snapshotter == "" && c .SnapshotKey == "" {
636+ if ! isRootfsAbs (s .Root .Path ) {
637+ return errors .New ("rootfs absolute path is required" )
638+ }
639+ return setUser (s .Root .Path )
638640 }
639641 if c .Snapshotter == "" {
640642 return errors .Errorf ("no snapshotter set for container" )
@@ -649,20 +651,7 @@ func WithUserID(uid uint32) SpecOpts {
649651 }
650652
651653 mounts = tryReadonlyMounts (mounts )
652- return mount .WithTempMount (ctx , mounts , func (root string ) error {
653- user , err := UserFromPath (root , func (u user.User ) bool {
654- return u .Uid == int (uid )
655- })
656- if err != nil {
657- if os .IsNotExist (err ) || err == ErrNoUsersFound {
658- s .Process .User .UID , s .Process .User .GID = uid , 0
659- return nil
660- }
661- return err
662- }
663- s .Process .User .UID , s .Process .User .GID = uint32 (user .Uid ), uint32 (user .Gid )
664- return nil
665- })
654+ return mount .WithTempMount (ctx , mounts , setUser )
666655 }
667656}
668657
@@ -674,11 +663,8 @@ func WithUsername(username string) SpecOpts {
674663 return func (ctx context.Context , client Client , c * containers.Container , s * Spec ) (err error ) {
675664 setProcess (s )
676665 if s .Linux != nil {
677- if c .Snapshotter == "" && c .SnapshotKey == "" {
678- if ! isRootfsAbs (s .Root .Path ) {
679- return errors .Errorf ("rootfs absolute path is required" )
680- }
681- user , err := UserFromPath (s .Root .Path , func (u user.User ) bool {
666+ setUser := func (root string ) error {
667+ user , err := UserFromPath (root , func (u user.User ) bool {
682668 return u .Name == username
683669 })
684670 if err != nil {
@@ -687,6 +673,12 @@ func WithUsername(username string) SpecOpts {
687673 s .Process .User .UID , s .Process .User .GID = uint32 (user .Uid ), uint32 (user .Gid )
688674 return nil
689675 }
676+ if c .Snapshotter == "" && c .SnapshotKey == "" {
677+ if ! isRootfsAbs (s .Root .Path ) {
678+ return errors .New ("rootfs absolute path is required" )
679+ }
680+ return setUser (s .Root .Path )
681+ }
690682 if c .Snapshotter == "" {
691683 return errors .Errorf ("no snapshotter set for container" )
692684 }
@@ -700,16 +692,7 @@ func WithUsername(username string) SpecOpts {
700692 }
701693
702694 mounts = tryReadonlyMounts (mounts )
703- return mount .WithTempMount (ctx , mounts , func (root string ) error {
704- user , err := UserFromPath (root , func (u user.User ) bool {
705- return u .Name == username
706- })
707- if err != nil {
708- return err
709- }
710- s .Process .User .UID , s .Process .User .GID = uint32 (user .Uid ), uint32 (user .Gid )
711- return nil
712- })
695+ return mount .WithTempMount (ctx , mounts , setUser )
713696 } else if s .Windows != nil {
714697 s .Process .User .Username = username
715698 } else {
0 commit comments