Skip to content

Conversation

@zrggw
Copy link
Contributor

@zrggw zrggw commented Jun 27, 2025

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?:


Copilot AI review requested due to automatic review settings June 27, 2025 10:05
@kmesh-bot kmesh-bot added the kind/enhancement New feature or request label Jun 27, 2025
@kmesh-bot
Copy link
Collaborator

Welcome @zrggw! It looks like this is your first PR to kmesh-net/kmesh 🎉

This comment was marked as outdated.

@hzxuzhonghu
Copy link
Member

LGTM

Can you add test cases to the existing test

@kmesh-bot kmesh-bot added size/XXL and removed size/S labels Jul 2, 2025
@hzxuzhonghu
Copy link
Member

why do you change the bpf generated file for kernel native mode

@hzxuzhonghu
Copy link
Member

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines 350 to 356
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]
})

Choose a reason for hiding this comment

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

medium

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.

Comment on lines 372 to 381
// 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)
}

Choose a reason for hiding this comment

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

medium

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.

@hzxuzhonghu hzxuzhonghu requested a review from Copilot July 2, 2025 03:03
Copy link

Copilot AI left a 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.json and workload_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

Comment on lines 350 to 354
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) {
Copy link

Copilot AI Jul 2, 2025

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.

Suggested change
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) {

Copilot uses AI. Check for mistakes.
Comment on lines 350 to 357
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]
})

Copy link

Copilot AI Jul 2, 2025

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.

Suggested change
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 uses AI. Check for mistakes.
// 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)
Copy link

Copilot AI Jul 2, 2025

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.

Suggested change
fmt.Printf("Modified dump differs from original: %v\n", err)
t.Logf("Modified dump differs from original: %v", err)

Copilot uses AI. Check for mistakes.

util.RefreshGoldenFile(t, w.Body.Bytes(), "./testdata/workload_configdump.json")
// Create a new HTTP request and response
req2 := httptest.NewRequest(http.MethodGet, "/configDumpWorkload", nil)
Copy link
Member

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.

@zrggw
Copy link
Contributor Author

zrggw commented Jul 2, 2025

why do you change the bpf generated file for kernel native mode

The generated files under the kernelnative root directory were created incorrectly, and I will delete these files later.

@hzxuzhonghu
Copy link
Member

The generated files under the kernelnative root directory were created incorrectly, and I will delete these files later.

@lec-bit Can you confirm?

@zrggw zrggw force-pushed the sort_dump_output branch from ab62d27 to 5b73a5f Compare July 2, 2025 09:06
@zrggw
Copy link
Contributor Author

zrggw commented Jul 2, 2025

I revise the code according to the code review results.

  1. remove second and third req.
  2. remove rand.
  3. remove generated files under the kernelnative root directory.

@codecov
Copy link

codecov bot commented Jul 2, 2025

Codecov Report

Attention: Patch coverage is 66.66667% with 3 lines in your changes missing coverage. Please review.

Project coverage is 35.86%. Comparing base (8f619b9) to head (81896f5).
Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
pkg/status/status_server.go 66.66% 2 Missing and 1 partial ⚠️
Files with missing lines Coverage Δ
pkg/status/status_server.go 34.04% <66.66%> (+0.70%) ⬆️

... and 32 files with indirect coverage changes


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 accb01d...81896f5. Read the comment docs.

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

* 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
Copy link
Contributor

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.

@zrggw zrggw force-pushed the sort_dump_output branch from 5b73a5f to b8d76f0 Compare July 3, 2025 06:45
zrggw added 3 commits July 3, 2025 14:49
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>
@zrggw zrggw force-pushed the sort_dump_output branch from b8d76f0 to 52bc681 Compare July 3, 2025 06:54
// Modify service properties
svc := buildService(fmt.Sprintf("service-%d-modified", i), fmt.Sprintf("hostname-%d-modified", i))
// Modify service ports
svc.Ports = []*workloadapi.Port{
Copy link
Member

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?

Copy link
Contributor Author

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))
Copy link
Member

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?

Copy link
Contributor Author

@zrggw zrggw Jul 4, 2025

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.

hzxuzhonghu
hzxuzhonghu previously approved these changes Jul 4, 2025
@hzxuzhonghu
Copy link
Member

please fix the ci

Signed-off-by: aicee <hhbin2000@foxmail.com>
@zrggw
Copy link
Contributor Author

zrggw commented Jul 10, 2025

/retest

@kmesh-bot
Copy link
Collaborator

@zrggw: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

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.

@hzxuzhonghu
Copy link
Member

/ok-to-test

/retest

@zrggw
Copy link
Contributor Author

zrggw commented Jul 10, 2025

/retest

@hzxuzhonghu
Copy link
Member

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 {
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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>
@LiZhenCheng9527
Copy link
Contributor

/lgtm

Copy link
Member

@hzxuzhonghu hzxuzhonghu left a comment

Choose a reason for hiding this comment

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

/approve

@kmesh-bot
Copy link
Collaborator

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kmesh-bot kmesh-bot merged commit e35895c into kmesh-net:main Jul 17, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants