Skip to content

Commit

Permalink
Merge pull request #53 from pchaseh/feat/dry-run-mode
Browse files Browse the repository at this point in the history
feat: Add a dry run mode
  • Loading branch information
ahrtr authored Aug 29, 2024
2 parents 91036c7 + 788f04d commit f1a5f2d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type globalConfig struct {
defragRule string

printVersion bool

dryRun bool
}

func clientConfigWithoutEndpoints(gcfg globalConfig) *clientv3.ConfigSpec {
Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -142,6 +148,11 @@ func defragCommandFunc(cmd *cobra.Command, args []string) {
continue
}

if globalCfg.dryRun {
fmt.Printf("[Dry run] skip defragmenting endpoint %q\n", ep)
continue
}

fmt.Printf("Defragmenting endpoint %q\n", ep)
startTs := time.Now()
err = defragment(globalCfg, ep)
Expand Down

0 comments on commit f1a5f2d

Please sign in to comment.