Skip to content

Commit 60d4e21

Browse files
Merge pull request #9451 from littlejawa/backport_logrotate_fix_1.32
[release-1.32] Backport kata logrotate fix
2 parents 6140265 + bca2999 commit 60d4e21

File tree

2 files changed

+79
-14
lines changed

2 files changed

+79
-14
lines changed

internal/oci/runtime_vm.go

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/logs.bats

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,32 @@ function teardown() {
5151
output=$(crictl inspect "$ctr_id" | jq -r ".status.state")
5252
[[ "$output" == "CONTAINER_RUNNING" ]]
5353
}
54+
55+
@test "Log file rotation should work" {
56+
start_crio
57+
58+
jq '.metadata.name = "logger"
59+
| .command = ["/bin/sh", "-c", "while true; do echo hello; sleep 1; done"]' \
60+
"$TESTDATA"/container_config.json > "$TESTDIR"/logger.json
61+
62+
ctr_id=$(crictl run "$TESTDIR"/logger.json "$TESTDATA"/sandbox_config.json)
63+
# Especially when using kata, it sometimes takes a few seconds to actually run container
64+
sleep 5
65+
66+
logpath=$(crictl inspect "$ctr_id" | jq -r ".status.logPath")
67+
[[ -f "$logpath" ]]
68+
69+
# Move log file away, then ask for re-open.
70+
# It will fail if the new log file is not created
71+
mv "$logpath" "$logpath".rotated
72+
crictl logs -r "$ctr_id"
73+
74+
[[ -f "$logpath" ]]
75+
76+
# Verify that the rotated log file is not written to anymore
77+
initial_size=$(stat -c %s "$logpath.rotated")
78+
[ "$initial_size" -gt 0 ]
79+
sleep 2 # our logger writes every second, leave enough time for at least one write
80+
new_size=$(stat -c %s "$logpath.rotated")
81+
[ "$new_size" -eq "$initial_size" ]
82+
}

0 commit comments

Comments
 (0)