Skip to content

Commit b999247

Browse files
Merge pull request moby#5179 from tonistiigi/executor-err-upt
executor error improvements
2 parents 5c66a49 + 304b6ec commit b999247

File tree

11 files changed

+99
-63
lines changed

11 files changed

+99
-63
lines changed

executor/oci/hosts.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,34 @@ func makeHostsFile(stateDir string, extraHosts []executor.HostIP, idmap *idtools
4040
return "", func() {}, nil
4141
}
4242
if !errors.Is(err, os.ErrNotExist) {
43-
return "", nil, err
43+
return "", nil, errors.WithStack(err)
4444
}
4545

4646
b := &bytes.Buffer{}
4747
if _, err := b.Write([]byte(initHostsFile(hostname))); err != nil {
48-
return "", nil, err
48+
return "", nil, errors.WithStack(err)
4949
}
5050

5151
for _, h := range extraHosts {
5252
if _, err := b.Write([]byte(fmt.Sprintf("%s\t%s\n", h.IP.String(), h.Host))); err != nil {
53-
return "", nil, err
53+
return "", nil, errors.WithStack(err)
5454
}
5555
}
5656

5757
tmpPath := p + ".tmp"
5858
if err := os.WriteFile(tmpPath, b.Bytes(), 0644); err != nil {
59-
return "", nil, err
59+
return "", nil, errors.WithStack(err)
6060
}
6161

6262
if idmap != nil {
6363
root := idmap.RootPair()
6464
if err := os.Chown(tmpPath, root.UID, root.GID); err != nil {
65-
return "", nil, err
65+
return "", nil, errors.WithStack(err)
6666
}
6767
}
6868

6969
if err := os.Rename(tmpPath, p); err != nil {
70-
return "", nil, err
70+
return "", nil, errors.WithStack(err)
7171
}
7272
return p, func() {
7373
os.RemoveAll(p)

executor/oci/resolvconf.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
3939
fi, err := os.Stat(p)
4040
if err != nil {
4141
if !errors.Is(err, os.ErrNotExist) {
42-
return struct{}{}, err
42+
return struct{}{}, errors.WithStack(err)
4343
}
4444
generate = true
4545
}
@@ -65,7 +65,7 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
6565

6666
dt, err := os.ReadFile(resolvconfPath())
6767
if err != nil && !errors.Is(err, os.ErrNotExist) {
68-
return struct{}{}, err
68+
return struct{}{}, errors.WithStack(err)
6969
}
7070

7171
tmpPath := p + ".tmp"
@@ -87,32 +87,32 @@ func GetResolvConf(ctx context.Context, stateDir string, idmap *idtools.Identity
8787

8888
f, err := resolvconf.Build(tmpPath, dnsNameservers, dnsSearchDomains, dnsOptions)
8989
if err != nil {
90-
return struct{}{}, err
90+
return struct{}{}, errors.WithStack(err)
9191
}
9292
dt = f.Content
9393
}
9494

9595
if netMode != pb.NetMode_HOST || len(resolvconf.GetNameservers(dt, resolvconf.IP)) == 0 {
9696
f, err := resolvconf.FilterResolvDNS(dt, true)
9797
if err != nil {
98-
return struct{}{}, err
98+
return struct{}{}, errors.WithStack(err)
9999
}
100100
dt = f.Content
101101
}
102102

103103
if err := os.WriteFile(tmpPath, dt, 0644); err != nil {
104-
return struct{}{}, err
104+
return struct{}{}, errors.WithStack(err)
105105
}
106106

107107
if idmap != nil {
108108
root := idmap.RootPair()
109109
if err := os.Chown(tmpPath, root.UID, root.GID); err != nil {
110-
return struct{}{}, err
110+
return struct{}{}, errors.WithStack(err)
111111
}
112112
}
113113

114114
if err := os.Rename(tmpPath, p); err != nil {
115-
return struct{}{}, err
115+
return struct{}{}, errors.WithStack(err)
116116
}
117117
return struct{}{}, nil
118118
})

executor/oci/spec.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
8484
ctx = namespaces.WithNamespace(ctx, "buildkit")
8585
}
8686

87-
if mountOpts, err := generateMountOpts(resolvConf, hostsFile); err == nil {
88-
opts = append(opts, mountOpts...)
89-
} else {
90-
return nil, nil, err
91-
}
87+
opts = append(opts, generateMountOpts(resolvConf, hostsFile)...)
9288

9389
if securityOpts, err := generateSecurityOpts(meta.SecurityMode, apparmorProfile, selinuxB); err == nil {
9490
opts = append(opts, securityOpts...)
@@ -135,7 +131,7 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
135131

136132
s, err := oci.GenerateSpec(ctx, nil, c, opts...)
137133
if err != nil {
138-
return nil, nil, err
134+
return nil, nil, errors.WithStack(err)
139135
}
140136

141137
if cgroupV2NamespaceSupported() {
@@ -151,7 +147,7 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
151147

152148
// set the networking information on the spec
153149
if err := namespace.Set(s); err != nil {
154-
return nil, nil, err
150+
return nil, nil, errors.WithStack(err)
155151
}
156152

157153
sm := &submounts{}
@@ -196,6 +192,12 @@ func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mou
196192
mount, err = sm.subMount(mount, m.Selector)
197193
if err != nil {
198194
releaseAll()
195+
var os *os.PathError
196+
if errors.As(err, &os) {
197+
if strings.HasSuffix(os.Path, m.Selector) {
198+
os.Path = m.Selector
199+
}
200+
}
199201
return nil, nil, err
200202
}
201203
s.Mounts = append(s.Mounts, specs.Mount{
@@ -248,7 +250,7 @@ func (s *submounts) subMount(m mount.Mount, subPath string) (mount.Mount, error)
248250
}
249251
h, err := hashstructure.Hash(m, hashstructure.FormatV2, nil)
250252
if err != nil {
251-
return mount.Mount{}, err
253+
return mount.Mount{}, errors.WithStack(err)
252254
}
253255
if mr, ok := s.m[h]; ok {
254256
if sm, ok := mr.subRefs[subPath]; ok {

executor/oci/spec_freebsd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func withProcessArgs(args ...string) oci.SpecOpts {
1414
return oci.WithProcessArgs(args...)
1515
}
1616

17-
func generateMountOpts(_, _ string) ([]oci.SpecOpts, error) {
18-
return nil, nil
17+
func generateMountOpts(_, _ string) []oci.SpecOpts {
18+
return nil
1919
}
2020

2121
// generateSecurityOpts may affect mounts, so must be called after generateMountOpts

executor/oci/spec_linux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ func withProcessArgs(args ...string) oci.SpecOpts {
3838
return oci.WithProcessArgs(args...)
3939
}
4040

41-
func generateMountOpts(resolvConf, hostsFile string) ([]oci.SpecOpts, error) {
41+
func generateMountOpts(resolvConf, hostsFile string) []oci.SpecOpts {
4242
return []oci.SpecOpts{
4343
// https://github.com/moby/buildkit/issues/429
4444
withRemovedMount("/run"),
4545
withROBind(resolvConf, "/etc/resolv.conf"),
4646
withROBind(hostsFile, "/etc/hosts"),
4747
withCGroup(),
48-
}, nil
48+
}
4949
}
5050

5151
// generateSecurityOpts may affect mounts, so must be called after generateMountOpts
@@ -260,13 +260,13 @@ func sub(m mount.Mount, subPath string) (mount.Mount, func() error, error) {
260260
// similar to runc.WithProcfd
261261
fh, err := os.OpenFile(src, unix.O_PATH|unix.O_CLOEXEC, 0)
262262
if err != nil {
263-
return mount.Mount{}, nil, err
263+
return mount.Mount{}, nil, errors.WithStack(err)
264264
}
265265

266266
fdPath := "/proc/self/fd/" + strconv.Itoa(int(fh.Fd()))
267267
if resolved, err := os.Readlink(fdPath); err != nil {
268268
fh.Close()
269-
return mount.Mount{}, nil, err
269+
return mount.Mount{}, nil, errors.WithStack(err)
270270
} else if resolved != src {
271271
retries--
272272
if retries <= 0 {

executor/oci/spec_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ func withGetUserInfoMount() oci.SpecOpts {
5151
}
5252
}
5353

54-
func generateMountOpts(_, _ string) ([]oci.SpecOpts, error) {
54+
func generateMountOpts(_, _ string) []oci.SpecOpts {
5555
return []oci.SpecOpts{
5656
withGetUserInfoMount(),
57-
}, nil
57+
}
5858
}
5959

6060
// generateSecurityOpts may affect mounts, so must be called after generateMountOpts

executor/runcexecutor/executor.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root executor.Mount,
219219
bundle := filepath.Join(w.root, id)
220220

221221
if err := os.Mkdir(bundle, 0o711); err != nil {
222-
return nil, err
222+
return nil, errors.WithStack(err)
223223
}
224224
defer os.RemoveAll(bundle)
225225

@@ -230,23 +230,23 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root executor.Mount,
230230

231231
rootFSPath := filepath.Join(bundle, "rootfs")
232232
if err := idtools.MkdirAllAndChown(rootFSPath, 0o700, identity); err != nil {
233-
return nil, err
233+
return nil, errors.WithStack(err)
234234
}
235235
if err := mount.All(rootMount, rootFSPath); err != nil {
236-
return nil, err
236+
return nil, errors.WithStack(err)
237237
}
238238
defer mount.Unmount(rootFSPath, 0)
239239

240240
defer executor.MountStubsCleaner(context.WithoutCancel(ctx), rootFSPath, mounts, meta.RemoveMountStubsRecursive)()
241241

242242
uid, gid, sgids, err := oci.GetUser(rootFSPath, meta.User)
243243
if err != nil {
244-
return nil, err
244+
return nil, errors.WithStack(err)
245245
}
246246

247247
f, err := os.Create(filepath.Join(bundle, "config.json"))
248248
if err != nil {
249-
return nil, err
249+
return nil, errors.WithStack(err)
250250
}
251251
defer f.Close()
252252

@@ -297,7 +297,7 @@ func (w *runcExecutor) Run(ctx context.Context, id string, root executor.Mount,
297297
}
298298

299299
if err := json.NewEncoder(f).Encode(spec); err != nil {
300-
return nil, err
300+
return nil, errors.WithStack(err)
301301
}
302302

303303
bklog.G(ctx).Debugf("> creating %s %v", id, meta.Args)

0 commit comments

Comments
 (0)