1.42
This commit is contained in:
parent
f619654b1a
commit
640cbf9c99
@ -21,6 +21,9 @@ import io.kubernetes.client.util.Config;
|
||||
import io.kubernetes.client.util.Yaml;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.StringReader;
|
||||
@ -598,8 +601,8 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp
|
||||
// 日志大小限制:10MB(防止OOM)
|
||||
Integer limitBytes = 10 * 1024 * 1024;
|
||||
|
||||
// 查询Pod日志
|
||||
String logs = api.readNamespacedPodLog(
|
||||
// 使用底层Call API + try-with-resources确保Response正确关闭,避免OkHttp连接泄漏
|
||||
Call call = api.readNamespacedPodLogCall(
|
||||
podName, // Pod名称
|
||||
namespace, // 命名空间
|
||||
container, // 容器名称(可选)
|
||||
@ -610,12 +613,23 @@ public class K8sServiceIntegrationImpl extends BaseExternalSystemIntegration imp
|
||||
false, // previous(是否查询上一个容器的日志)
|
||||
effectiveSinceSeconds, // sinceSeconds(使用智能默认值)
|
||||
effectiveTail, // tail(使用智能默认值)
|
||||
true // timestamps(必须启用,用于引用点系统)
|
||||
true, // timestamps(必须启用,用于引用点系统)
|
||||
null // callback
|
||||
);
|
||||
|
||||
int logLength = logs != null ? logs.length() : 0;
|
||||
log.info("查询Pod日志成功,日志长度: {} bytes, 行数约: {}", logLength, logLength > 0 ? logs.split("\n").length : 0);
|
||||
return logs != null ? logs : "";
|
||||
// 执行调用并使用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;
|
||||
log.info("查询Pod日志成功,日志长度: {} bytes, 行数约: {}", logLength, logLength > 0 ? logs.split("\n").length : 0);
|
||||
return logs != null ? logs : "";
|
||||
}
|
||||
|
||||
} catch (ApiException e) {
|
||||
if (e.getCode() == 404) {
|
||||
|
||||
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user