Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

etcdctl: add hex option for endpoint status #12110

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions etcdctl/ctlv3/command/printer_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,15 @@ func newJSONPrinter(isHex bool) printer {
}

func (p *jsonPrinter) EndpointHealth(r []epHealth) { printJSON(r) }
func (p *jsonPrinter) EndpointStatus(r []epStatus) { printJSON(r) }
func (p *jsonPrinter) EndpointHashKV(r []epHashKV) { printJSON(r) }
func (p *jsonPrinter) EndpointStatus(r []epStatus) {
if p.isHex {
printEndpointStatusWithHexJSON(r)
} else {
printJSON(r)
}
}

func (p *jsonPrinter) DBStatus(r snapshot.Status) { printJSON(r) }

func (p *jsonPrinter) MemberList(r clientv3.MemberListResponse) {
Expand All @@ -59,6 +66,60 @@ func printJSON(v interface{}) {
fmt.Println(string(b))
}

func printEndpointStatusWithHexJSON(r []epStatus) {
var buffer bytes.Buffer
var b []byte

buffer.WriteString("[")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid "hand-crafting" of JSON format as it might have subtle escaping consequences.

Please see discussion in:
#12595 (comment)

for i := 0; i < len(r); i++ {
if i != 0 {
buffer.WriteString(",")
}

buffer.WriteString("{\"Endpoint\":\"" + r[i].Ep + "\",")
buffer.WriteString("\"Status\":{ \"header\":{\"cluster_id\":\"")
b = strconv.AppendUint(nil, r[i].Resp.Header.ClusterId, 16)
buffer.Write(b)
buffer.WriteString("\",\"member_id\":\"")
b = strconv.AppendUint(nil, r[i].Resp.Header.MemberId, 16)
buffer.Write(b)

buffer.WriteString("\",\"revision\":\"")
b = strconv.AppendInt(nil, r[i].Resp.Header.Revision, 16)
buffer.Write(b)

buffer.WriteString("\",\"raft_term\":\"")
b = strconv.AppendUint(nil, r[i].Resp.Header.RaftTerm, 16)
buffer.Write(b)
buffer.WriteString("\"}")

buffer.WriteString(",\"version\":\"" + r[i].Resp.Version + "\"")
buffer.WriteString(",\"dbSize\":\"")
b = strconv.AppendInt(nil, r[i].Resp.DbSize, 16)
buffer.Write(b)
buffer.WriteString("\",\"leader\":\"")
b = strconv.AppendUint(nil, r[i].Resp.Leader, 16)
buffer.Write(b)
buffer.WriteString("\",\"raftIndex\":\"")
b = strconv.AppendUint(nil, r[i].Resp.RaftIndex, 16)
buffer.Write(b)
buffer.WriteString("\",\"raftTerm\":\"")
b = strconv.AppendUint(nil, r[i].Resp.RaftTerm, 16)
buffer.Write(b)
buffer.WriteString("\",\"raftAppliedIndex\":\"")
b = strconv.AppendUint(nil, r[i].Resp.RaftAppliedIndex, 16)
buffer.Write(b)
buffer.WriteString("\",\"dbSizeInUse\":\"")
b = strconv.AppendInt(nil, r[i].Resp.DbSizeInUse, 16)
buffer.Write(b)

buffer.WriteString("\"}}")
}

buffer.WriteString("]")
fmt.Println(string(buffer.Bytes()))
}

func printMemberListWithHexJSON(r clientv3.MemberListResponse) {
var buffer bytes.Buffer
var b []byte
Expand All @@ -68,10 +129,10 @@ func printMemberListWithHexJSON(r clientv3.MemberListResponse) {
buffer.WriteString("\",\"member_id\":\"")
b = strconv.AppendUint(nil, r.Header.MemberId, 16)
buffer.Write(b)
buffer.WriteString("\",\"raft_term\":")
buffer.WriteString("\",\"raft_term\":\"")
b = strconv.AppendUint(nil, r.Header.RaftTerm, 16)
buffer.Write(b)
buffer.WriteByte('}')
buffer.WriteString("\"}")
for i := 0; i < len(r.Members); i++ {
if i == 0 {
buffer.WriteString(",\"members\":[{\"ID\":\"")
Expand Down
13 changes: 13 additions & 0 deletions tests/e2e/ctl_v3_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

func TestCtlV3EndpointHealth(t *testing.T) { testCtl(t, endpointHealthTest, withQuorum()) }
func TestCtlV3EndpointStatus(t *testing.T) { testCtl(t, endpointStatusTest, withQuorum()) }
func TestCtlV3EndpointStatusHex(t *testing.T) { testCtl(t, endpointStatusHexTest, withQuorum()) }
func TestCtlV3EndpointHashKV(t *testing.T) { testCtl(t, endpointHashKVTest, withQuorum()) }

func endpointHealthTest(cx ctlCtx) {
Expand All @@ -49,6 +50,18 @@ func endpointStatusTest(cx ctlCtx) {
}
}

func endpointStatusHexTest(cx ctlCtx) {
if err := endpointStatusTest(cx); err != nil {
cx.t.Fatalf("endpointStatusTest error (%v)", err)
}

cmdArgs := append(cx.PrefixArgs(), "--write-out", "json", "--hex", "endpoint", "status")
proc, err := spawnCmd(cmdArgs)
if err != nil {
cx.t.Fatalf("endpointStatusHexTest error (%v)", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please verify the test output (printed format).

}

func ctlV3EndpointStatus(cx ctlCtx) error {
cmdArgs := append(cx.PrefixArgs(), "endpoint", "status")
var eps []string
Expand Down