问题现象
使用kubernetes-client/java 对 Deployment、ReplicaSet或者StatefulSet 进行Scale(手动扩缩容),修改实例数
客户端版本
<!--k8s--> <dependency> <groupId>io.kubernetes</groupId> <artifactId>client-java</artifactId> <version>6.0.1</version> <!--<scope>compile</scope>--> </dependency>
操作实例 StatefulSet
@Test public void AppsV1ApiTest(){ AppsV1Api apiInstance = new AppsV1Api(); String jsonPatchStr = "{\"op\":\"replace\",\"path\":\"/spec/replicas\", \"value\": " + 2 + " }"; V1Patch patch = new V1Patch(jsonPatchStr); try { apiInstance.patchNamespacedStatefulSet("资源名称", "分区名称", patch, "false", null, null, null); } catch (ApiException e) { e.printStackTrace(); } }
报错信息
{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"json: cannot unmarshal object into Go value of type jsonpatch.Patch","code":500}
操作实例 Deployment
@Test public void ExtensionsApiTest(){ ExtensionsV1beta1Api apiInstance = new ExtensionsV1beta1Api(); String jsonPatchStr = "{\"op\":\"replace\",\"path\":\"/spec/replicas\", \"value\": " + 2 + " }"; V1Patch patch = new V1Patch(jsonPatchStr); try { ApiResponse<ExtensionsV1beta1Scale> apiResponse = apiInstance. patchNamespacedDeploymentScaleWithHttpInfo("资源名称", "分区名称", patch, "false", null, null, null); } catch (ApiException e) { e.printStackTrace(); } }
报错信息
问题分析:
使用jsonpatch 更新或修改一个资源对象时,需要是数组形式,不能是一个对象
问题解决:
new V1Patch(jsonPatchStr)的内容,改为数组格式
String jsonPatchStr = "[{\"op\":\"replace\",\"path\":\"/spec/replicas\", \"value\": " + 2 + " }]";
JSON Patch讲解
原始内容
{ "baz": "qux", "foo": "bar" }
通过JSONPath操作
[ { "op": "replace", "path": "/baz", "value": "boo" }, { "op": "add", "path": "/hello", "value": ["world"] }, { "op": "remove", "path": "/foo" } ]
输出结果
{ "baz": "boo", "hello": ["world"] }
测试JSONPath还支持
Copy(复制)
{ "op": "copy", "from": "/biscuits/0", "path": "/best_biscuit" }
将值从JSON文档中的一个位置复制到另一个位置。这两个
from
和path
是JSON指针。
Move(移动)
{ "op": "move", "from": "/biscuits", "path": "/cookies" }
将值从一个位置移动到另一位置。这两个
from
和path
是JSON指针。
Test(测试)
{ "op": "test", "path": "/best_biscuit/name", "value": "Choco Leibniz" }
测试是否在文档中设置了指定的值。如果测试失败,则整个补丁都不适用。
参考链接: