1.30 k8s pods查询
This commit is contained in:
parent
53ff53738d
commit
39c116a608
@ -7,10 +7,13 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* K8S日志解析工具类
|
* K8S日志解析工具类
|
||||||
* 基于Kubernetes Dashboard的引用点系统实现
|
* 参考Kubernetes Dashboard实现
|
||||||
|
* https://github.com/kubernetes/dashboard/blob/master/modules/api/pkg/resource/logs/logs.go
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class K8sLogParser {
|
public class K8sLogParser {
|
||||||
@ -18,9 +21,18 @@ public class K8sLogParser {
|
|||||||
private static final String NEWEST = "newest";
|
private static final String NEWEST = "newest";
|
||||||
private static final String OLDEST = "oldest";
|
private static final String OLDEST = "oldest";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* K8s日志时间戳正则表达式
|
||||||
|
* 匹配RFC3339格式:2025-12-13T23:27:18.124567890Z
|
||||||
|
* 或常规格式:2025-12-13 23:27:18.124
|
||||||
|
*/
|
||||||
|
private static final Pattern TIMESTAMP_PATTERN = Pattern.compile(
|
||||||
|
"^(\\d{4}-\\d{2}-\\d{2}[T ]\\d{2}:\\d{2}:\\d{2}\\.\\d+Z?)\\s"
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析K8S原始日志为日志行列表
|
* 解析K8S原始日志为日志行列表
|
||||||
* K8S日志格式:2025-12-13T23:00:01.123456789Z log content here
|
* 参考:logs.go ToLogLines函数
|
||||||
*
|
*
|
||||||
* @param rawLogs 原始日志字符串
|
* @param rawLogs 原始日志字符串
|
||||||
* @return 日志行列表
|
* @return 日志行列表
|
||||||
@ -38,14 +50,14 @@ public class K8sLogParser {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找第一个空格,分隔时间戳和内容
|
// 使用正则表达式提取时间戳
|
||||||
int spaceIndex = line.indexOf(' ');
|
Matcher matcher = TIMESTAMP_PATTERN.matcher(line);
|
||||||
if (spaceIndex > 0 && spaceIndex < line.length() - 1) {
|
if (matcher.find()) {
|
||||||
String timestamp = line.substring(0, spaceIndex);
|
String timestamp = matcher.group(1);
|
||||||
String content = line.substring(spaceIndex + 1);
|
String content = line.substring(matcher.end());
|
||||||
lines.add(new K8sLogLine(timestamp, content));
|
lines.add(new K8sLogLine(timestamp, content));
|
||||||
} else {
|
} else {
|
||||||
// 如果没有时间戳,使用空字符串作为时间戳
|
// 没有时间戳的日志行
|
||||||
lines.add(new K8sLogLine("", line));
|
lines.add(new K8sLogLine("", line));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,6 +67,7 @@ public class K8sLogParser {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 查找引用点在日志列表中的索引
|
* 查找引用点在日志列表中的索引
|
||||||
|
* 参考:logs.go getLineIndex函数
|
||||||
*
|
*
|
||||||
* @param lines 日志行列表
|
* @param lines 日志行列表
|
||||||
* @param referenceTimestamp 引用点时间戳
|
* @param referenceTimestamp 引用点时间戳
|
||||||
@ -74,28 +87,43 @@ public class K8sLogParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 使用二分查找定位时间戳
|
// 使用二分查找定位时间戳
|
||||||
int index = Collections.binarySearch(lines, new K8sLogLine(referenceTimestamp, ""),
|
int matchingStartedAt = binarySearchTimestamp(lines, referenceTimestamp);
|
||||||
(a, b) -> a.getTimestamp().compareTo(b.getTimestamp()));
|
|
||||||
|
|
||||||
if (index >= 0) {
|
if (matchingStartedAt < 0 || matchingStartedAt >= lines.size()) {
|
||||||
// 找到精确匹配
|
return -1;
|
||||||
return index;
|
|
||||||
} else {
|
|
||||||
// 未找到精确匹配,返回插入点
|
|
||||||
int insertionPoint = -(index + 1);
|
|
||||||
// 返回最接近的索引
|
|
||||||
if (insertionPoint >= lines.size()) {
|
|
||||||
return lines.size() - 1;
|
|
||||||
}
|
|
||||||
if (insertionPoint <= 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return insertionPoint;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return matchingStartedAt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 二分查找时间戳
|
||||||
|
*
|
||||||
|
* @param lines 日志行列表
|
||||||
|
* @param timestamp 目标时间戳
|
||||||
|
* @return 第一个大于等于目标时间戳的索引
|
||||||
|
*/
|
||||||
|
private static int binarySearchTimestamp(List<K8sLogLine> lines, String timestamp) {
|
||||||
|
int left = 0;
|
||||||
|
int right = lines.size();
|
||||||
|
|
||||||
|
while (left < right) {
|
||||||
|
int mid = left + (right - left) / 2;
|
||||||
|
String midTimestamp = lines.get(mid).getTimestamp();
|
||||||
|
|
||||||
|
if (midTimestamp.compareTo(timestamp) < 0) {
|
||||||
|
left = mid + 1;
|
||||||
|
} else {
|
||||||
|
right = mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return left;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据选择器切片日志
|
* 根据选择器切片日志
|
||||||
|
* 参考:logs.go SelectLogs函数
|
||||||
*
|
*
|
||||||
* @param lines 日志行列表
|
* @param lines 日志行列表
|
||||||
* @param selection 日志选择器
|
* @param selection 日志选择器
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user