From 67679ee3e0ccd1469c05b58631f91f49ddb91bc3 Mon Sep 17 00:00:00 2001 From: Ferdinando Formica Date: Mon, 26 Feb 2024 14:13:28 +0000 Subject: [PATCH 1/5] 1197: adding rm --dl (--deletelog) option Signed-off-by: Ferdinando Formica --- cmd/crictl/container.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index 78216f8a11..8764fa054f 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: "deletelog", + Aliases: []string{"dl"}, + Usage: "Delete the container log file and its rotations as well", + }, &cli.BoolFlag{ Name: "all", Aliases: []string{"a"}, @@ -385,6 +392,21 @@ var removeContainerCommand = &cli.Command{ logrus.Errorf("removing container %q failed: %v", id, err) errored = true continue + } else if ctx.Bool("deletelog") { + logFileName := resp.GetStatus().GetLogPath() + if logFileName != "" { + logRotations, err := filepath.Glob(logFileName + "*") + if err != nil { + logRotations = []string{logFileName} + } + + 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) + } + } + } } } From 64e373b292f3625773bcd5bd02cb3067423a32b7 Mon Sep 17 00:00:00 2001 From: Ferdinando Formica Date: Tue, 27 Feb 2024 09:56:07 +0000 Subject: [PATCH 2/5] Changing option to keep-logs and inverting default Signed-off-by: Ferdinando Formica --- cmd/crictl/container.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index 8764fa054f..799d98456d 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -329,9 +329,9 @@ var removeContainerCommand = &cli.Command{ Usage: "Force removal of the container, disregarding if running", }, &cli.BoolFlag{ - Name: "deletelog", - Aliases: []string{"dl"}, - Usage: "Delete the container log file and its rotations as well", + Name: "keep-logs", + Aliases: []string{"kl"}, + Usage: "Preserve the container log file and its rotations", }, &cli.BoolFlag{ Name: "all", @@ -392,12 +392,12 @@ var removeContainerCommand = &cli.Command{ logrus.Errorf("removing container %q failed: %v", id, err) errored = true continue - } else if ctx.Bool("deletelog") { - logFileName := resp.GetStatus().GetLogPath() - if logFileName != "" { - logRotations, err := filepath.Glob(logFileName + "*") + } else if !ctx.Bool("keep-logs") { + logPath := resp.GetStatus().GetLogPath() + if logPath != "" { + logRotations, err := filepath.Glob(logPath + "*") if err != nil { - logRotations = []string{logFileName} + logRotations = []string{logPath} } for _, logFile := range logRotations { From 09b9303e620a2667dfd56f5f04e8ed03fd7e7e13 Mon Sep 17 00:00:00 2001 From: Ferdinando Formica Date: Tue, 27 Feb 2024 10:01:20 +0000 Subject: [PATCH 3/5] Applying comment Changing alias to just k Co-authored-by: Sascha Grunert --- cmd/crictl/container.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index 799d98456d..70ce395408 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -330,7 +330,7 @@ var removeContainerCommand = &cli.Command{ }, &cli.BoolFlag{ Name: "keep-logs", - Aliases: []string{"kl"}, + Aliases: []string{"k"}, Usage: "Preserve the container log file and its rotations", }, &cli.BoolFlag{ From 5afb4c73fe97b203c8ffa071ad791359d963ac07 Mon Sep 17 00:00:00 2001 From: Ferdinando Formica Date: Tue, 27 Feb 2024 14:27:12 +0000 Subject: [PATCH 4/5] Matching kubelet's containerLogManager.rotateLog() glob pattern Signed-off-by: Ferdinando Formica --- cmd/crictl/container.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index 70ce395408..3a95151ce8 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -395,7 +395,7 @@ var removeContainerCommand = &cli.Command{ } else if !ctx.Bool("keep-logs") { logPath := resp.GetStatus().GetLogPath() if logPath != "" { - logRotations, err := filepath.Glob(logPath + "*") + logRotations, err := filepath.Glob(logPath + ".*") if err != nil { logRotations = []string{logPath} } From 949545e15dc101642204a1cb4a9fba24833e5c94 Mon Sep 17 00:00:00 2001 From: Ferdinando Formica Date: Tue, 27 Feb 2024 15:36:48 +0000 Subject: [PATCH 5/5] Readd the logPath to the list Signed-off-by: Ferdinando Formica --- cmd/crictl/container.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index 3a95151ce8..5d5de50f7d 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -397,8 +397,9 @@ var removeContainerCommand = &cli.Command{ if logPath != "" { logRotations, err := filepath.Glob(logPath + ".*") if err != nil { - logRotations = []string{logPath} + logRotations = []string{} } + logRotations = append(logRotations, logPath) for _, logFile := range logRotations { err = os.Remove(logFile)