Skip to content

Reuse a single dedicated RESTClient and close all connections per check - #1403

Open
bjencks wants to merge 1 commit into
kubernetes:masterfrom
bjencks:cleanup
Open

bjencks wants to merge 1 commit into
kubernetes:masterfrom
bjencks:cleanup

Conversation

@bjencks

@bjencks bjencks commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Calling rest.RESTClientFor on every poll with Proxy != nil creates a new uncached transport each time. When CertFile and KeyFile are configured without inline CertData/KeyData, client-go enables ReloadTLSFiles and starts a new certRotatingDialer and workqueue (4+ background goroutines) per poll, whose cancellation waits for GC via runtime.AddCleanup (or leaks permanently on client-go < v0.36 or when ClientsAllowTLSCacheGC is disabled).

Lazily initialize a single dedicated RESTClient per apiServerMonitor instance (keeping Proxy != nil so tlsTransportCache is skipped even when ClientsAllowTLSCacheGC is disabled) and wrap its dialer with connrotation.NewDialer. Close connections before each check (in case a prior dial completed right after CloseAll) and via LIFO defers on return (cancel checkCtx, call CloseIdleConnectionsFor to evict idle connections from http.Transport's pool, then call connDialer.CloseAll() to actively close any remaining non-idle connections such as timed-out HTTP/2 streams).

@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 1, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @bjencks. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubernetes-prow
kubernetes-prow Bot requested review from hakman and sjenning December 1, 2026 23:00
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: bjencks
Once this PR has been reviewed and has the lgtm label, please assign derekwaynecarr for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow kubernetes-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 1, 2026
Calling rest.RESTClientFor on every poll with Proxy != nil creates a new
uncached transport each time. When CertFile and KeyFile are configured
without inline CertData/KeyData, client-go enables ReloadTLSFiles and
starts a new certRotatingDialer and workqueue (4+ background goroutines)
per poll, whose cancellation waits for GC via runtime.AddCleanup (or
leaks permanently on client-go < v0.36 or when ClientsAllowTLSCacheGC is
disabled).

Lazily initialize a single dedicated RESTClient per apiServerMonitor
instance (keeping Proxy != nil so tlsTransportCache is skipped even when
ClientsAllowTLSCacheGC is disabled) and wrap its dialer with
connrotation.NewDialer. Close connections before each check (in case a
prior dial completed right after CloseAll) and via LIFO defers on return
(cancel checkCtx, call CloseIdleConnectionsFor to evict idle connections
from http.Transport's pool, then call connDialer.CloseAll() to actively
close any remaining non-idle connections such as timed-out HTTP/2
streams).

@SergeyKanzhelev SergeyKanzhelev left a comment

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.

I am ok to lgtm as-is. But thinking more I am wondering if setting proxy doing anything now

Comment on lines 95 to 100
if kubeConfig.Proxy == nil {
// Explicitly set Proxy so client-go's tlsTransportCache (transport/cache.go:
// tlsConfigKey returns uncacheable when Proxy != nil) creates a fresh
// http.Transport on each RESTClientFor call instead of reusing a cached
// transport and its already-established TCP connection. TestCheckAPIServer's
// new-connection assertion acts as a canary if a client-go upgrade changes this.
// Set Proxy to bypass client-go's transport cache (undocumented behavior
// when Proxy != nil). A single RESTClient with a connrotation dialer is
// reused to avoid per-check cert-rotation goroutines and close all sockets.
kubeConfig.Proxy = http.ProxyFromEnvironment
}

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.

nit: Proxy looks redundant with the dedicated dialer that isolates the transport. Are you sure we need it after this change?

@SergeyKanzhelev

Copy link
Copy Markdown
Member

/ok-to-test

@kubernetes-prow kubernetes-prow Bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants