From 1f20d5d9241817e938fcd8f964b1dc3ec1fe720f Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Mon, 14 Aug 2017 12:17:38 -0700 Subject: [PATCH] etcdctl/ctlv3: add 'lease list' command Signed-off-by: Gyu-Ho Lee --- etcdctl/README.md | 26 ++++++++++++++++++++++--- etcdctl/ctlv3/command/lease_command.go | 20 +++++++++++++++++++ etcdctl/ctlv3/command/printer.go | 2 ++ etcdctl/ctlv3/command/printer_fields.go | 7 +++++++ etcdctl/ctlv3/command/printer_simple.go | 7 +++++++ 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/etcdctl/README.md b/etcdctl/README.md index c6525887916..4c9d43db9a0 100644 --- a/etcdctl/README.md +++ b/etcdctl/README.md @@ -439,6 +439,26 @@ Prints lease information. # {"cluster_id":17186838941855831277,"member_id":4845372305070271874,"revision":3,"raft_term":2,"id":3279279168933706764,"ttl":459,"granted-ttl":500,"keys":["Zm9vMQ==","Zm9vMg=="]} ``` +### LEASE LIST + +LEASE LIST lists all active leases. + +RPC: LeaseLeases + +#### Output + +Prints a message with a list of active leases. + +#### Example + +```bash +./etcdctl lease grant 10 +# lease 32695410dcc0ca06 granted with TTL(10s) + +./etcdctl lease list +32695410dcc0ca06 +``` + ### LEASE KEEP-ALIVE \ LEASE KEEP-ALIVE periodically refreshes a lease so it does not expire. @@ -736,9 +756,9 @@ If NOSPACE alarm is present: ### DEFRAG [options] -DEFRAG defragments the backend database file for a set of given endpoints while etcd is running, or directly defragments an -etcd data directory while etcd is not running. When an etcd member reclaims storage space from deleted and compacted keys, the -space is kept in a free list and the database file remains the same size. By defragmenting the database, the etcd member +DEFRAG defragments the backend database file for a set of given endpoints while etcd is running, or directly defragments an +etcd data directory while etcd is not running. When an etcd member reclaims storage space from deleted and compacted keys, the +space is kept in a free list and the database file remains the same size. By defragmenting the database, the etcd member releases this free space back to the file system. #### Options diff --git a/etcdctl/ctlv3/command/lease_command.go b/etcdctl/ctlv3/command/lease_command.go index 0afb3d69c7c..fe6fa3634a0 100644 --- a/etcdctl/ctlv3/command/lease_command.go +++ b/etcdctl/ctlv3/command/lease_command.go @@ -33,6 +33,7 @@ func NewLeaseCommand() *cobra.Command { lc.AddCommand(NewLeaseGrantCommand()) lc.AddCommand(NewLeaseRevokeCommand()) lc.AddCommand(NewLeaseTimeToLiveCommand()) + lc.AddCommand(NewLeaseListCommand()) lc.AddCommand(NewLeaseKeepAliveCommand()) return lc @@ -129,6 +130,25 @@ func leaseTimeToLiveCommandFunc(cmd *cobra.Command, args []string) { display.TimeToLive(*resp, timeToLiveKeys) } +// NewLeaseListCommand returns the cobra command for "lease list". +func NewLeaseListCommand() *cobra.Command { + lc := &cobra.Command{ + Use: "list", + Short: "List all active leases", + Run: leaseListCommandFunc, + } + return lc +} + +// leaseListCommandFunc executes the "lease list" command. +func leaseListCommandFunc(cmd *cobra.Command, args []string) { + resp, rerr := mustClientFromCmd(cmd).Leases(context.TODO()) + if rerr != nil { + ExitWithError(ExitBadConnection, rerr) + } + display.Leases(*resp) +} + // NewLeaseKeepAliveCommand returns the cobra command for "lease keep-alive". func NewLeaseKeepAliveCommand() *cobra.Command { lc := &cobra.Command{ diff --git a/etcdctl/ctlv3/command/printer.go b/etcdctl/ctlv3/command/printer.go index 613c555ebe6..64c0fb72e5a 100644 --- a/etcdctl/ctlv3/command/printer.go +++ b/etcdctl/ctlv3/command/printer.go @@ -36,6 +36,7 @@ type printer interface { Revoke(id v3.LeaseID, r v3.LeaseRevokeResponse) KeepAlive(r v3.LeaseKeepAliveResponse) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) + Leases(r v3.LeaseLeasesResponse) MemberAdd(v3.MemberAddResponse) MemberRemove(id uint64, r v3.MemberRemoveResponse) @@ -96,6 +97,7 @@ func (p *printerRPC) Grant(r v3.LeaseGrantResponse) { p.p(r func (p *printerRPC) Revoke(id v3.LeaseID, r v3.LeaseRevokeResponse) { p.p(r) } func (p *printerRPC) KeepAlive(r v3.LeaseKeepAliveResponse) { p.p(r) } func (p *printerRPC) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) { p.p(&r) } +func (p *printerRPC) Leases(r v3.LeaseLeasesResponse) { p.p(&r) } func (p *printerRPC) MemberAdd(r v3.MemberAddResponse) { p.p((*pb.MemberAddResponse)(&r)) } func (p *printerRPC) MemberRemove(id uint64, r v3.MemberRemoveResponse) { diff --git a/etcdctl/ctlv3/command/printer_fields.go b/etcdctl/ctlv3/command/printer_fields.go index f7d1cae5915..24ff283a8ca 100644 --- a/etcdctl/ctlv3/command/printer_fields.go +++ b/etcdctl/ctlv3/command/printer_fields.go @@ -118,6 +118,13 @@ func (p *fieldsPrinter) TimeToLive(r v3.LeaseTimeToLiveResponse, keys bool) { } } +func (p *fieldsPrinter) Leases(r v3.LeaseLeasesResponse) { + p.hdr(r.ResponseHeader) + for _, item := range r.Leases { + fmt.Println(`"ID" :`, item.ID) + } +} + func (p *fieldsPrinter) MemberList(r v3.MemberListResponse) { p.hdr(r.Header) for _, m := range r.Members { diff --git a/etcdctl/ctlv3/command/printer_simple.go b/etcdctl/ctlv3/command/printer_simple.go index 5e0ae9d3fd2..fc1c66dfbc1 100644 --- a/etcdctl/ctlv3/command/printer_simple.go +++ b/etcdctl/ctlv3/command/printer_simple.go @@ -104,6 +104,13 @@ func (s *simplePrinter) TimeToLive(resp v3.LeaseTimeToLiveResponse, keys bool) { fmt.Println(txt) } +func (s *simplePrinter) Leases(resp v3.LeaseLeasesResponse) { + fmt.Printf("found %d leases\n", len(resp.Leases)) + for _, item := range resp.Leases { + fmt.Printf("%016x\n", item.ID) + } +} + func (s *simplePrinter) Alarm(resp v3.AlarmResponse) { for _, e := range resp.Alarms { fmt.Printf("%+v\n", e)