Skip to content

Commit 7b5b07c

Browse files
committed
runtime_vm: Implement the ReopenContainerLog function
This commit implements the ReopenContainerLog function. This fixes an issue where kata container logs could not be rotated. This required duplicating part of the createContainerIO function, so I am moving this in its own subfunction for reuse. Signed-off-by: Julien Ropé <[email protected]>
1 parent 60c6d8d commit 7b5b07c

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

internal/oci/runtime_vm.go

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -942,16 +942,35 @@ func (r *runtimeVM) createContainerIO(ctx context.Context, c *Container, cioOpts
942942
}
943943
}()
944944

945-
f, err := os.OpenFile(c.LogPath(), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o600)
945+
stdout, stderr, err := r.createContainerLoggers(ctx, c.LogPath())
946946
if err != nil {
947947
return nil, err
948948
}
949949

950+
containerIO.AddOutput(c.LogPath(), stdout, stderr)
951+
containerIO.Pipe()
952+
953+
r.Lock()
954+
r.ctrs[c.ID()] = containerInfo{
955+
cio: containerIO,
956+
}
957+
r.Unlock()
958+
959+
return containerIO, nil
960+
}
961+
962+
// createContainerLoggers creates container loggers and return write closer for stdout and stderr.
963+
func (r *runtimeVM) createContainerLoggers(ctx context.Context, logPath string) (stdout, stderr io.WriteCloser, err error) {
964+
f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o600)
965+
if err != nil {
966+
return nil, nil, err
967+
}
968+
950969
var stdoutCh, stderrCh <-chan struct{}
951970

952971
wc := cioutil.NewSerialWriteCloser(f)
953-
stdout, stdoutCh := cio.NewCRILogger(c.LogPath(), wc, cio.Stdout, -1)
954-
stderr, stderrCh := cio.NewCRILogger(c.LogPath(), wc, cio.Stderr, -1)
972+
stdout, stdoutCh = cio.NewCRILogger(logPath, wc, cio.Stdout, -1)
973+
stderr, stderrCh = cio.NewCRILogger(logPath, wc, cio.Stderr, -1)
955974

956975
go func() {
957976
if stdoutCh != nil {
@@ -962,20 +981,11 @@ func (r *runtimeVM) createContainerIO(ctx context.Context, c *Container, cioOpts
962981
<-stderrCh
963982
}
964983

965-
log.Debugf(ctx, "Finish redirecting log file %q, closing it", c.LogPath())
984+
log.Debugf(ctx, "Finish redirecting log file %q, closing it", logPath)
966985
f.Close()
967986
}()
968987

969-
containerIO.AddOutput(c.LogPath(), stdout, stderr)
970-
containerIO.Pipe()
971-
972-
r.Lock()
973-
r.ctrs[c.ID()] = containerInfo{
974-
cio: containerIO,
975-
}
976-
r.Unlock()
977-
978-
return containerIO, nil
988+
return stdout, stderr, nil
979989
}
980990

981991
// PauseContainer pauses a container.
@@ -1228,6 +1238,29 @@ func (r *runtimeVM) ReopenContainerLog(ctx context.Context, c *Container) error
12281238
log.Debugf(ctx, "RuntimeVM.ReopenContainerLog() start")
12291239
defer log.Debugf(ctx, "RuntimeVM.ReopenContainerLog() end")
12301240

1241+
r.Lock()
1242+
cInfo, ok := r.ctrs[c.ID()]
1243+
r.Unlock()
1244+
1245+
if !ok {
1246+
return errors.New("could not retrieve container information")
1247+
}
1248+
1249+
// Create new container logger and replace the existing ones.
1250+
stdoutWC, stderrWC, err := r.createContainerLoggers(ctx, c.LogPath())
1251+
if err != nil {
1252+
return err
1253+
}
1254+
1255+
oldStdoutWC, oldStderrWC := cInfo.cio.AddOutput(c.LogPath(), stdoutWC, stderrWC)
1256+
if oldStdoutWC != nil {
1257+
oldStdoutWC.Close()
1258+
}
1259+
1260+
if oldStderrWC != nil {
1261+
oldStderrWC.Close()
1262+
}
1263+
12311264
return nil
12321265
}
12331266

0 commit comments

Comments
 (0)