diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index 78216f8a11..5d5de50f7d 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -22,6 +22,8 @@ import ( "errors" "fmt" "log" + "os" + "path/filepath" goruntime "runtime" "sort" "strings" @@ -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"}, @@ -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) + } + } + } } }