-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Reflect changes to server selector in opaqueness #12031
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
886cc8f
Update endpoints on server change which unselects an address
adleong 108e410
add test
adleong 15b010f
remove debug logging
adleong 9580171
podSelector and externalWorkloadSelector are mutually exclusive
adleong 9a04598
Remove unused const
adleong 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
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 |
---|---|---|
|
@@ -511,11 +511,14 @@ func (ew *EndpointsWatcher) addServer(obj interface{}) { | |
defer ew.Unlock() | ||
server := obj.(*v1beta2.Server) | ||
for _, sp := range ew.publishers { | ||
sp.updateServer(server, true) | ||
sp.updateServer(nil, server) | ||
} | ||
} | ||
|
||
func (ew *EndpointsWatcher) updateServer(oldObj interface{}, newObj interface{}) { | ||
ew.Lock() | ||
defer ew.Unlock() | ||
|
||
oldServer := oldObj.(*v1beta2.Server) | ||
newServer := newObj.(*v1beta2.Server) | ||
oldUpdated := latestUpdated(oldServer.ManagedFields) | ||
|
@@ -525,15 +528,17 @@ func (ew *EndpointsWatcher) updateServer(oldObj interface{}, newObj interface{}) | |
serverInformerLag.Observe(delta.Seconds()) | ||
} | ||
|
||
ew.addServer(newObj) | ||
for _, sp := range ew.publishers { | ||
sp.updateServer(oldServer, newServer) | ||
} | ||
} | ||
|
||
func (ew *EndpointsWatcher) deleteServer(obj interface{}) { | ||
ew.Lock() | ||
defer ew.Unlock() | ||
server := obj.(*v1beta2.Server) | ||
for _, sp := range ew.publishers { | ||
sp.updateServer(server, false) | ||
sp.updateServer(server, nil) | ||
} | ||
} | ||
|
||
|
@@ -703,12 +708,12 @@ func (sp *servicePublisher) metricsLabels(port Port, hostname string) prometheus | |
return endpointsLabels(sp.cluster, sp.id.Namespace, sp.id.Name, strconv.Itoa(int(port)), hostname) | ||
} | ||
|
||
func (sp *servicePublisher) updateServer(server *v1beta2.Server, isAdd bool) { | ||
func (sp *servicePublisher) updateServer(oldServer, newServer *v1beta2.Server) { | ||
sp.Lock() | ||
defer sp.Unlock() | ||
|
||
for _, pp := range sp.ports { | ||
pp.updateServer(server, isAdd) | ||
pp.updateServer(oldServer, newServer) | ||
} | ||
} | ||
|
||
|
@@ -1237,70 +1242,12 @@ func (pp *portPublisher) unsubscribe(listener EndpointUpdateListener) { | |
|
||
pp.metrics.setSubscribers(len(pp.listeners)) | ||
} | ||
func (pp *portPublisher) updateServer(server *v1beta2.Server, isAdd bool) { | ||
func (pp *portPublisher) updateServer(oldServer, newServer *v1beta2.Server) { | ||
updated := false | ||
for id, address := range pp.addresses.Addresses { | ||
portMatch := false | ||
if address.Pod != nil { | ||
selector, err := metav1.LabelSelectorAsSelector(server.Spec.PodSelector) | ||
if err != nil { | ||
pp.log.Errorf("failed to create Selector: %s", err) | ||
return | ||
} | ||
|
||
if !selector.Matches(labels.Set(address.Pod.Labels)) { | ||
continue | ||
} | ||
|
||
switch server.Spec.Port.Type { | ||
case intstr.Int: | ||
if server.Spec.Port.IntVal == int32(address.Port) { | ||
portMatch = true | ||
} | ||
case intstr.String: | ||
for _, c := range address.Pod.Spec.Containers { | ||
for _, p := range c.Ports { | ||
if p.ContainerPort == int32(address.Port) && p.Name == server.Spec.Port.StrVal { | ||
portMatch = true | ||
} | ||
} | ||
} | ||
default: | ||
continue | ||
} | ||
|
||
} else if address.ExternalWorkload != nil { | ||
selector, err := metav1.LabelSelectorAsSelector(server.Spec.ExternalWorkloadSelector) | ||
if err != nil { | ||
pp.log.Errorf("failed to create Selector: %s", err) | ||
return | ||
} | ||
|
||
if !selector.Matches(labels.Set(address.ExternalWorkload.Labels)) { | ||
continue | ||
} | ||
|
||
switch server.Spec.Port.Type { | ||
case intstr.Int: | ||
if server.Spec.Port.IntVal == int32(address.Port) { | ||
portMatch = true | ||
} | ||
case intstr.String: | ||
for _, p := range address.ExternalWorkload.Spec.Ports { | ||
if p.Port == int32(address.Port) && p.Name == server.Spec.Port.StrVal { | ||
portMatch = true | ||
} | ||
} | ||
default: | ||
continue | ||
} | ||
|
||
} else { | ||
continue | ||
} | ||
|
||
if portMatch { | ||
if isAdd && server.Spec.ProxyProtocol == opaqueProtocol { | ||
if pp.isAddressSelected(address, oldServer) || pp.isAddressSelected(address, newServer) { | ||
if newServer != nil && pp.isAddressSelected(address, newServer) && newServer.Spec.ProxyProtocol == opaqueProtocol { | ||
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. Could we maybe simplify this by saying: if pp.isAddressSelected(address, oldServer) || pp.isAddressSelected(address, newServer) {
if pp.isAddressSelected(address, newServer) && newServer.Spec.ProxyProtocol == opaqueProtocol {
}
} iiuc |
||
address.OpaqueProtocol = true | ||
} else { | ||
address.OpaqueProtocol = false | ||
|
@@ -1319,6 +1266,64 @@ func (pp *portPublisher) updateServer(server *v1beta2.Server, isAdd bool) { | |
} | ||
} | ||
|
||
func (pp *portPublisher) isAddressSelected(address Address, server *v1beta2.Server) bool { | ||
if server == nil { | ||
return false | ||
} | ||
|
||
if address.Pod != nil { | ||
selector, err := metav1.LabelSelectorAsSelector(server.Spec.PodSelector) | ||
if err != nil { | ||
pp.log.Errorf("failed to create Selector: %s", err) | ||
return false | ||
} | ||
|
||
if !selector.Matches(labels.Set(address.Pod.Labels)) { | ||
return false | ||
} | ||
|
||
switch server.Spec.Port.Type { | ||
case intstr.Int: | ||
if server.Spec.Port.IntVal == int32(address.Port) { | ||
return true | ||
} | ||
case intstr.String: | ||
for _, c := range address.Pod.Spec.Containers { | ||
for _, p := range c.Ports { | ||
if p.ContainerPort == int32(address.Port) && p.Name == server.Spec.Port.StrVal { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
|
||
} else if address.ExternalWorkload != nil { | ||
selector, err := metav1.LabelSelectorAsSelector(server.Spec.ExternalWorkloadSelector) | ||
if err != nil { | ||
pp.log.Errorf("failed to create Selector: %s", err) | ||
return false | ||
} | ||
|
||
if !selector.Matches(labels.Set(address.ExternalWorkload.Labels)) { | ||
return false | ||
} | ||
|
||
switch server.Spec.Port.Type { | ||
case intstr.Int: | ||
if server.Spec.Port.IntVal == int32(address.Port) { | ||
return true | ||
} | ||
case intstr.String: | ||
for _, p := range address.ExternalWorkload.Spec.Ports { | ||
if p.Port == int32(address.Port) && p.Name == server.Spec.Port.StrVal { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
//////////// | ||
/// util /// | ||
//////////// | ||
|
Oops, something went wrong.
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.
can we add the same test for external workloads ?
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.
great question. I tried this and then spent forever racking my brain trying to figure out why it wasn't working. it turns out the server_tests all run with the endpoint_watcher in Endpoints mode, not EndpointSlices mode, and external workloads are not compatible with Endpoints.
Given that EndpointSlices mode is the default, we'll want to rewrite these tests to use it, but not in this PR.