Skip to content

Commit 17b4dd6

Browse files
authored
Merge pull request #1367 from fformica/master
1197: crictl rm to also remove container log files
2 parents b28e069 + 949545e commit 17b4dd6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

cmd/crictl/container.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"errors"
2323
"fmt"
2424
"log"
25+
"os"
26+
"path/filepath"
2527
goruntime "runtime"
2628
"sort"
2729
"strings"
@@ -326,6 +328,11 @@ var removeContainerCommand = &cli.Command{
326328
Aliases: []string{"f"},
327329
Usage: "Force removal of the container, disregarding if running",
328330
},
331+
&cli.BoolFlag{
332+
Name: "keep-logs",
333+
Aliases: []string{"k"},
334+
Usage: "Preserve the container log file and its rotations",
335+
},
329336
&cli.BoolFlag{
330337
Name: "all",
331338
Aliases: []string{"a"},
@@ -385,6 +392,22 @@ var removeContainerCommand = &cli.Command{
385392
logrus.Errorf("removing container %q failed: %v", id, err)
386393
errored = true
387394
continue
395+
} else if !ctx.Bool("keep-logs") {
396+
logPath := resp.GetStatus().GetLogPath()
397+
if logPath != "" {
398+
logRotations, err := filepath.Glob(logPath + ".*")
399+
if err != nil {
400+
logRotations = []string{}
401+
}
402+
logRotations = append(logRotations, logPath)
403+
404+
for _, logFile := range logRotations {
405+
err = os.Remove(logFile)
406+
if err != nil {
407+
logrus.Errorf("removing log file %s for container %q failed: %v", logFile, id, err)
408+
}
409+
}
410+
}
388411
}
389412
}
390413

0 commit comments

Comments
 (0)