-
Notifications
You must be signed in to change notification settings - Fork 110
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
Switch to EndpointSlices #154
Merged
openshift-merge-robot
merged 1 commit into
openshift:master
from
frobware:endpointslices
Jul 31, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package endpointsubset | ||
|
||
import ( | ||
"bytes" | ||
"net" | ||
"sort" | ||
"strings" | ||
|
||
kapi "k8s.io/api/core/v1" | ||
) | ||
|
||
type EndpointAddressCmpFunc func(x, y *kapi.EndpointAddress) int | ||
type EndpointAddressLessFunc func(x, y *kapi.EndpointAddress) bool | ||
|
||
type endpointAddressMultiSorter struct { | ||
addresses []kapi.EndpointAddress | ||
less []EndpointAddressLessFunc | ||
} | ||
|
||
var ( | ||
EndpointAddressHostnameCmpFn = func(x, y *kapi.EndpointAddress) int { | ||
return strings.Compare(x.Hostname, y.Hostname) | ||
} | ||
|
||
EndpointAddressIPCmpFn = func(x, y *kapi.EndpointAddress) int { | ||
return bytes.Compare(net.ParseIP(x.IP), net.ParseIP(y.IP)) | ||
} | ||
|
||
EndpointAddressHostnameLessFn = func(x, y *kapi.EndpointAddress) bool { | ||
return EndpointAddressHostnameCmpFn(x, y) < 0 | ||
} | ||
|
||
EndpointAddressIPLessFn = func(x, y *kapi.EndpointAddress) bool { | ||
return EndpointAddressIPCmpFn(x, y) < 0 | ||
} | ||
) | ||
|
||
var _ sort.Interface = &endpointAddressMultiSorter{} | ||
|
||
// Sort sorts the argument slice according to the comparator functions | ||
// passed to orderBy. | ||
func (s *endpointAddressMultiSorter) Sort(addresses []kapi.EndpointAddress) { | ||
s.addresses = addresses | ||
sort.Sort(s) | ||
} | ||
|
||
// newEndpointAddressOrderBy returns a Sorter that sorts using a number | ||
// of comparator functions. | ||
func newEndpointAddressOrderBy(less ...EndpointAddressLessFunc) *endpointAddressMultiSorter { | ||
return &endpointAddressMultiSorter{ | ||
less: less, | ||
} | ||
} | ||
|
||
// Len is part of sort.Interface. | ||
func (s *endpointAddressMultiSorter) Len() int { | ||
return len(s.addresses) | ||
} | ||
|
||
// Swap is part of sort.Interface. | ||
func (s *endpointAddressMultiSorter) Swap(i, j int) { | ||
s.addresses[i], s.addresses[j] = s.addresses[j], s.addresses[i] | ||
} | ||
|
||
// Less is part of sort.Interface. | ||
func (s *endpointAddressMultiSorter) Less(i, j int) bool { | ||
p, q := s.addresses[i], s.addresses[j] | ||
|
||
// Try all but the last comparison. | ||
var k int | ||
for k = 0; k < len(s.less)-1; k++ { | ||
less := s.less[k] | ||
switch { | ||
case less(&p, &q): | ||
return true | ||
case less(&q, &p): | ||
return false | ||
} | ||
// p == q; try the next comparison. | ||
} | ||
|
||
return s.less[k](&p, &q) | ||
} | ||
|
||
func DefaultEndpointAddressOrderByFuncs() []EndpointAddressLessFunc { | ||
return []EndpointAddressLessFunc{ | ||
EndpointAddressIPLessFn, | ||
EndpointAddressHostnameLessFn, | ||
} | ||
} | ||
|
||
func SortAddresses(addresses []kapi.EndpointAddress, orderByFuncs ...EndpointAddressLessFunc) { | ||
if len(orderByFuncs) == 0 { | ||
orderByFuncs = DefaultEndpointAddressOrderByFuncs() | ||
} | ||
newEndpointAddressOrderBy(orderByFuncs...).Sort(addresses) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package endpointsubset | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
kapi "k8s.io/api/core/v1" | ||
) | ||
|
||
type EndpointPortCmpFunc func(x, y *kapi.EndpointPort) int | ||
type EndpointPortLessFunc func(x, y *kapi.EndpointPort) bool | ||
|
||
type endpointPortMultiSorter struct { | ||
ports []kapi.EndpointPort | ||
less []EndpointPortLessFunc | ||
} | ||
|
||
var _ sort.Interface = &endpointPortMultiSorter{} | ||
|
||
var ( | ||
EndpointPortNameCmpFn = func(x, y *kapi.EndpointPort) int { | ||
return strings.Compare(x.Name, y.Name) | ||
} | ||
|
||
EndpointPortNameLessFn = func(x, y *kapi.EndpointPort) bool { | ||
return EndpointPortNameCmpFn(x, y) < 0 | ||
} | ||
|
||
EndpointPortNumberCmpFn = func(x, y *kapi.EndpointPort) int { | ||
return int(x.Port - y.Port) | ||
} | ||
|
||
EndpointPortPortNumberLessFn = func(x, y *kapi.EndpointPort) bool { | ||
return EndpointPortNumberCmpFn(x, y) < 0 | ||
} | ||
|
||
EndpointPortProtocolCmpFn = func(x, y *kapi.EndpointPort) int { | ||
return strings.Compare(string(x.Protocol), string(y.Protocol)) | ||
} | ||
|
||
EndpointPortProtocolLessFn = func(x, y *kapi.EndpointPort) bool { | ||
return EndpointPortProtocolCmpFn(x, y) < 0 | ||
} | ||
) | ||
|
||
// Sort sorts the argument slice according to the comparator functions | ||
// passed to orderBy. | ||
func (s *endpointPortMultiSorter) Sort(ports []kapi.EndpointPort) { | ||
s.ports = ports | ||
sort.Sort(s) | ||
} | ||
|
||
// endpointPortOrderBy returns a Sorter that sorts using a number | ||
// of comparator functions. | ||
func endpointPortOrderBy(less ...EndpointPortLessFunc) *endpointPortMultiSorter { | ||
return &endpointPortMultiSorter{ | ||
less: less, | ||
} | ||
} | ||
|
||
// Len is part of sort.Interface. | ||
func (s *endpointPortMultiSorter) Len() int { | ||
return len(s.ports) | ||
} | ||
|
||
// Swap is part of sort.Interface. | ||
func (s *endpointPortMultiSorter) Swap(i, j int) { | ||
s.ports[i], s.ports[j] = s.ports[j], s.ports[i] | ||
} | ||
|
||
// Less is part of sort.Interface. | ||
func (s *endpointPortMultiSorter) Less(i, j int) bool { | ||
p, q := s.ports[i], s.ports[j] | ||
|
||
// Try all but the last comparison. | ||
var k int | ||
for k = 0; k < len(s.less)-1; k++ { | ||
less := s.less[k] | ||
switch { | ||
case less(&p, &q): | ||
return true | ||
case less(&q, &p): | ||
return false | ||
} | ||
// p == q; try the next comparison. | ||
} | ||
|
||
return s.less[k](&p, &q) | ||
} | ||
|
||
func DefaultEndpointPortOrderByFuncs() []EndpointPortLessFunc { | ||
return []EndpointPortLessFunc{ | ||
EndpointPortPortNumberLessFn, | ||
EndpointPortProtocolLessFn, | ||
EndpointPortNameLessFn, | ||
} | ||
} | ||
|
||
func SortPorts(ports []kapi.EndpointPort, orderByFuncs ...EndpointPortLessFunc) { | ||
if len(orderByFuncs) == 0 { | ||
orderByFuncs = DefaultEndpointPortOrderByFuncs() | ||
} | ||
endpointPortOrderBy(orderByFuncs...).Sort(ports) | ||
} |
Oops, something went wrong.
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.
It's good to be prudent, but is there any reason to believe that an endpoint address can be non-unique?