Skip to content
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

Merged
merged 2 commits into from
May 23, 2024

Conversation

adleong
Copy link
Member

@adleong adleong commented May 21, 2024

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 a Backend::Service but with a new field exists set to false. This allows us to update this field as backend services are created or deleted.

Signed-off-by: Alex Leong <alex@buoyant.io>
Copy link
Member

@olix0r olix0r left a 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

policy-controller/grpc/src/outbound.rs Outdated Show resolved Hide resolved
policy-controller/k8s/index/src/outbound/tests.rs Outdated Show resolved Hide resolved
Signed-off-by: Alex Leong <alex@buoyant.io>
Comment on lines +321 to +325
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() {
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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.

@adleong adleong merged commit d81b1f0 into main May 23, 2024
27 checks passed
@adleong adleong deleted the alex/backend-reindex branch May 23, 2024 21:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants