-
Notifications
You must be signed in to change notification settings - Fork 132
Sort dump output by name #1432
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
Sort dump output by name #1432
Conversation
|
Welcome @zrggw! It looks like this is your first PR to kmesh-net/kmesh 🎉 |
|
LGTM Can you add test cases to the existing test |
|
why do you change the bpf generated file for kernel native mode |
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces sorting to the kmeshctl dump output, which is a valuable enhancement for ensuring deterministic output for comparisons and testing. The implementation in pkg/status/status_server.go is correct and achieves the desired outcome.
The tests have been significantly expanded to validate this new behavior. However, I've identified a couple of areas for improvement in pkg/status/status_server_test.go. Specifically, there's some ineffective code related to shuffling test data, and the test unconditionally writes golden files, which is not ideal. My comments provide suggestions to address these points.
pkg/status/status_server_test.go
Outdated
| rand.New(rand.NewSource(time.Now().UnixNano())) | ||
| rand.Shuffle(len(workloads), func(i, j int) { | ||
| workloads[i], workloads[j] = workloads[j], workloads[i] | ||
| }) | ||
| rand.Shuffle(len(services), func(i, j int) { | ||
| services[i], services[j] = services[j], services[i] | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rand.Shuffle calls on the workloads and services slices have no effect on the test outcome. The server.configDumpWorkload function retrieves data from the cache within the server object, which was populated before this shuffle. The local workloads and services slices are not used after being shuffled.
The non-deterministic nature of map iteration within the cache's List() method is sufficient to test that the output is correctly sorted.
Additionally, rand.New(...) creates a new rand.Rand object that is not used, so this line is a no-op. Please remove these lines.
pkg/status/status_server_test.go
Outdated
| // save original and modified dumps to json files | ||
| err = os.WriteFile("./testdata/workload_configdump_original.json", w2.Body.Bytes(), 0644) | ||
| if err != nil { | ||
| t.Errorf("Failed to write original dump to file: %v", err) | ||
| } | ||
|
|
||
| err = os.WriteFile("./testdata/workload_configdump_modified.json", w3.Body.Bytes(), 0644) | ||
| if err != nil { | ||
| t.Errorf("Failed to write modified dump to file: %v", err) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test unconditionally writes to workload_configdump_original.json and workload_configdump_modified.json. Tests should not write files to the source tree during normal execution as it can cause side effects and make tests non-hermetic.
If the goal is to update golden files, this should be controlled by a flag (e.g., an -update flag). Since these files are already part of the pull request, this logic seems unnecessary for the test itself. Consider removing these os.WriteFile calls. The existing util.RefreshGoldenFile is a better pattern for managing golden files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR ensures the output of kmeshctl dump is consistently ordered by name and adds tests to verify this behavior.
- Added sorting of workloads, services, and policies in the server before marshalling JSON.
- Refactored and expanded the integration test (
TestServer_configDumpWorkload) with helpers, multiple scenarios, shuffle-based verification, and golden file generation. - Added two JSON fixtures (
workload_configdump_original.jsonandworkload_configdump_modified.json) for the new test.
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| pkg/status/status_server.go | Added sort.Slice calls to order workloads, services, and policies |
| pkg/status/status_server_test.go | Rewrote dump test with buildWorkload/buildService, multiple dumps, shuffling, and golden-file logic |
| pkg/status/testdata/workload_configdump_original.json | New fixture representing the original sorted dump |
| pkg/status/testdata/workload_configdump_modified.json | New fixture representing the modified sorted dump |
pkg/status/status_server_test.go
Outdated
| rand.New(rand.NewSource(time.Now().UnixNano())) | ||
| rand.Shuffle(len(workloads), func(i, j int) { | ||
| workloads[i], workloads[j] = workloads[j], workloads[i] | ||
| }) | ||
| rand.Shuffle(len(services), func(i, j int) { |
Copilot
AI
Jul 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling rand.New without using its returned *Rand makes the seeding ineffective; use rand.Seed to seed the global RNG or assign the new Rand instance to a variable and use it for Shuffle.
| rand.New(rand.NewSource(time.Now().UnixNano())) | |
| rand.Shuffle(len(workloads), func(i, j int) { | |
| workloads[i], workloads[j] = workloads[j], workloads[i] | |
| }) | |
| rand.Shuffle(len(services), func(i, j int) { | |
| r := rand.New(rand.NewSource(time.Now().UnixNano())) | |
| r.Shuffle(len(workloads), func(i, j int) { | |
| workloads[i], workloads[j] = workloads[j], workloads[i] | |
| }) | |
| r.Shuffle(len(services), func(i, j int) { |
pkg/status/status_server_test.go
Outdated
| rand.New(rand.NewSource(time.Now().UnixNano())) | ||
| rand.Shuffle(len(workloads), func(i, j int) { | ||
| workloads[i], workloads[j] = workloads[j], workloads[i] | ||
| }) | ||
| rand.Shuffle(len(services), func(i, j int) { | ||
| services[i], services[j] = services[j], services[i] | ||
| }) | ||
|
|
Copilot
AI
Jul 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shuffling the workloads slice after populating the cache has no effect on the cache’s internal order; move the Shuffle call to before adding entries to fakeWorkloadCache so the test actually verifies sorting.
| rand.New(rand.NewSource(time.Now().UnixNano())) | |
| rand.Shuffle(len(workloads), func(i, j int) { | |
| workloads[i], workloads[j] = workloads[j], workloads[i] | |
| }) | |
| rand.Shuffle(len(services), func(i, j int) { | |
| services[i], services[j] = services[j], services[i] | |
| }) |
pkg/status/status_server_test.go
Outdated
| // compare the modified dump with the original | ||
| err := util.Compare(w3.Body.Bytes(), w2.Body.Bytes()) | ||
| if err != nil { | ||
| fmt.Printf("Modified dump differs from original: %v\n", err) |
Copilot
AI
Jul 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use t.Logf instead of fmt.Printf in tests so log output is captured by the test runner; consider replacing this call with t.Logf.
| fmt.Printf("Modified dump differs from original: %v\n", err) | |
| t.Logf("Modified dump differs from original: %v", err) |
pkg/status/status_server_test.go
Outdated
|
|
||
| util.RefreshGoldenFile(t, w.Body.Bytes(), "./testdata/workload_configdump.json") | ||
| // Create a new HTTP request and response | ||
| req2 := httptest.NewRequest(http.MethodGet, "/configDumpWorkload", nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel we donot need to send a second req, mixing the workloads and services at first, we can test getting only once.
The generated files under the kernelnative root directory were created incorrectly, and I will delete these files later. |
@lec-bit Can you confirm? |
|
I revise the code according to the code review results.
|
Codecov ReportAttention: Patch coverage is
... and 32 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
| * loopback address link conflicts. Obtains the namespace cookie of the | ||
| * current container based on the bpf_get_netns_cookie auxiliary function. | ||
| */ | ||
| #define MDA_LOOPBACK_ADDR 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use make clean command clean up if you have build kmesh code locally before pushing your code to github.
Signed-off-by: aicee <hhbin2000@foxmail.com>
Signed-off-by: aicee <hhbin2000@foxmail.com>
…cording to the code review results. Signed-off-by: aicee <hhbin2000@foxmail.com>
| // Modify service properties | ||
| svc := buildService(fmt.Sprintf("service-%d-modified", i), fmt.Sprintf("hostname-%d-modified", i)) | ||
| // Modify service ports | ||
| svc.Ports = []*workloadapi.Port{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why modify ports, does it influence the order?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to test that the sorting is not affected by other modifications, like port modifications.
| services := []*workloadapi.Service{} | ||
|
|
||
| for i := 0; i < 10; i++ { | ||
| w := buildWorkload(fmt.Sprintf("workload-%d", i)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this make the workloads alreadt sorted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Later, when w is added to fakeWorkloadCache(line 326) and then a workDump is created(line 346 and status_servers.go, line 493), the wokload in workloadDump will be unordered due to the use of map.
|
please fix the ci |
Signed-off-by: aicee <hhbin2000@foxmail.com>
|
/retest |
|
@zrggw: Cannot trigger testing until a trusted user reviews the PR and leaves an In response to this:
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/test-infra repository. |
|
/ok-to-test /retest |
|
/retest |
|
Looks this failure is related |
Signed-off-by: aicee <hhbin2000@foxmail.com>
| secretManager.SendCertRequest(identity, RETRY) | ||
| time.Sleep(2000 * time.Millisecond) | ||
| assert.NotNil(t, secretManager.GetCert(identity).cert) | ||
| for { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: please use retry.UntilSuccess, which also have a timeout
And seems we can remove L168 now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we can also remove line 182-186? Because we have already checked whether cert.cert is nil on line 173.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right
Signed-off-by: aicee <hhbin2000@foxmail.com>
|
/lgtm |
hzxuzhonghu
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/approve
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: hzxuzhonghu The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What type of PR is this?
/kind enhancement
What this PR does / why we need it:
#1431
Because the output of "kmeshctl dump" is unordered, it is inconvenient to compare the results from two different uses of dump. So I sorted it by "name".
Does this PR introduce a user-facing change?: