@@ -618,11 +618,8 @@ func WithUIDGID(uid, gid uint32) SpecOpts {
618
618
func WithUserID (uid uint32 ) SpecOpts {
619
619
return func (ctx context.Context , client Client , c * containers.Container , s * Spec ) (err error ) {
620
620
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 {
626
623
return u .Uid == int (uid )
627
624
})
628
625
if err != nil {
@@ -634,7 +631,12 @@ func WithUserID(uid uint32) SpecOpts {
634
631
}
635
632
s .Process .User .UID , s .Process .User .GID = uint32 (user .Uid ), uint32 (user .Gid )
636
633
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 )
638
640
}
639
641
if c .Snapshotter == "" {
640
642
return errors .Errorf ("no snapshotter set for container" )
@@ -649,20 +651,7 @@ func WithUserID(uid uint32) SpecOpts {
649
651
}
650
652
651
653
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 )
666
655
}
667
656
}
668
657
@@ -674,11 +663,8 @@ func WithUsername(username string) SpecOpts {
674
663
return func (ctx context.Context , client Client , c * containers.Container , s * Spec ) (err error ) {
675
664
setProcess (s )
676
665
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 {
682
668
return u .Name == username
683
669
})
684
670
if err != nil {
@@ -687,6 +673,12 @@ func WithUsername(username string) SpecOpts {
687
673
s .Process .User .UID , s .Process .User .GID = uint32 (user .Uid ), uint32 (user .Gid )
688
674
return nil
689
675
}
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
+ }
690
682
if c .Snapshotter == "" {
691
683
return errors .Errorf ("no snapshotter set for container" )
692
684
}
@@ -700,16 +692,7 @@ func WithUsername(username string) SpecOpts {
700
692
}
701
693
702
694
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 )
713
696
} else if s .Windows != nil {
714
697
s .Process .User .Username = username
715
698
} else {
0 commit comments