Skip to content

Use Configuration.socket_options instead of monkey-patching urllib3 in cncf-kubernetes - #68557

Closed
anmolxlight wants to merge 8 commits into
apache:mainfrom
anmolxlight:fix-tcp-keepalive-urllib3-v2
Closed

anmolxlight wants to merge 8 commits into
apache:mainfrom
anmolxlight:fix-tcp-keepalive-urllib3-v2

Conversation

@anmolxlight

@anmolxlight anmolxlight commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

The kubernetes_executor.enable_tcp_keepalive config option was relying on monkey-patching urllib3's default_socket_options after module import. In urllib3 v2.x, the socket_options parameter of HTTPConnection.__init__ is evaluated as a default argument at import time, so changing the class attribute afterwards has no effect on newly created connections. This meant the TCP keepalive configuration was silently a no-op.

This PR fixes the issue by passing socket options through the Kubernetes client's Configuration.socket_options field, which is properly threaded through:

ApiClient -> RESTClientObject.__init__ -> urllib3.PoolManager -> HTTPConnectionPool -> HTTPConnection.__init__

The kubernetes-client/python library already supports this: Configuration has a socket_options attribute that is checked in RESTClientObject.__init__ and forwarded to urllib3.PoolManager as a keyword argument.

Changes

  • kube_client.py: _enable_tcp_keepalive() now accepts a Configuration object and sets configuration.socket_options directly instead of monkey-patching urllib3. get_kube_client() calls it after obtaining the configuration object.
  • kubernetes_engine.py: Passes configuration through to _enable_tcp_keepalive
  • tests: Updated test_enable_tcp_keepalive to verify configuration.socket_options is set correctly

Notes

  • Includes TCP_NODELAY in the socket options to preserve the default urllib3 behavior of disabling Nagle's algorithm

Related issue

Closes: #68396

Reproduction

See https://github.com/jonminter-dojo/airflow-k8s-tcp-keepalive-repro for a minimal reproduction demonstrating the bug with the old approach.


Important

🛠️ Maintainer triage note for @anmolxlight · by @potiuk · 2026-07-15 16:20 UTC

The ready for maintainer review label was removed — the next move is yours (see our Pull Request quality criteria):

  • ❌ Other failing CI checks. See docs.
  • ❌ Build docs. See docs.
  • ❌ Provider tests. See docs.

The ball is in your court — you've been assigned to this PR. Fix the failing checks, then mark it Ready for review again.

Automated triage — may be imperfect; a maintainer takes the next look.

@boring-cyborg boring-cyborg Bot added area:providers provider:cncf-kubernetes Kubernetes (k8s) provider related issues provider:google Google (including GCP) related issues labels Jun 15, 2026
@anmolxlight
anmolxlight force-pushed the fix-tcp-keepalive-urllib3-v2 branch 2 times, most recently from 27a9bca to fa68e17 Compare June 15, 2026 20:03
…key-patching urllib3

also: add backward compat for kubernetes < 36.0, fix tests for new _enable_tcp_keepalive signature
@anmolxlight
anmolxlight force-pushed the fix-tcp-keepalive-urllib3-v2 branch from 10c8abb to 935d4b2 Compare June 16, 2026 15:36
@shahar1 shahar1 changed the title fix(cncf-kubernetes): use Configuration.socket_options instead of monkey-patching urllib3 Use Configuration.socket_options instead of monkey-patching urllib3 in cncf-kubernetes Jun 16, 2026
@potiuk potiuk added the ready for maintainer review Set by triage when all criteria pass, or by the fork CI reconciler when fork CI is green. label Jun 17, 2026
…ation shim

- Rename _enable_tcp_keepalive(configuration) to enable_tcp_keepalive(configuration)
- Keep _enable_tcp_keepalive as a deprecated wrapper
- Add # use next version to cncf.kubernetes pyproject.toml
- Update all internal callers and tests to use the public function
@shahar1
shahar1 self-requested a review July 2, 2026 07:11

@shahar1 shahar1 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.

Could you please resolve conflicts?

The PR moves enable_tcp_keepalive from get_conn_body() into
_TimeoutK8sApiClient.__init__(), but uses an inline import that
resolves to the original kube_client function, bypassing the test's
mock patch at hooks.kubernetes.enable_tcp_keepalive.

Fix: rename parameter to should_enable_tcp_keepalive to avoid
shadowing the imported function, so the mock is correctly resolved.
Also update test_prefixed_names_still_work assertion signature.
@anmolxlight
anmolxlight force-pushed the fix-tcp-keepalive-urllib3-v2 branch from 66d8cab to 5c81544 Compare July 2, 2026 18:53
@potiuk potiuk removed the ready for maintainer review Set by triage when all criteria pass, or by the fork CI reconciler when fork CI is green. label Jul 15, 2026
anmolxlight added 3 commits July 15, 2026 16:34
The new public enable_tcp_keepalive() function (previously private
_enable_tcp_keepalive) exposed its docstring to the API docs spellcheck
for the first time. The private function was excluded from docs builds,
so these words never triggered before.
These GitHub usernames appear in the enable_tcp_keepalive() docstring
under 'ping @user for modifications'. Previously hidden from spellcheck
by private _ prefix. Now public function exposes them.
@potiuk potiuk added the ready for maintainer review Set by triage when all criteria pass, or by the fork CI reconciler when fork CI is green. label Jul 18, 2026
@eladkal
eladkal requested a review from shahar1 July 21, 2026 06:14
@shahar1 shahar1 removed the ready for maintainer review Set by triage when all criteria pass, or by the fork CI reconciler when fork CI is green. label Jul 25, 2026

@shahar1 shahar1 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.

Could you please add AI disclosure to the PR body? Thank you!

@shahar1

shahar1 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Closing as stale, I'll create a new PR to fix it based on this one

@shahar1 shahar1 closed this Oct 17, 2026
@shahar1 shahar1 added the stale Stale PRs per the .github/workflows/stale.yml policy file label Oct 17, 2026
shahar1 pushed a commit to shahar1/airflow that referenced this pull request Oct 20, 2026
…n cncf-kubernetes

In urllib3 2.x, HTTPConnection.__init__ binds socket_options to
default_socket_options as a default argument at import time, so
reassigning the class attribute later never reaches new connections and
[kubernetes_executor] enable_tcp_keepalive was silently a no-op.

Pass the options through the Kubernetes client's Configuration.socket_options,
which RESTClientObject forwards to urllib3.PoolManager.

Closes: apache#68396
Supersedes: apache#68557
shahar1 pushed a commit to shahar1/airflow that referenced this pull request Oct 21, 2026
…n cncf-kubernetes

In urllib3 2.x, HTTPConnection.__init__ binds socket_options to
default_socket_options as a default argument at import time, so
reassigning the class attribute later never reaches new connections and
[kubernetes_executor] enable_tcp_keepalive was silently a no-op.

Pass the options through the Kubernetes client's Configuration.socket_options,
which RESTClientObject forwards to urllib3.PoolManager.

Closes: apache#68396
Supersedes: apache#68557
henry3260 pushed a commit that referenced this pull request Oct 21, 2026
* Use Configuration.socket_options instead of monkey-patching urllib3 in cncf-kubernetes

In urllib3 2.x, HTTPConnection.__init__ binds socket_options to
default_socket_options as a default argument at import time, so
reassigning the class attribute later never reaches new connections and
[kubernetes_executor] enable_tcp_keepalive was silently a no-op.

Pass the options through the Kubernetes client's Configuration.socket_options,
which RESTClientObject forwards to urllib3.PoolManager.

Closes: #68396
Supersedes: #68557

* Address review findings for cncf.kubernetes TCP keepalive fix

- Mark the google cncf.kubernetes floor with 'use next version'.
- enable_tcp_keepalive: drop the urllib3 monkey-patch fallback; warn and return when Configuration has no socket_options.
- Merge keepalive options into existing socket_options (or urllib3 defaults) without duplicates.
- Deprecated _enable_tcp_keepalive() now sets the default Configuration.
- Tidy docstrings and revert the spelling_wordlist additions.
- Add tests for merging, idempotency, unsupported clients, get_kube_client, _TimeoutK8sApiClient, the deprecated helper and the hook; use autospec in hook tests.

* Require kubernetes client 36.0.1 for TCP keepalive support

Configuration.socket_options, the only way to apply keepalive options with urllib3 2.x, does not exist in kubernetes 35.0.0, so on that floor keepalive could only ever be logged as unavailable and the lowest-dependency tests failed on it.

* Parametrize cncf.kubernetes TCP keepalive client tests

Cover custom tcp_keep_idle/intvl/cnt values, and exercise
_TimeoutK8sApiClient with and without a supplied configuration and
disable_verify_ssl, asserting the global default stays unchanged.

* Align TCP keepalive kwarg naming in cncf.kubernetes client

Rename the _TimeoutK8sApiClient and KubernetesHook kwarg
should_enable_tcp_keepalive to enable_tcp_keepalive, matching
disable_verify_ssl and the GKE hook. The unreleased public helper
becomes configure_tcp_keepalive so the kwarg does not shadow it.
The socket-options test now derives urllib3's defaults instead of
hard-coding TCP_NODELAY.

Also restore uv.lock revision 5 from main, which an older local uv
had rewritten to 3.

---------

Co-authored-by: anmolxlight <anmolx.work@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:cncf-kubernetes Kubernetes (k8s) provider related issues provider:google Google (including GCP) related issues stale Stale PRs per the .github/workflows/stale.yml policy file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kubernetes provider kubernetes_executor.enable_tcp_keepalive does not actually enable TCP keepalives

3 participants