fabric8 kubernetes client 中的UnrecognizedPropertyException: Unrecognized field “emulationMajor“
·
原因:这个原因是为某些k8s的api-server返回的字段可能和fabric8的不太一样,例如我的version接口返回
{
"major": "1",
"minor": "33",
"emulationMajor": "1",
"emulationMinor": "33",
"minCompatibilityMajor": "1",
"minCompatibilityMinor": "32",
"gitVersion": "v1.33.4+k3s1",
"gitCommit": "148243c49519922720fe1b340008dbce8fb02516",
"gitTreeState": "clean",
"buildDate": "2025-08-25T16:59:07Z",
"goVersion": "go1.24.5",
"compiler": "gc",
"platform": "linux/amd64"
}
而VersionInfo返回是这样的
private Date buildDate;
private String gitCommit;
private String gitVersion;
private String major;
private String minor;
private String gitTreeState;
private String platform;
private String goVersion;
private String compiler;
这会导致某些字段json的key找不到对象属性,报错。
解决办法:
1、如果你能升级到java11及以上,那么请升级到fabric8的7.0.0以上版本,我看git上面已经一致了。如果你是别的接口并不是version报错,请看2
2、我直接反射忽略未找到的属性。
方法调用位置是client创建之后,至于为啥可以看源码。
如果你需要获取某些字段,但是不想使用他的对象,可以看源码,其中有。方法差不多,有时间我在写一下
/**
* 配置 ObjectMapper 以忽略未知字段
* 解决 Kubernetes 1.33+ 版本中 VersionInfo 新增字段导致的反序列化异常
*
* @param client KubernetesClient 实例,用于获取其内部的 KubernetesSerialization
*/
private void configureObjectMapper(KubernetesClient client) {
try {
// 通过反射获取 BaseClient 的 kubernetesSerialization 属性
// kubernetesSerialization 是 BaseClient 抽象类的属性
Field serializationField = null;
Class<?> currentClass = client.getClass();
// 向上查找父类中的 kubernetesSerialization 字段
while (currentClass != null && serializationField == null) {
try {
serializationField = currentClass.getDeclaredField("kubernetesSerialization");
} catch (NoSuchFieldException e) {
currentClass = currentClass.getSuperclass();
}
}
if (serializationField == null) {
throw new NoSuchFieldException("kubernetesSerialization field not found in class hierarchy");
}
serializationField.setAccessible(true);
KubernetesSerialization serialization = (KubernetesSerialization) serializationField.get(client);
// 通过反射获取 KubernetesSerialization 的 mapper 属性
Field mapperField = KubernetesSerialization.class.getDeclaredField("mapper");
mapperField.setAccessible(true);
ObjectMapper mapper = (ObjectMapper) mapperField.get(serialization);
// 配置 mapper 忽略未知属性
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
log.info("已配置 KubernetesSerialization ObjectMapper 忽略未知字段,解决 Kubernetes 1.33+ 兼容性问题");
} catch (Exception e) {
log.warn("配置 KubernetesSerialization ObjectMapper 失败,可能影响 Kubernetes 1.33+ 兼容性", e);
}
}
更多推荐


所有评论(0)