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 all 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
23 changes: 23 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{"k"},
Usage: "Preserve the container log file and its rotations",
},
&cli.BoolFlag{
Name: "all",
Aliases: []string{"a"},
Expand Down Expand Up @@ -385,6 +392,22 @@ 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 + ".*")
if err != nil {
logRotations = []string{}
}
logRotations = append(logRotations, 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