This commit is contained in:
dengqichen 2025-12-18 10:14:59 +08:00
parent f619654b1a
commit 640cbf9c99
2 changed files with 1002 additions and 29 deletions

View File

@ -21,6 +21,9 @@ import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.Yaml; import io.kubernetes.client.util.Yaml;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.StringReader; import java.io.StringReader;
@ -598,8 +601,8 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp
// 日志大小限制10MB防止OOM // 日志大小限制10MB防止OOM
Integer limitBytes = 10 * 1024 * 1024; Integer limitBytes = 10 * 1024 * 1024;
// 查询Pod日志 // 使用底层Call API + try-with-resources确保Response正确关闭避免OkHttp连接泄漏
String logs = api.readNamespacedPodLog( Call call = api.readNamespacedPodLogCall(
podName, // Pod名称 podName, // Pod名称
namespace, // 命名空间 namespace, // 命名空间
container, // 容器名称可选 container, // 容器名称可选
@ -610,12 +613,23 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp
false, // previous是否查询上一个容器的日志 false, // previous是否查询上一个容器的日志
effectiveSinceSeconds, // sinceSeconds使用智能默认值 effectiveSinceSeconds, // sinceSeconds使用智能默认值
effectiveTail, // tail使用智能默认值 effectiveTail, // tail使用智能默认值
true // timestamps必须启用用于引用点系统 true, // timestamps必须启用用于引用点系统
null // callback
); );
// 执行调用并使用try-with-resources自动关闭Response
try (Response response = call.execute()) {
ResponseBody body = response.body();
if (body == null) {
log.warn("查询Pod日志返回空body: {}/{}", namespace, podName);
return "";
}
String logs = body.string();
int logLength = logs != null ? logs.length() : 0; int logLength = logs != null ? logs.length() : 0;
log.info("查询Pod日志成功日志长度: {} bytes, 行数约: {}", logLength, logLength > 0 ? logs.split("\n").length : 0); log.info("查询Pod日志成功日志长度: {} bytes, 行数约: {}", logLength, logLength > 0 ? logs.split("\n").length : 0);
return logs != null ? logs : ""; return logs != null ? logs : "";
}
} catch (ApiException e) { } catch (ApiException e) {
if (e.getCode() == 404) { if (e.getCode() == 404) {

File diff suppressed because one or more lines are too long