10000 [ISSUE #13345] fix apiClient readTimeout must be zero by 2200376luhuabin · Pull Request #13362 · alibaba/nacos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[ISSUE #13345] fix apiClient readTimeout must be zero #13362

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

Merged
merged 3 commits into from
May 14, 2025

Conversation

2200376luhuabin
Copy link
Contributor

#13345

What is the purpose of the change

  • fix apiClient readTimeout must be zero

Brief changelog

  • get apiClient from ClientBuilder like getOutsideApiClient method

Copy link

Thanks for your this PR. 🙏
Please check again for your PR changes whether contains any usage/api/configuration change such as Add new API , Add new configuration, Change default value of configuration.
If so, please add or update documents(markdown type) in docs/next/ for repository nacos-group/nacos-group.github.io


感谢您提交的PR。 🙏
请再次查看您的PR内容,确认是否包含任何使用方式/API/配置参数的变更,如:新增API新增配置参数修改默认配置等操作。
如果是,请确保在提交之前,在仓库nacos-group/nacos-group.github.io中的docs/next/目录下添加或更新文档(markdown格式)。

Copy link
lingma-agents bot commented May 12, 2025

修复ApiClient配置并统一全局默认设置

变更文件

文件路径 变更说明
k8s-sync/​src/​main/​java/​com/​alibaba/​nacos/​k8s/​sync/​K8sSyncServer.java 重构ApiClient初始化逻辑,移除外置集群配置中的冗余代码,统一通过ClientBuilder创建并设置全局默认ApiClient,简化kubeconfig加载实现

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @Lingma-Agent 分析这个方法的性能瓶颈并提供优化建议。

  • @Lingma-Agent 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @Lingma-Agent 请总结上述讨论并提出解决方案。

  • @Lingma-Agent 请根据讨论内容生成优化代码。

Copy link
@lingma-agents lingma-agents bot left a comment

Choose a reason for hiding this comment

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

🔍 代码评审报告

🎯 评审意见概览

严重度 数量 说明
🔴 Blocker 0 阻断性问题,需立即修复。例如:系统崩溃、关键功能不可用或严重安全漏洞。
🟠 Critical 1 严重问题,高优先级修复。例如:核心功能异常或性能瓶颈影响用户体验。
🟡 Major 1 主要问题,建议修复。例如:非核心功能缺陷或代码维护性较差。
🟢 Minor 0 次要问题,酌情优化。例如:代码格式不规范或注释缺失。

总计: 2 个问题


📋 评审意见详情

💡 单文件建议

以下是文件级别的代码建议,聚焦于代码的可读性、可维护性和潜在问题。
☕ k8s-sync/src/main/java/com/alibaba/nacos/k8s/sync/K8sSyncServer.java (2 💬)

🚀 跨文件建议

以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍 1. 默认ApiClient设置路径不一致可能导致配置失效

在in-cluster模式和outside模式下,ApiClient的默认配置设置逻辑被分散到两个不同位置。当通过getOutsideApiClient()创建ApiClient时,未在该方法内设置默认ApiClient(原代码中存在该逻辑但被移除),而统一在startInformer()方法中设置。这可能导致以下问题:

  1. 若其他创建ApiClient的路径未经过startInformer()方法,将无法设置默认配置
  2. 全局状态修改存在隐藏依赖风险,其他组件可能在未初始化时使用未设置的默认ApiClient
  3. 核心V1API的创建顺序存在潜在问题,应在设置默认ApiClient后再初始化API对象
    建议将默认 10000 ApiClient设置逻辑与ApiClient创建逻辑耦合,避免跨方法的全局状态修改。

📌 关键代码:

+        Configuration.setDefaultApiClient(apiClient);
return ClientBuilder.kubeconfig(...).build();

⚠️ 潜在风险: - 其他组件可能因未及时设置默认ApiClient而使用错误配置

  • 线程安全问题:全局状态修改可能导致多线程环境下配置污染
  • 核心API对象可能在未设置默认配置时初始化,引发连接错误
🔍 2. 核心API对象初始化顺序存在潜在风险

在startInformer()方法中,coreV1Api的初始化被提前到设置默认ApiClient之前。虽然当前代码通过显式设置apiClient解决了问题,但存在设计隐患:

  1. CoreV1Api的构造函数可能依赖默认ApiClient配置
  2. 若后续修改构造顺序可能导致回归问题
  3. 未保持与原有代码中"in-cluster模式"下的初始化逻辑一致性
    建议将所有API对象的初始化统一放在设置默认ApiClient之后,确保构造时能正确获取配置。

📌 关键代码:

+        coreV1Api = new CoreV1Api();

⚠️ 潜在风险: - API对象可能在未配置正确客户端时初始化,导致后续操作失败

  • 构造顺序修改可能引发隐蔽的配置错误
🔍 3. 全局ApiClient配置存在单点故障风险

系统关键功能完全依赖全局的默认ApiClient配置,这种设计存在以下问题:

  1. 不支持多环境/多配置同时存在的情况
  2. 全局状态修改可能导致不可预测的副作用
  3. 与微服务架构中推荐的依赖注入模式相悖
    建议采用依赖注入方式将ApiClient作为参数传递,或使用IoC容器管理配置实例。

⚠️ 潜在风险: - 系统扩展时无法支持多集群/多环境接入

  • 全局状态修改可能引发不可重现的间歇性故障
  • 维护成本随系统复杂度增长呈指数级上升

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @Lingma-Agent 分析这个方法的性能瓶颈并提供优化建议。

  • @Lingma-Agent 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @Lingma-Agent 请总结上述讨论并提出解决方案。

  • @Lingma-Agent 请根据讨论内容生成优化代码。

} else {
coreV1Api = new CoreV1Api();
apiClient = coreV1Api.getApiClient();
apiClient = ClientBuilder.defaultClient();
Copy link

Choose a reason for hiding this comment

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

在in-cluster模式下使用ClientBuilder.defaultClient()可能导致配置错误

🟠 Critical | 🐞 Bugs

📋 问题详情

在startInformer方法的else分支中,当k8sSyncConfig.isOutsideCluster()为false时,直接使用ClientBuilder.defaultClient()初始化apiClient可能未正确配置in-cluster环境。in-cluster模式通常需要通过ClientBuilder.cluster()方法创建客户端,而非defaultClient,否则可能导致无法连接到集群或配置不生效。

💡 解决方案

将ClientBuilder.defaultClient()替换为ClientBuilder.cluster().build():

- apiClient = ClientBuilder.defaultClient();
+ apiClient = ClientBuilder.cluster().build();
🔧 建议代码

‼️AI 生成代码 - 请在应用前检查逻辑、规范并测试

Suggested change
apiClient = ClientBuilder.defaultClient();
apiClient = ClientBuilder.cluster().build();

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

// set the global default api-client to the in-cluster one from above
Configuration.setDefaultApiClient(apiClient);
return apiClient;
return ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();
Copy link

Choose a reason for hiding this comment

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

getOutsideApiClient方法未保留设置默认ApiClient的逻辑

🟡 Major | 🧹 Code Smells

📋 问题详情

在getOutsideApiClient方法中移除了对Configuration.setDefaultApiClient的调用,但startInformer方法中虽然补充了该逻辑,但存在潜在风险:若该方法被其他代码路径调用(如单元测试),可能导致默认ApiClient未被正确设置。

💡 解决方案

在返回前补充设置默认ApiClient:

+ Configuration.setDefaultApiClient(apiClient);
  return apiClient;

但需注意该修改需要保留原有代码结构,因此完整的修复应:

- return ClientBuilder.kubeconfig(...).build();
+ ApiClient apiClient = ClientBuilder.kubeconfig(...).build();
+ Configuration.setDefaultApiClient(apiClient);
+ return apiClient;

您的反馈对我们很重要!(建议右键在新标签页中打开以下链接)

有用意见👍无用意见👎错误意见❌

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.32%. Comparing base (4c29458) to head (52f0026).
Report is 4 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##             develop   #13362      +/-   ##
=============================================
+ Coverage      68.30%   70.32%   +2.01%     
- Complexity     11418    11712     +294     
=============================================
  Files           1592     1592              
  Lines          51104    50984     -120     
  Branches        5166     5149      -17     
=============================================
+ Hits           34906    35853     +947     
+ Misses         13760    12703    -1057     
+ Partials        2438     2428      -10     

see 82 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8ecdd5d...52f0026. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@KomachiSion KomachiSion merged commit d15272c into alibaba:develop May 14, 2025
3 checks passed
@wuyfee
Copy link
wuyfee commented May 14, 2025

$\color{red}{FAILURE}$
DETAILS
✅ - docker: success
❌ - deploy (standalone & cluster & standalone_auth): failure
❌ - e2e-java-test (standalone & cluster & standalone_auth): skipped
❌ - e2e-go-test (standalone & cluster): skipped
❌ - e2e-cpp-test (standalone & cluster): skipped
❌ - e2e-csharp-test (standalone & cluster): skipped
❌ - e2e-nodejs-test (standalone & cluster): skipped
❌ - e2e-python-test (standalone & cluster): skipped
✅ - clean (standalone & cluster & standalone_auth): success

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.

4 participants
0