Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1197: crictl rm to also remove container log files #1367

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
goruntime "runtime"
"sort"
"strings"
Expand Down Expand Up @@ -326,6 +328,11 @@ var removeContainerCommand = &cli.Command{
Aliases: []string{"f"},
Usage: "Force removal of the container, disregarding if running",
},
&cli.BoolFlag{
Name: "keep-logs",
Aliases: []string{"kl"},
Usage: "Preserve the container log file and its rotations",
},
&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Expand Down Expand Up @@ -385,6 +392,21 @@ var removeContainerCommand = &cli.Command{
logrus.Errorf("removing container %q failed: %v", id, err)
errored = true
continue
} else if !ctx.Bool("keep-logs") {
logPath := resp.GetStatus().GetLogPath()
if logPath != "" {
logRotations, err := filepath.Glob(logPath + "*")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this save for the log rotation? We would also possible remove files which are not rotated logs but other files, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible, although the rotations attach to the file name so it's unlikely, e.g. this was from my test with cri-o:

-rw------- 1 root root 5.3M Feb 26 05:49 0.log
-rw-r--r-- 1 root root 1.2M Feb 26 05:44 0.log.20240226-053352.gz
-rw------- 1 root root  11M Feb 26 05:44 0.log.20240226-054404

Another container would be e.g. 1.log, but a user could have backed up a 0.log.bak, and this code would delete that too. I wasn't sure if the log rotation always follows this format though.
Reading https://kubernetes.io/docs/concepts/cluster-administration/logging/ the rotation in /var/log should only be performed by kubelet, so I could use that as a guide, removing files in this format in that directory and ignoring rotations outside it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like kubelet uses a very similar pattern: https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/logs/container_log_manager.go#L312
For the time being I'll do the same and just add the missing dot; I'm averse to adding hardcoded references to the dir, especially because it seems a bit more variable under Windows (the doc mentions C:\var\logs and C:\var\log\pods).

if err != nil {
logRotations = []string{logPath}
}

for _, logFile := range logRotations {
err = os.Remove(logFile)
if err != nil {
logrus.Errorf("removing log file %s for container %q failed: %v", logFile, id, err)
}
}
}
}
}

Expand Down
Loading