-
Notifications
You must be signed in to change notification settings - Fork 459
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 order of ports returned to fix flaky tests #1003
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cc6c3c7
Sort order of ports returned to fix flaky tests
kevinearls b0f3b9d
Verify type of receiver name
kevinearls 7d22f3b
Sort ports too before returning
kevinearls 5305fc2
Add unit test
kevinearls 0b72f8d
respond to comments
kevinearls 75ce40f
fix nits
kevinearls File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,7 @@ package adapters | |||||||||||||||
|
||||||||||||||||
import ( | ||||||||||||||||
"errors" | ||||||||||||||||
"sort" | ||||||||||||||||
|
||||||||||||||||
"github.com/go-logr/logr" | ||||||||||||||||
corev1 "k8s.io/api/core/v1" | ||||||||||||||||
|
@@ -60,28 +61,34 @@ func ConfigToReceiverPorts(logger logr.Logger, config map[interface{}]interface{ | |||||||||||||||
return nil, ErrReceiversNotAMap | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
sortedNames := make([]string, 0, len(receivers)) | ||||||||||||||||
for name := range receivers { | ||||||||||||||||
sortedNames = append(sortedNames, name.(string)) | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to check the type just for safety here?
Suggested change
|
||||||||||||||||
} | ||||||||||||||||
sort.Strings(sortedNames) | ||||||||||||||||
|
||||||||||||||||
ports := []corev1.ServicePort{} | ||||||||||||||||
for key, val := range receivers { | ||||||||||||||||
for _, name := range sortedNames { | ||||||||||||||||
// This check will pass only the enabled receivers, | ||||||||||||||||
// then only the related ports will be opened. | ||||||||||||||||
if !recEnabled[key] { | ||||||||||||||||
if !recEnabled[name] { | ||||||||||||||||
continue | ||||||||||||||||
} | ||||||||||||||||
receiver, ok := val.(map[interface{}]interface{}) | ||||||||||||||||
|
||||||||||||||||
receiver, ok := receivers[name].(map[interface{}]interface{}) | ||||||||||||||||
if !ok { | ||||||||||||||||
logger.Info("receiver doesn't seem to be a map of properties", "receiver", key) | ||||||||||||||||
logger.Info("receiver doesn't seem to be a map of properties", "receiver", name) | ||||||||||||||||
receiver = map[interface{}]interface{}{} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
rcvrName := key.(string) | ||||||||||||||||
rcvrParser := parser.For(logger, rcvrName, receiver) | ||||||||||||||||
rcvrParser := parser.For(logger, name, receiver) | ||||||||||||||||
|
||||||||||||||||
rcvrPorts, err := rcvrParser.Ports() | ||||||||||||||||
if err != nil { | ||||||||||||||||
// should we break the process and return an error, or just ignore this faulty parser | ||||||||||||||||
// and let the other parsers add their ports to the service? right now, the best | ||||||||||||||||
// option seems to be to log the failures and move on, instead of failing them all | ||||||||||||||||
logger.Error(err, "parser for '%s' has returned an error: %w", rcvrName, err) | ||||||||||||||||
logger.Error(err, "parser for '%s' has returned an error: %w", name, err) | ||||||||||||||||
continue | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@kevinearls wouldn't it be cleaner to sort
ports
at the end of the function?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.
Maybe, but I'm not sure it necessary. Originally when I saw this problem it occurred because we were getting receivers in the wrong order. Sorting them alphabetically by name is fairly simple.
The ports, on the other hand, are in an array of corev1.ServicePort. That does not implement methods needed to feed it to sort.Sort(), so sorting those would be non-trivial.
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.
What is the root cause of the problem? That the ports are not sorted or receivers? We should use the least error-prone approach that will guarantee always the same result.
Take a look at
sort.Slice
: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 problem I had was caused by the receivers being in the wrong order, not the ports for a receiver.
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.
In other words, the implementation in this PR will break again if
ReceiverParser.Ports()
returns randomly sorted ports. In fact right now that API does not guarantee the order.Also, there could be a unit test checking the functionality - or change the existing tests to assume the order of ports matters.