@@ -37,6 +37,7 @@ import (
3737 "github.com/containerd/containerd/metadata"
3838 "github.com/containerd/containerd/mount"
3939 "github.com/containerd/containerd/namespaces"
40+ "github.com/containerd/containerd/pkg/cleanup"
4041 "github.com/containerd/containerd/pkg/process"
4142 "github.com/containerd/containerd/platforms"
4243 "github.com/containerd/containerd/plugin"
@@ -165,6 +166,10 @@ func (r *Runtime) Create(ctx context.Context, id string, opts runtime.CreateOpts
165166 if err != nil {
166167 return nil , err
167168 }
169+ ctx = log .WithLogger (ctx , log .G (ctx ).WithError (err ).WithFields (logrus.Fields {
170+ "id" : id ,
171+ "namespace" : namespace ,
172+ }))
168173
169174 if err := identifiers .Validate (id ); err != nil {
170175 return nil , fmt .Errorf ("invalid task id: %w" , err )
@@ -206,11 +211,8 @@ func (r *Runtime) Create(ctx context.Context, id string, opts runtime.CreateOpts
206211 return
207212 }
208213
209- if err = r .cleanupAfterDeadShim (context .Background (), bundle , namespace , id ); err != nil {
210- log .G (ctx ).WithError (err ).WithFields (logrus.Fields {
211- "id" : id ,
212- "namespace" : namespace ,
213- }).Warn ("failed to clean up after killed shim" )
214+ if err = r .cleanupAfterDeadShim (cleanup .Background (ctx ), bundle , namespace , id ); err != nil {
215+ log .G (ctx ).WithError (err ).Warn ("failed to clean up after killed shim" )
214216 }
215217 }
216218 shimopt = ShimRemote (r .config , r .address , cgroup , exitHandler )
@@ -222,8 +224,7 @@ func (r *Runtime) Create(ctx context.Context, id string, opts runtime.CreateOpts
222224 }
223225 defer func () {
224226 if err != nil {
225- deferCtx , deferCancel := context .WithTimeout (
226- namespaces .WithNamespace (context .TODO (), namespace ), cleanupTimeout )
227+ deferCtx , deferCancel := context .WithTimeout (cleanup .Background (ctx ), cleanupTimeout )
227228 defer deferCancel ()
228229 if kerr := s .KillShim (deferCtx ); kerr != nil {
229230 log .G (ctx ).WithError (kerr ).Error ("failed to kill shim" )
@@ -359,6 +360,11 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
359360 filepath .Join (r .root , ns , id ),
360361 )
361362 ctx = namespaces .WithNamespace (ctx , ns )
363+ ctx = log .WithLogger (ctx , log .G (ctx ).WithError (err ).WithFields (logrus.Fields {
364+ "id" : id ,
365+ "namespace" : ns ,
366+ }))
367+
362368 pid , _ := runc .ReadPidFile (filepath .Join (bundle .path , process .InitPidFile ))
363369 shimExit := make (chan struct {})
364370 s , err := bundle .NewShimClient (ctx , ns , ShimConnect (r .config , func () {
@@ -374,10 +380,7 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
374380 }
375381 }), nil )
376382 if err != nil {
377- log .G (ctx ).WithError (err ).WithFields (logrus.Fields {
378- "id" : id ,
379- "namespace" : ns ,
380- }).Error ("connecting to shim" )
383+ log .G (ctx ).WithError (err ).Error ("connecting to shim" )
381384 err := r .cleanupAfterDeadShim (ctx , bundle , ns , id )
382385 if err != nil {
383386 log .G (ctx ).WithError (err ).WithField ("bundle" , bundle .path ).
@@ -402,11 +405,8 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
402405 }
403406 shimStdoutLog , err := v1 .OpenShimStdoutLog (ctx , logDirPath )
404407 if err != nil {
405- log .G (ctx ).WithError (err ).WithFields (logrus.Fields {
406- "id" : id ,
407- "namespace" : ns ,
408- "logDirPath" : logDirPath ,
409- }).Error ("opening shim stdout log pipe" )
408+ log .G (ctx ).WithError (err ).WithField ("logDirPath" , logDirPath ).
409+ Error ("opening shim stdout log pipe" )
410410 continue
411411 }
412412 if r .config .ShimDebug {
@@ -417,11 +417,8 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
417417
418418 shimStderrLog , err := v1 .OpenShimStderrLog (ctx , logDirPath )
419419 if err != nil {
420- log .G (ctx ).WithError (err ).WithFields (logrus.Fields {
421- "id" : id ,
422- "namespace" : ns ,
423- "logDirPath" : logDirPath ,
424- }).Error ("opening shim stderr log pipe" )
420+ log .G (ctx ).WithError (err ).WithField ("logDirPath" , logDirPath ).
421+ Error ("opening shim stderr log pipe" )
425422 continue
426423 }
427424 if r .config .ShimDebug {
@@ -441,13 +438,9 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
441438}
442439
443440func (r * Runtime ) cleanupAfterDeadShim (ctx context.Context , bundle * bundle , ns , id string ) error {
444- log .G (ctx ).WithFields (logrus.Fields {
445- "id" : id ,
446- "namespace" : ns ,
447- }).Warn ("cleaning up after shim dead" )
441+ log .G (ctx ).Warn ("cleaning up after shim dead" )
448442
449443 pid , _ := runc .ReadPidFile (filepath .Join (bundle .path , process .InitPidFile ))
450- ctx = namespaces .WithNamespace (ctx , ns )
451444 if err := r .terminate (ctx , bundle , ns , id ); err != nil {
452445 if r .config .ShimDebug {
453446 return fmt .Errorf ("failed to terminate task, leaving bundle for debugging: %w" , err )
0 commit comments