|
37 | 37 | import io.kubernetes.client.util.generic.options.PatchOptions;
|
38 | 38 | import io.kubernetes.client.util.generic.options.UpdateOptions;
|
39 | 39 | import java.io.IOException;
|
| 40 | +import java.util.Arrays; |
| 41 | +import java.util.function.Function; |
40 | 42 | import okhttp3.Call;
|
41 | 43 | import okhttp3.HttpUrl;
|
42 | 44 |
|
@@ -476,6 +478,63 @@ public KubernetesApiResponse<ApiType> update(ApiType object, final UpdateOptions
|
476 | 478 | });
|
477 | 479 | }
|
478 | 480 |
|
| 481 | + /** |
| 482 | + * Create kubernetes api response, if the namespace in the object is present, it will send a |
| 483 | + * namespace-scoped requests, vice versa. |
| 484 | + * |
| 485 | + * @param object the object |
| 486 | + * @param status function to extract the status from the object |
| 487 | + * @return the kubernetes api response |
| 488 | + */ |
| 489 | + public KubernetesApiResponse<ApiType> updateStatus( |
| 490 | + ApiType object, Function<ApiType, Object> status) { |
| 491 | + return updateStatus(object, status, new UpdateOptions()); |
| 492 | + } |
| 493 | + |
| 494 | + /** |
| 495 | + * Update status of kubernetes api response. |
| 496 | + * |
| 497 | + * @param object the object |
| 498 | + * @param status function to extract the status from the object |
| 499 | + * @param updateOptions the update options |
| 500 | + * @return the kubernetes api response |
| 501 | + */ |
| 502 | + public KubernetesApiResponse<ApiType> updateStatus( |
| 503 | + ApiType object, Function<ApiType, Object> status, final UpdateOptions updateOptions) { |
| 504 | + V1ObjectMeta objectMeta = object.getMetadata(); |
| 505 | + return executeCall( |
| 506 | + customObjectsApi.getApiClient(), |
| 507 | + apiTypeClass, |
| 508 | + () -> { |
| 509 | + //// TODO(yue9944882): judge namespaced object via api discovery |
| 510 | + boolean isNamespaced = !Strings.isNullOrEmpty(objectMeta.getNamespace()); |
| 511 | + if (isNamespaced) { |
| 512 | + return customObjectsApi.patchNamespacedCustomObjectStatusCall( |
| 513 | + this.apiGroup, |
| 514 | + this.apiVersion, |
| 515 | + objectMeta.getNamespace(), |
| 516 | + this.resourcePlural, |
| 517 | + objectMeta.getName(), |
| 518 | + Arrays.asList(new StatusPatch(status.apply(object))), |
| 519 | + updateOptions.getDryRun(), |
| 520 | + updateOptions.getFieldManager(), |
| 521 | + null, |
| 522 | + null); |
| 523 | + } else { |
| 524 | + return customObjectsApi.patchClusterCustomObjectStatusCall( |
| 525 | + this.apiGroup, |
| 526 | + this.apiVersion, |
| 527 | + this.resourcePlural, |
| 528 | + objectMeta.getName(), |
| 529 | + Arrays.asList(new StatusPatch(status.apply(object))), |
| 530 | + updateOptions.getDryRun(), |
| 531 | + updateOptions.getFieldManager(), |
| 532 | + null, |
| 533 | + null); |
| 534 | + } |
| 535 | + }); |
| 536 | + } |
| 537 | + |
479 | 538 | /**
|
480 | 539 | * Patch kubernetes api response.
|
481 | 540 | *
|
@@ -771,4 +830,41 @@ private Call tweakCallForCoreV1Group(Call call) {
|
771 | 830 | .getHttpClient()
|
772 | 831 | .newCall(call.request().newBuilder().url(tweakedUrl).build());
|
773 | 832 | }
|
| 833 | + |
| 834 | + public static class StatusPatch { |
| 835 | + |
| 836 | + private String op = "replace"; |
| 837 | + |
| 838 | + private String path = "/status"; |
| 839 | + |
| 840 | + private Object value; |
| 841 | + |
| 842 | + public StatusPatch(Object value) { |
| 843 | + this.value = value; |
| 844 | + } |
| 845 | + |
| 846 | + public String getOp() { |
| 847 | + return op; |
| 848 | + } |
| 849 | + |
| 850 | + public void setOp(String op) { |
| 851 | + this.op = op; |
| 852 | + } |
| 853 | + |
| 854 | + public String getPath() { |
| 855 | + return path; |
| 856 | + } |
| 857 | + |
| 858 | + public void setPath(String path) { |
| 859 | + this.path = path; |
| 860 | + } |
| 861 | + |
| 862 | + public Object getValue() { |
| 863 | + return value; |
| 864 | + } |
| 865 | + |
| 866 | + public void setValue(Object value) { |
| 867 | + this.value = value; |
| 868 | + } |
| 869 | + } |
774 | 870 | }
|
0 commit comments