Skip to content

🐛 Share OpenStack HTTP transports by TLS trust config to stop connection leaks - #3255

Open
bartekle wants to merge 1 commit into
kubernetes-sigs:mainfrom
bartekle:fix/scope-transport-leak
Open

🐛 Share OpenStack HTTP transports by TLS trust config to stop connection leaks#3255
bartekle wants to merge 1 commit into
kubernetes-sigs:mainfrom
bartekle:fix/scope-transport-leak

Conversation

@bartekle

@bartekle bartekle commented Jul 7, 2026

Copy link
Copy Markdown

pkg/scope cached one *http.Transport per credential (via the scope LRU cache), but evicting or refreshing a scope never closed it. net/http has no finalizer on Transport, so an evicted scope's idle keep-alive connections were never released - only accumulated - for the lifetime of the manager process. With one credential per cluster and a small scope cache, this showed up as tens of thousands of stale established connections across a fleet of clusters.

Key transports by TLS trust config (CA bundle + verify mode) instead of by credential: that dimension is low-cardinality and stable across a fleet against the same cloud(s), so it can be cached in a small, explicitly-evicting LRU (closing idle connections on overflow) without reintroducing the leak. The credential-level scope cache can now evict freely since it no longer owns an exclusive connection pool.

Also raises MaxIdleConnsPerHost to 25 since this transport is now shared by every credential in the fleet against a given host, and adds --transport-cache-max-size to make the new cache's bound configurable.

What this PR does / why we need it:

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #3227

Special notes for your reviewer:

  1. Please confirm that if this PR changes any image versions, then that's the sole change this PR makes.

TODOs:

  • squashed commits
  • if necessary:
    • includes documentation
    • adds unit tests

/hold

…n leaks

pkg/scope cached one *http.Transport per credential (via the scope LRU
cache), but evicting or refreshing a scope never closed it. net/http has
no finalizer on Transport, so an evicted scope's idle keep-alive
connections were never released - only accumulated - for the lifetime of
the manager process. With one credential per cluster and a small scope
cache, this showed up as tens of thousands of stale established
connections across a fleet of clusters.

Key transports by TLS trust config (CA bundle + verify mode) instead of
by credential: that dimension is low-cardinality and stable across a
fleet against the same cloud(s), so it can be cached in a small,
explicitly-evicting LRU (closing idle connections on overflow) without
reintroducing the leak. The credential-level scope cache can now evict
freely since it no longer owns an exclusive connection pool.

Also raises MaxIdleConnsPerHost (2 -> 25, matching client-go's
tlsTransportCache) since this transport is now shared by every credential
in the fleet against a given host, and adds --transport-cache-max-size to
make the new cache's bound configurable.
@kubernetes-prow kubernetes-prow Bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 7, 2026
@netlify

netlify Bot commented Jul 7, 2026

Copy link
Copy Markdown

Deploy Preview for kubernetes-sigs-cluster-api-openstack ready!

Name Link
🔨 Latest commit b14e1d4
🔍 Latest deploy log https://app.netlify.com/projects/kubernetes-sigs-cluster-api-openstack/deploys/6a4cfa627f5cb70008e4a1c9
😎 Deploy Preview https://deploy-preview-3255--kubernetes-sigs-cluster-api-openstack.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 7, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: bartekle / name: Bartosz Leszczynski (b14e1d4)

@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign emilienm 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 requested review from EmilienM and nikParasyr July 7, 2026 13:08
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @bartekle!

It looks like this is your first PR to kubernetes-sigs/cluster-api-provider-openstack 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/cluster-api-provider-openstack has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow kubernetes-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 7, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @bartekle. Thanks for your PR.

I'm waiting for a kubernetes-sigs 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 added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 7, 2026
@bartekle

bartekle commented Jul 7, 2026

Copy link
Copy Markdown
Author

i tried this version on my staging cluster with ~80 clusters and it seems like it helps with number of connections

ss -tan | awk 'NR>1 {print $1}' | sort | uniq -c | sort -rn
     11 TIME-WAIT
      6 ESTAB
      3 LISTEN

@nikParasyr

Copy link
Copy Markdown
Contributor

@bartekle could you please sign the cla as documented here: https://github.com/kubernetes-sigs/cluster-api-provider-openstack/blob/main/CONTRIBUTING.md#sign-the-cla and also update PR title with the correct emoji

@bartekle bartekle changed the title Share OpenStack HTTP transports by TLS trust config to stop connection leaks 🐛 Share OpenStack HTTP transports by TLS trust config to stop connection leaks Jul 10, 2026
@bartekle bartekle changed the title 🐛 Share OpenStack HTTP transports by TLS trust config to stop connection leaks 🐛 Share OpenStack HTTP transports by TLS trust config to stop connection leaks Jul 10, 2026
@bartekle

Copy link
Copy Markdown
Author

i will try to deal with CLA asap

@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jul 20, 2026
@nikParasyr

Copy link
Copy Markdown
Contributor

/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 Jul 22, 2026

@lentzi90 lentzi90 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the PR!
I am a bit concerned with the complexity of this. Package level state (cache), mutex, changes to exported package signatures (NewFactory) and a new flag.
Do we need all this to fix the connection leak? Mainly I wonder if we could simply set IdleConnTimeout on the existing transport and skip all the caching changes/additions? The caching seems like a separate concern to me and should not be strictly needed for fixing the bug.

Comment thread pkg/scope/provider.go
Comment on lines -273 to -274

provider.HTTPClient.Transport = &http.Transport{Proxy: http.ProxyFromEnvironment, TLSClientConfig: config}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What if we simply set IdleConnTimeout here?

provider.HTTPClient.Transport = &http.Transport{
  Proxy: http.ProxyFromEnvironment, TLSClientConfig: config
  IdleConnTimeout:                                   90 * time.Second
}

Maybe also MaxIdleConnsPerHost?

Comment on lines +58 to +60
// None of these tests use t.Parallel(): they share the package-level,
// size-bounded transportCache, and running them concurrently would let one
// test's insertions evict entries another test is mid-assertion on.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do not like the sound of this. I think we need to consider alternatives. Could the cache be stored in the providerScopeFactory together with the clientCache instead?

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. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. 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

Status: Inbox

3 participants