1.问题描述
通过:
BatchV1Api apiInstance = new BatchV1Api();
apiInstance.deleteNamespacedJob(name, namespace, new V1DeleteOptions(), null, null, 0, null, null);
对Kubernets的job进行删除时报错:
java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 1436 path $.status
但实际删除是成功的。
2.原因
通过查找发现这是目前Kubernetes的一个bug,无论是删除Job还是Pod都会有这个错误。
在上面代码中,因为deleteNamespacedJob
期待返回的对象是V1Status,而实际返回的是一个V1Job,当将response的内容反序列化成V1Status就报错了。
具体可参考:
https://github.com/kubernetes/kubernetes/issues/65121
3.解决
为了让程序正确运行,通过try{}catch{}的方式
BatchV1Api apiInstance = new BatchV1Api();
try {
apiInstance.deleteNamespacedJob(name, namespace, new V1DeleteOptions(), null, null, 0, null, null);
} catch (Exception e) {
if (e.getCause() instanceof IllegalStateException) {
IllegalStateException ise = (IllegalStateException) e.getCause();
if (ise.getMessage() != null && ise.getMessage().contains("Expected a string but was BEGIN_OBJECT")) {
log.info("Catching exception because of issue https://github.com/kubernetes/kubernetes/issues/65121");
} else {
//...throw error or log
}
} else {
//...throw error or log
}
}