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: fix json output #19469

Draft
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

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: kstrifonoff
Once this PR has been reviewed and has the lgtm label, please assign spzala for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@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 36 lines in your changes missing coverage. Please review.

Project coverage is 68.94%. Comparing base (49fbd1e) to head (3c14ffe).
Report is 55 commits behind head on main.

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

... and 24 files with indirect coverage changes

@@            Coverage Diff             @@
##             main   #19469      +/-   ##
==========================================
+ Coverage   68.87%   68.94%   +0.06%     
==========================================
  Files         420      421       +1     
  Lines       35788    35893     +105     
==========================================
+ Hits        24648    24745      +97     
- Misses       9714     9725      +11     
+ Partials     1426     1423       -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 49fbd1e...3c14ffe. Read the comment docs.

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

Signed-off-by: Kirill Trifonov <kstrifonoff@gmail.com>
@kstrifonoff kstrifonoff force-pushed the fix-hex-json-printer branch from db2b56f to 3c14ffe Compare March 9, 2025 04:27
@k8s-ci-robot
Copy link

@kstrifonoff: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-etcd-coverage-report 3c14ffe link true /test pull-etcd-coverage-report

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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. I understand the commands that are listed here.

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
3 participants