Skip to content

Commit

Permalink
Merge pull request #1367 from fformica/master
Browse files Browse the repository at this point in the history
1197: crictl rm to also remove container log files
  • Loading branch information
k8s-ci-robot authored Feb 28, 2024
2 parents b28e069 + 949545e commit 17b4dd6
Showing 1 changed file with 23 additions and 0 deletions.
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

0 comments on commit 17b4dd6

Please sign in to comment.