Skip to content

Use WatchListXXXForAllNamespacesAsync to list Kubernetes resources and refactor WatchAsync#3024

Open
Xulei-NL wants to merge 15 commits into
dotnet:mainfrom
Xulei-NL:refactor/asyncwatch-k8s-resources
Open

Use WatchListXXXForAllNamespacesAsync to list Kubernetes resources and refactor WatchAsync#3024
Xulei-NL wants to merge 15 commits into
dotnet:mainfrom
Xulei-NL:refactor/asyncwatch-k8s-resources

Conversation

@Xulei-NL
Copy link
Copy Markdown

@Xulei-NL Xulei-NL commented Apr 28, 2026

This PR

  1. Replace WatchListXXXForAllNamespaces with WatchListXXXForAllNamespacesAsync. For instace WatchListEndpointsForAllNamespaces with WatchListEndpointsForAllNamespacesAsync. If so, we could get watch results asynchrounously.
  2. Refactor WatchAsync to handle returned IAsyncEnumerable(<WatchEventType, TResource>) in step 1. In this way we could remove TaskCompletionSource which simulates a Task and rely on await foreach.

Appreciate every feedback!

Closes #3008

@Xulei-NL
Copy link
Copy Markdown
Author

The PR for the issue #3008.

@Xulei-NL
Copy link
Copy Markdown
Author

@dotnet-policy-service agree

Copy link
Copy Markdown
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

Comment thread src/Kubernetes.Controller/Client/V1IngressResourceInformer.cs Outdated
Comment thread src/Kubernetes.Controller/Client/ResourceInformer.cs Outdated
Comment thread src/Kubernetes.Controller/Client/ResourceInformer.cs Outdated
Comment thread src/Kubernetes.Controller/Client/ResourceInformer.cs Outdated
Comment thread src/Kubernetes.Controller/Client/ResourceInformer.cs Outdated
Comment on lines +292 to +295
// Used as an atomic boolean to ensure the re-connect cancellation path is triggered only once.
// 0 means cancellation has not been requested; 1 means cancellation has already been requested.
// Access only through Interlocked operations.
public int CancellationRequested;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? You're only calling .Cancel() on the CTS, there's no harm in doing so multiple times

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear @MihaZupan ,

indeed calling .Cancel() many times has no harm in the timer. The code currently on the master branch uses lastEventUtc = DateTime.MaxValue; to make sure the cancellation happened once. See https://github.com/dotnet/yarp/pull/3024/changes/BASE..1462c1a1f4822994206edbb8084a65fe8bb93be0#diff-d890e91050758eee4329344d227734ac5b1d3e99f2e27a9dead7c564f206c8acL339.

I regarded this PR as a refactoring PR so I wanted to make sure the log below was only printed once:

                    Logger.LogDebug(
                        EventId(EventType.DisposingToReconnect),
                        "Disposing watcher for {ResourceType} to cause reconnect.",
                        typeof(TResource).Name);

I'm also totally fine if we remove the public int CancellationRequested. Do you think we need to remove it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'd remove it. If your cancellation took 45 seconds to kick in your process is very dead anyway

Copy link
Copy Markdown
Author

@Xulei-NL Xulei-NL May 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear. I have removed it and meanwhile refactored the comments. Will request you a new review now.

@Xulei-NL Xulei-NL requested a review from MihaZupan May 28, 2026 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Kubernetes.Controller] Switch from WatchListXXX to WatchListXXXAsync in WatchResourceListAsync

2 participants