Skip to content

Commit 95058eb

Browse files
committed
etcdctl/ctlv3: add 'require-leader' flag
Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>
1 parent ed92420 commit 95058eb

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

etcdctl/ctlv3/command/global.go

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type GlobalFlags struct {
4141
InsecureSkipVerify bool
4242
InsecureDiscovery bool
4343
Endpoints []string
44+
RequireLeader bool
4445
DialTimeout time.Duration
4546
CommandTimeOut time.Duration
4647
KeepAliveTime time.Duration
@@ -382,3 +383,11 @@ func endpointsFromFlagValue(cmd *cobra.Command) ([]string, error) {
382383
}
383384
return ret, err
384385
}
386+
387+
func requireLeaderFromCmd(cmd *cobra.Command) bool {
388+
req, err := cmd.Flags().GetString("require-leader")
389+
if err != nil {
390+
ExitWithError(ExitBadArgs, err)
391+
}
392+
return req
393+
}

etcdctl/ctlv3/command/watch_command.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func watchCommandFunc(cmd *cobra.Command, args []string) {
5757
}
5858

5959
c := mustClientFromCmd(cmd)
60-
wc, err := getWatchChan(c, args)
60+
reqLeader := requireLeaderFromCmd(cmd)
61+
wc, err := getWatchChan(c, reqLeader, args)
6162
if err != nil {
6263
ExitWithError(ExitBadArgs, err)
6364
}
@@ -71,6 +72,7 @@ func watchCommandFunc(cmd *cobra.Command, args []string) {
7172

7273
func watchInteractiveFunc(cmd *cobra.Command, args []string) {
7374
c := mustClientFromCmd(cmd)
75+
reqLeader := requireLeaderFromCmd(cmd)
7476

7577
reader := bufio.NewReader(os.Stdin)
7678

@@ -98,7 +100,7 @@ func watchInteractiveFunc(cmd *cobra.Command, args []string) {
98100
fmt.Fprintf(os.Stderr, "Invalid command %s (%v)\n", l, err)
99101
continue
100102
}
101-
ch, err := getWatchChan(c, flagset.Args())
103+
ch, err := getWatchChan(c, reqLeader, flagset.Args())
102104
if err != nil {
103105
fmt.Fprintf(os.Stderr, "Invalid command %s (%v)\n", l, err)
104106
continue
@@ -107,7 +109,7 @@ func watchInteractiveFunc(cmd *cobra.Command, args []string) {
107109
}
108110
}
109111

110-
func getWatchChan(c *clientv3.Client, args []string) (clientv3.WatchChan, error) {
112+
func getWatchChan(c *clientv3.Client, requireLeader bool, args []string) (clientv3.WatchChan, error) {
111113
if len(args) < 1 || len(args) > 2 {
112114
return nil, fmt.Errorf("bad number of arguments")
113115
}
@@ -125,7 +127,11 @@ func getWatchChan(c *clientv3.Client, args []string) (clientv3.WatchChan, error)
125127
if watchPrevKey {
126128
opts = append(opts, clientv3.WithPrevKV())
127129
}
128-
return c.Watch(context.TODO(), key, opts...), nil
130+
ctx := context.Background()
131+
if requireLeader {
132+
ctx = clientv3.WithRequireLeader(ctx)
133+
}
134+
return c.Watch(ctx, key, opts...), nil
129135
}
130136

131137
func printWatchCh(ch clientv3.WatchChan) {

etcdctl/ctlv3/ctl.go

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func init() {
6060
rootCmd.PersistentFlags().BoolVar(&globalFlags.Insecure, "insecure-transport", true, "disable transport security for client connections")
6161
rootCmd.PersistentFlags().BoolVar(&globalFlags.InsecureDiscovery, "insecure-discovery", true, "accept insecure SRV records describing cluster endpoints")
6262
rootCmd.PersistentFlags().BoolVar(&globalFlags.InsecureSkipVerify, "insecure-skip-tls-verify", false, "skip server certificate verification")
63+
rootCmd.PersistentFlags().BoolVar(&globalFlags.RequireLeader, "require-leader", false, "require client requests to only succeed when the cluster has a leader (only available for watch)")
6364
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CertFile, "cert", "", "identify secure client using this TLS certificate file")
6465
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.KeyFile, "key", "", "identify secure client using this TLS key file")
6566
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CAFile, "cacert", "", "verify certificates of TLS-enabled secure servers using this CA bundle")

0 commit comments

Comments
 (0)