-
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
Reindex outbound policy backends when a service changes #12635
Conversation
Signed-off-by: Alex Leong <alex@buoyant.io>
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.
makes sense to me
for routes in self.service_port_routes.values_mut() { | ||
for routes in routes.watches_by_ns.values_mut() { | ||
for route in routes.routes.values_mut() { | ||
for rule in route.rules.iter_mut() { | ||
for backend in rule.backends.iter_mut() { |
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 out of curiosity, I assume there's effectively no better way to accomplish what needs doing here without nesting for loops this deeply, is there?
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 way to avoid nesting for loops would be to do something like:
for routes in self
.service_port_routes
.values_mut()
.flat_map(|routes| routes.watches_by_ns.values_mut())
{
routes
.routes
.values_mut()
.flat_map(|route| route.rules.iter_mut())
.flat_map(|rule| rule.backends.iter_mut())
.for_each(|backend| {
if let Backend::Service(svc) = backend {
let service_ref = ServiceRef {
name: svc.name.clone(),
namespace: svc.namespace.clone(),
};
svc.exists = service_info.contains_key(&service_ref);
}
});
routes.send_if_modified()
}
but I don't think that's clearer than the nested for loops in this case.
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.
You're certainly right about clarity 😂. Any idea if there's a meaningful difference in the compiled binary? It absolutely should't be a blocker to merging, by all means "looks good, ship it"-stamped. Just curious if it could be avoided or if maybe clang will take care of it either way.
If an HTTPRoute references a backend service that does not exist, the policy controller synthesizes a FailureInjector in the outbound policy so that requests to that backend will fail with a 500 status code. However, we do not update the policy when backend services are created or deleted, which can result in an outbound policy that synthesizes 500s for backends, even if the backend currently exists (or vice versa).
This is often papered over because when a backend service is created or deleted, this will trigger the HTTPRoute's ResolvedRef status condition to change which will cause a reindex of the HTTPRotue and a recomputation of the backends. However, depending on the specific order that these updates are processed, the outbound policy can still be left with the incorrect backend state.
In order to be able to update the backend of an outbound policy when backend services are created or deleted, we change the way these backends are represented in the index. Previously, we had represented backends which were services that did not exist as
Backend::Invalid
. However, this discards the necessary backend information necessary to recreate the backend if the service is created. Instead, we update this to represent these backends as aBackend::Service
but with a new fieldexists
set to false. This allows us to update this field as backend services are created or deleted.