From cb188d0b26ec63574af176e68eebc71f9cef186a Mon Sep 17 00:00:00 2001 From: marco Date: Fri, 27 Oct 2017 00:27:11 +0100 Subject: [PATCH 1/2] etcdctl v3: adds the --once option to the lease keep-alive command Fixes: #8719 --- etcdctl/ctlv3/command/lease_command.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/etcdctl/ctlv3/command/lease_command.go b/etcdctl/ctlv3/command/lease_command.go index 28c9a0adaaa..3b2452a9af5 100644 --- a/etcdctl/ctlv3/command/lease_command.go +++ b/etcdctl/ctlv3/command/lease_command.go @@ -150,15 +150,21 @@ func leaseListCommandFunc(cmd *cobra.Command, args []string) { display.Leases(*resp) } +var ( + leaseKeepAliveOnce bool +) + // NewLeaseKeepAliveCommand returns the cobra command for "lease keep-alive". func NewLeaseKeepAliveCommand() *cobra.Command { lc := &cobra.Command{ - Use: "keep-alive ", + Use: "keep-alive [options] ", Short: "Keeps leases alive (renew)", Run: leaseKeepAliveCommandFunc, } + lc.Flags().BoolVar(&leaseKeepAliveOnce, "once", false, "Resets the keep-alive time to its original value and exits immediately") + return lc } @@ -169,11 +175,20 @@ func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) { } id := leaseFromArgs(args[0]) + + if leaseKeepAliveOnce { + respc, kerr := mustClientFromCmd(cmd).KeepAliveOnce(context.TODO(), id) + if kerr != nil { + ExitWithError(ExitBadConnection, kerr) + } + display.KeepAlive(*respc) + return + } + respc, kerr := mustClientFromCmd(cmd).KeepAlive(context.TODO(), id) if kerr != nil { ExitWithError(ExitBadConnection, kerr) } - for resp := range respc { display.KeepAlive(*resp) } From aaf4a70cd0e8f043a79b9fd307e2401965ddaea6 Mon Sep 17 00:00:00 2001 From: marco Date: Fri, 27 Oct 2017 08:48:22 +0100 Subject: [PATCH 2/2] etcdctl v3: e2e test for the --once option to the lease keep-alive command Follow up #8775 --- e2e/ctl_v3_lease_test.go | 32 ++++++++++++++++++++++++++ etcdctl/ctlv3/command/lease_command.go | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/e2e/ctl_v3_lease_test.go b/e2e/ctl_v3_lease_test.go index 34571ac9f17..4c11d0707d5 100644 --- a/e2e/ctl_v3_lease_test.go +++ b/e2e/ctl_v3_lease_test.go @@ -24,6 +24,7 @@ import ( func TestCtlV3LeaseGrantTimeToLive(t *testing.T) { testCtl(t, leaseTestGrantTimeToLive) } func TestCtlV3LeaseGrantLeases(t *testing.T) { testCtl(t, leaseTestGrantLeasesList) } func TestCtlV3LeaseKeepAlive(t *testing.T) { testCtl(t, leaseTestKeepAlive) } +func TestCtlV3LeaseKeepAliveOnce(t *testing.T) { testCtl(t, leaseTestKeepAliveOnce) } func TestCtlV3LeaseRevoke(t *testing.T) { testCtl(t, leaseTestRevoke) } func leaseTestGrantTimeToLive(cx ctlCtx) { @@ -89,6 +90,23 @@ func leaseTestKeepAlive(cx ctlCtx) { } } +func leaseTestKeepAliveOnce(cx ctlCtx) { + // put with TTL 10 seconds and keep-alive once + leaseID, err := ctlV3LeaseGrant(cx, 10) + if err != nil { + cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseGrant error (%v)", err) + } + if err := ctlV3Put(cx, "key", "val", leaseID); err != nil { + cx.t.Fatalf("leaseTestKeepAlive: ctlV3Put error (%v)", err) + } + if err := ctlV3LeaseKeepAliveOnce(cx, leaseID); err != nil { + cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseKeepAliveOnce error (%v)", err) + } + if err := ctlV3Get(cx, []string{"key"}, kv{"key", "val"}); err != nil { + cx.t.Fatalf("leaseTestKeepAlive: ctlV3Get error (%v)", err) + } +} + func leaseTestRevoke(cx ctlCtx) { // put with TTL 10 seconds and revoke leaseID, err := ctlV3LeaseGrant(cx, 10) @@ -143,6 +161,20 @@ func ctlV3LeaseKeepAlive(cx ctlCtx, leaseID string) error { return proc.Stop() } +func ctlV3LeaseKeepAliveOnce(cx ctlCtx, leaseID string) error { + cmdArgs := append(cx.PrefixArgs(), "lease", "keep-alive", "--once", leaseID) + + proc, err := spawnCmd(cmdArgs) + if err != nil { + return err + } + + if _, err = proc.Expect(fmt.Sprintf("lease %s keepalived with TTL(", leaseID)); err != nil { + return err + } + return proc.Stop() +} + func ctlV3LeaseRevoke(cx ctlCtx, leaseID string) error { cmdArgs := append(cx.PrefixArgs(), "lease", "revoke", leaseID) return spawnWithExpect(cmdArgs, fmt.Sprintf("lease %s revoked", leaseID)) diff --git a/etcdctl/ctlv3/command/lease_command.go b/etcdctl/ctlv3/command/lease_command.go index 3b2452a9af5..ecfe3a6fb63 100644 --- a/etcdctl/ctlv3/command/lease_command.go +++ b/etcdctl/ctlv3/command/lease_command.go @@ -175,7 +175,7 @@ func leaseKeepAliveCommandFunc(cmd *cobra.Command, args []string) { } id := leaseFromArgs(args[0]) - + if leaseKeepAliveOnce { respc, kerr := mustClientFromCmd(cmd).KeepAliveOnce(context.TODO(), id) if kerr != nil {