@@ -897,37 +897,50 @@ func (r *runtimeVM) createContainerIO(ctx context.Context, c *Container, cioOpts
897897 }
898898 }()
899899
900- f , err := os . OpenFile ( c .LogPath (), os . O_WRONLY | os . O_APPEND | os . O_CREATE , 0o600 )
900+ stdout , stderr , err := r . createContainerLoggers ( ctx , c .LogPath ())
901901 if err != nil {
902902 return nil , err
903903 }
904904
905+ containerIO .AddOutput (c .LogPath (), stdout , stderr )
906+ containerIO .Pipe ()
907+
908+ r .Lock ()
909+ r .ctrs [c .ID ()] = containerInfo {
910+ cio : containerIO ,
911+ }
912+ r .Unlock ()
913+
914+ return containerIO , nil
915+ }
916+
917+ // createContainerLoggers creates container loggers and return write closer for stdout and stderr.
918+ func (r * runtimeVM ) createContainerLoggers (ctx context.Context , logPath string ) (stdout , stderr io.WriteCloser , err error ) {
919+ f , err := os .OpenFile (logPath , os .O_WRONLY | os .O_APPEND | os .O_CREATE , 0o600 )
920+ if err != nil {
921+ return nil , nil , err
922+ }
923+
905924 var stdoutCh , stderrCh <- chan struct {}
925+
906926 wc := cioutil .NewSerialWriteCloser (f )
907- stdout , stdoutCh : = cio .NewCRILogger (c . LogPath () , wc , cio .Stdout , - 1 )
908- stderr , stderrCh : = cio .NewCRILogger (c . LogPath () , wc , cio .Stderr , - 1 )
927+ stdout , stdoutCh = cio .NewCRILogger (logPath , wc , cio .Stdout , - 1 )
928+ stderr , stderrCh = cio .NewCRILogger (logPath , wc , cio .Stderr , - 1 )
909929
910930 go func () {
911931 if stdoutCh != nil {
912932 <- stdoutCh
913933 }
934+
914935 if stderrCh != nil {
915936 <- stderrCh
916937 }
917- log .Debugf (ctx , "Finish redirecting log file %q, closing it" , c .LogPath ())
938+
939+ log .Debugf (ctx , "Finish redirecting log file %q, closing it" , logPath )
918940 f .Close ()
919941 }()
920942
921- containerIO .AddOutput (c .LogPath (), stdout , stderr )
922- containerIO .Pipe ()
923-
924- r .Lock ()
925- r .ctrs [c .ID ()] = containerInfo {
926- cio : containerIO ,
927- }
928- r .Unlock ()
929-
930- return containerIO , nil
943+ return stdout , stderr , nil
931944}
932945
933946// PauseContainer pauses a container.
@@ -1170,6 +1183,29 @@ func (r *runtimeVM) ReopenContainerLog(ctx context.Context, c *Container) error
11701183 log .Debugf (ctx , "RuntimeVM.ReopenContainerLog() start" )
11711184 defer log .Debugf (ctx , "RuntimeVM.ReopenContainerLog() end" )
11721185
1186+ r .Lock ()
1187+ cInfo , ok := r .ctrs [c .ID ()]
1188+ r .Unlock ()
1189+
1190+ if ! ok {
1191+ return errors .New ("could not retrieve container information" )
1192+ }
1193+
1194+ // Create new container logger and replace the existing ones.
1195+ stdoutWC , stderrWC , err := r .createContainerLoggers (ctx , c .LogPath ())
1196+ if err != nil {
1197+ return err
1198+ }
1199+
1200+ oldStdoutWC , oldStderrWC := cInfo .cio .AddOutput (c .LogPath (), stdoutWC , stderrWC )
1201+ if oldStdoutWC != nil {
1202+ oldStdoutWC .Close ()
1203+ }
1204+
1205+ if oldStderrWC != nil {
1206+ oldStderrWC .Close ()
1207+ }
1208+
11731209 return nil
11741210}
11751211
0 commit comments