Skip to content

etcdctl: fix json output #19469

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kstrifonoff
Copy link

@kstrifonoff kstrifonoff commented Feb 23, 2025

Fix #19262

@k8s-ci-robot
Copy link

Hi @kstrifonoff. Thanks for your PR.

I'm waiting for a etcd-io member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kstrifonoff
Copy link
Author

/cc @ivanvc

@k8s-ci-robot k8s-ci-robot requested a review from ivanvc February 23, 2025 15:10
@ivanvc
Copy link
Member

ivanvc commented Feb 25, 2025

Hi @kstrifonoff, thanks for the pull request. I'm respectful to the original contributor who asked to work on this. Our guideline is to re-assign after a week of no answer when asking a contributor for updates.

A couple of suggestions by quickly looking at the code.

  1. The imports contain a lint error. Running make verify-lint locally will show you the error.
  2. The code doesn't have unit tests for this, but there's an e2e test, TestCtlV3MemberListWithHex. I think the test should pass, but adding your new cases would be good.
  3. I suggest reviewing the code from printKV. I think it would be better to follow that implementation approach, i.e., there would be less repetition with the if conditions.

Thanks again for your pull request :)

@kstrifonoff
Copy link
Author

Hi @ivanvc ,

I’ve made some changes to my initial suggestions based on your feedback.

Now, the “JSON with Hex” implementation is just a slight extension of the default “printJSON” implementation. I think it should probably be moved into the “utils” package.

Here are a few things I noticed:

  1. The implementation assumes marshalling back and forth the default JSON string, which can be inefficient, but I assume it’s still okay for the CLI.
  2. Also, the current approach makes it easy to add “json with hex” output for all the rest of commands that support JSON output.
  3. I’m not sure what kind of end-to-end test should be done here. What should it cover? It’s also not clear to me what the current end-to-end test actually checks.

@kstrifonoff kstrifonoff marked this pull request as ready for review March 6, 2025 20:07
@ivanvc
Copy link
Member

ivanvc commented Mar 7, 2025

/ok-to-test

@ivanvc
Copy link
Member

ivanvc commented Mar 7, 2025

I’m not sure what kind of end-to-end test should be done here. What should it cover? It’s also not clear to me what the current end-to-end test actually checks.

The e2e tests already exist, my suggestion is to expand them to cover the new cases you added:

func memberListWithHexTest(cx ctlCtx) {
resp, err := getMemberList(cx, false)
if err != nil {
cx.t.Fatalf("getMemberList error (%v)", err)
}
cmdArgs := append(cx.PrefixArgs(), "--write-out", "json", "--hex", "member", "list")
proc, err := e2e.SpawnCmd(cmdArgs, cx.envMap)
if err != nil {
cx.t.Fatalf("memberListWithHexTest error (%v)", err)
}
var txt string
txt, err = proc.Expect("members")
if err != nil {
cx.t.Fatalf("memberListWithHexTest error (%v)", err)
}
if err = proc.Close(); err != nil {
cx.t.Fatalf("memberListWithHexTest error (%v)", err)
}
hexResp := etcdserverpb.MemberListResponse{}
dec := json.NewDecoder(strings.NewReader(txt))
if err := dec.Decode(&hexResp); errors.Is(err, io.EOF) {
cx.t.Fatalf("memberListWithHexTest error (%v)", err)
}
num := len(resp.Members)
hexNum := len(hexResp.Members)
if num != hexNum {
cx.t.Fatalf("member number,expected %d,got %d", num, hexNum)
}
if num == 0 {
cx.t.Fatal("member number is 0")
}
if resp.Header.RaftTerm != hexResp.Header.RaftTerm {
cx.t.Fatalf("Unexpected raft_term, expected %d, got %d", resp.Header.RaftTerm, hexResp.Header.RaftTerm)
}
for i := 0; i < num; i++ {
if resp.Members[i].Name != hexResp.Members[i].Name {
cx.t.Fatalf("Unexpected member name,expected %v, got %v", resp.Members[i].Name, hexResp.Members[i].Name)
}
if !reflect.DeepEqual(resp.Members[i].PeerURLs, hexResp.Members[i].PeerURLs) {
cx.t.Fatalf("Unexpected member peerURLs, expected %v, got %v", resp.Members[i].PeerURLs, hexResp.Members[i].PeerURLs)
}
if !reflect.DeepEqual(resp.Members[i].ClientURLs, hexResp.Members[i].ClientURLs) {
cx.t.Fatalf("Unexpected member clientURLs, expected %v, got %v", resp.Members[i].ClientURLs, hexResp.Members[i].ClientURLs)
}
}
}

Copy link

codecov bot commented Mar 7, 2025

Codecov Report

Attention: Patch coverage is 0% with 47 lines in your changes missing coverage. Please review.

Project coverage is 68.75%. Comparing base (37eaafa) to head (311626f).
Report is 116 commits behind head on main.

Files with missing lines Patch % Lines
etcdctl/ctlv3/command/printer_json.go 0.00% 47 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
etcdctl/ctlv3/command/printer_json.go 0.00% <0.00%> (ø)

... and 36 files with indirect coverage changes

@@            Coverage Diff             @@
##             main   #19469      +/-   ##
==========================================
- Coverage   68.81%   68.75%   -0.06%     
==========================================
  Files         421      424       +3     
  Lines       35852    35909      +57     
==========================================
+ Hits        24672    24690      +18     
- Misses       9753     9795      +42     
+ Partials     1427     1424       -3     

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 37eaafa...311626f. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kstrifonoff
Copy link
Author

hi @ivanvc
Could you give it another look? I’ve made a few changes since we last spoke. The initial solution had a bug, so I’ve fixed it.

I’ve also fixed all the remaining member and existing endpoint commands. Now, you can also use them with json and hex output.
I’ve also extended the e2e tests as suggested.

Let me know if you have any questions!

@kstrifonoff
Copy link
Author

@ivanvc just a friendly reminder: could you please review this once you have some time?

@kstrifonoff
Copy link
Author

/cc @fuweid

Could you please review this PR?

@k8s-ci-robot k8s-ci-robot requested a review from fuweid April 29, 2025 20:55
func (p *jsonPrinter) EndpointStatus(r []epStatus) { printJSON(r) }
func (p *jsonPrinter) EndpointHashKV(r []epHashKV) { printJSON(r) }
type (
HexStatusResponse pb.StatusResponse
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for filing this refactor change.
Would you please add one method MemberAdd to jsonPrinter to fix that issue first?
Basically, we don't involve refactor change in bugfix. It's easy to review and maintain.
Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

@fuweid it makes sense. I’ve removed the extra code. Could you please check it again?

If it looks good, let’s talk about how to refactor the rest of the code.

@kstrifonoff
Copy link
Author

/retest

@kstrifonoff kstrifonoff requested a review from fuweid April 30, 2025 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

etcdctl member add -wjson --hex=true does not output member IDs in hex
4 participants