From 8930ea52d5962423d908f39357680924d44bdba0 Mon Sep 17 00:00:00 2001 From: pchaseh Date: Thu, 29 Aug 2024 13:33:11 +0000 Subject: [PATCH 1/2] Add a dry run mode --- config.go | 2 ++ main.go | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 9eec59b..1989c0a 100644 --- a/config.go +++ b/config.go @@ -38,6 +38,8 @@ type globalConfig struct { defragRule string printVersion bool + + dryRun bool } func clientConfigWithoutEndpoints(gcfg globalConfig) *clientv3.ConfigSpec { diff --git a/main.go b/main.go index 2b2d9ce..cdece6d 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,8 @@ func newDefragCommand() *cobra.Command { defragCmd.Flags().StringVar(&globalCfg.defragRule, "defrag-rule", "", "defragmentation rule (etcd-defrag will run defragmentation if the rule is empty or it is evaluated to true)") defragCmd.Flags().BoolVar(&globalCfg.printVersion, "version", false, "print the version and exit") + + defragCmd.Flags().BoolVar(&globalCfg.dryRun, "dry-run", false, "evaluate whether or not endpoints require defragmentation, but don't actually perform it") return defragCmd } @@ -79,6 +81,10 @@ func printVersion(printVersion bool) { func defragCommandFunc(cmd *cobra.Command, args []string) { printVersion(globalCfg.printVersion) + if globalCfg.dryRun { + fmt.Println("Using dry run mode, will not perform defragmentation") + } + fmt.Println("Validating configuration.") if err := validateConfig(cmd, globalCfg); err != nil { fmt.Fprintf(os.Stderr, "Validating configuration failed: %v\n", err) @@ -103,7 +109,7 @@ func defragCommandFunc(cmd *cobra.Command, args []string) { os.Exit(1) } - if globalCfg.compaction { + if globalCfg.compaction && !globalCfg.dryRun { fmt.Printf("Running compaction until revision: %d ... ", statusList[0].Resp.Header.Revision) if err := compact(globalCfg, statusList[0].Resp.Header.Revision, eps[0]); err != nil { fmt.Printf("failed, %v\n", err) @@ -142,6 +148,10 @@ func defragCommandFunc(cmd *cobra.Command, args []string) { continue } + if globalCfg.dryRun { + continue + } + fmt.Printf("Defragmenting endpoint %q\n", ep) startTs := time.Now() err = defragment(globalCfg, ep) From 788f04df17ed790076941268821911c8b2e075f8 Mon Sep 17 00:00:00 2001 From: Chase <70911039+pchaseh@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:16:30 -0400 Subject: [PATCH 2/2] Log when skipping endpoint due to dry run mode Co-authored-by: Benjamin Wang --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index cdece6d..4b027d9 100644 --- a/main.go +++ b/main.go @@ -149,6 +149,7 @@ func defragCommandFunc(cmd *cobra.Command, args []string) { } if globalCfg.dryRun { + fmt.Printf("[Dry run] skip defragmenting endpoint %q\n", ep) continue }