From bab6b8cb4e99966265529ffbdb10c9ff558a2983 Mon Sep 17 00:00:00 2001 From: Juliano Martinez Date: Tue, 5 Dec 2023 19:11:35 +0100 Subject: [PATCH] update runOnce --- cmd/root.go | 2 +- cmd/run.go | 2 +- cmd/runOnce.go | 28 ++++++++++++++++++++++++---- pkg/aclmanager/aclmanager.go | 18 ++++++++++-------- pkg/aclmanager/aclmanager_test.go | 5 ----- 5 files changed, 36 insertions(+), 19 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index beae394..8299082 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -48,7 +48,7 @@ var logger = slog.New( // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "bedel", - Short: "Small utility to sync redis acls with a master instance", + Short: "Small utility to sync redis acls with a primary instance", } // Execute adds all child commands to the root command and sets flags appropriately. diff --git a/cmd/run.go b/cmd/run.go index a456adc..5e248df 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -24,7 +24,7 @@ import ( // runCmd represents the run command var runCmd = &cobra.Command{ Use: "run", - Short: "Run the acl manager in mood loop, it will sync the follower with the master", + Short: "Run the acl manager in mood loop, it will sync the follower with the primary", Run: func(cmd *cobra.Command, args []string) { mgr := aclmanager.New(viper.GetString("address"), viper.GetString("username"), viper.GetString("password")) ctx := cmd.Context() diff --git a/cmd/runOnce.go b/cmd/runOnce.go index 3f133f8..a62ca6f 100644 --- a/cmd/runOnce.go +++ b/cmd/runOnce.go @@ -18,6 +18,8 @@ package cmd import ( "github.com/ncode/bedel/pkg/aclmanager" "github.com/spf13/viper" + "log/slog" + "os" "github.com/spf13/cobra" ) @@ -25,12 +27,30 @@ import ( // runOnceCmd represents the runOnce command var runOnceCmd = &cobra.Command{ Use: "runOnce", - Short: "Run the acl manager once, it will sync the follower with the master", + Short: "Run the acl manager once, it will sync the follower with the primary", Run: func(cmd *cobra.Command, args []string) { - mgr := aclmanager.New(viper.GetString("address"), viper.GetString("username"), viper.GetString("password")) - err := mgr.SyncAcls() + ctx := cmd.Context() + aclManager := aclmanager.New(viper.GetString("address"), viper.GetString("username"), viper.GetString("password")) + function, err := aclManager.CurrentFunction(ctx) if err != nil { - panic(err) + slog.Warn("unable to check if it's a Primary", "message", err) + os.Exit(1) + } + if function == aclmanager.Follower { + primary, err := aclManager.Primary(ctx) + if err != nil { + slog.Warn("unable to find Primary", "message", err) + os.Exit(1) + } + var added, deleted []string + added, deleted, err = aclManager.SyncAcls(ctx, primary) + if err != nil { + slog.Warn("unable to sync acls from Primary", "message", err) + os.Exit(1) + } + slog.Info("Synced acls from Primary", "added", added, "deleted", deleted) + } else { + slog.Info("Not a follower, nothing to do") } }, } diff --git a/pkg/aclmanager/aclmanager.go b/pkg/aclmanager/aclmanager.go index 520f503..2692a61 100644 --- a/pkg/aclmanager/aclmanager.go +++ b/pkg/aclmanager/aclmanager.go @@ -129,18 +129,17 @@ func (a *AclManager) Primary(ctx context.Context) (primary *AclManager, err erro } // SyncAcls connects to master node and syncs the acls to the current node -func (a *AclManager) SyncAcls(ctx context.Context, primary *AclManager) (err error) { +func (a *AclManager) SyncAcls(ctx context.Context, primary *AclManager) (added []string, deleted []string, err error) { slog.Debug("Syncing acls") if primary == nil { - return fmt.Errorf("no primary found") + return added, deleted, fmt.Errorf("no primary found") } - added, deleted, err := mirrorAcls(ctx, primary.RedisClient, a.RedisClient) + added, deleted, err = mirrorAcls(ctx, primary.RedisClient, a.RedisClient) if err != nil { - return fmt.Errorf("error syncing acls: %v", err) + return added, deleted, fmt.Errorf("error syncing acls: %v", err) } - slog.Info("Synced acls", "added", added, "deleted", deleted) - return err + return added, deleted, err } // Close closes the redis client @@ -254,11 +253,14 @@ func (a *AclManager) Loop(ctx context.Context) (err error) { slog.Warn("unable to find Primary", "message", e) continue } - e = a.SyncAcls(ctx, primary) + var added, deleted []string + added, deleted, err = a.SyncAcls(ctx, primary) if err != nil { slog.Warn("unable to sync acls from Primary", "message", e) + err = fmt.Errorf("unable to sync acls from Primary: %w", e) + continue } - err = fmt.Errorf("unable to sync acls from Primary: %w", e) + slog.Info("Synced acls from Primary", "added", added, "deleted", deleted) } } } diff --git a/pkg/aclmanager/aclmanager_test.go b/pkg/aclmanager/aclmanager_test.go index 805e6ae..e88bfba 100644 --- a/pkg/aclmanager/aclmanager_test.go +++ b/pkg/aclmanager/aclmanager_test.go @@ -50,11 +50,6 @@ repl_backlog_first_byte_offset:1 repl_backlog_histlen:434` ) -type NodeInfo struct { - Address string - Function int -} - func TestFindNodes(t *testing.T) { // Sample master and slave output for testing