Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add updateStatus() convenience methods to GenericKubernetesApi #1470

Merged
merged 1 commit into from Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -37,6 +37,8 @@
import io.kubernetes.client.util.generic.options.PatchOptions;
import io.kubernetes.client.util.generic.options.UpdateOptions;
import java.io.IOException;
import java.util.Arrays;
import java.util.function.Function;
import okhttp3.Call;
import okhttp3.HttpUrl;

Expand Down Expand Up @@ -476,6 +478,63 @@ public KubernetesApiResponse<ApiType> update(ApiType object, final UpdateOptions
});
}

/**
* Create kubernetes api response, if the namespace in the object is present, it will send a
* namespace-scoped requests, vice versa.
*
* @param object the object
* @param status function to extract the status from the object
* @return the kubernetes api response
*/
public KubernetesApiResponse<ApiType> updateStatus(
ApiType object, Function<ApiType, Object> status) {
return updateStatus(object, status, new UpdateOptions());
}

/**
* Update status of kubernetes api response.
*
* @param object the object
* @param status function to extract the status from the object
* @param updateOptions the update options
* @return the kubernetes api response
*/
public KubernetesApiResponse<ApiType> updateStatus(
ApiType object, Function<ApiType, Object> status, final UpdateOptions updateOptions) {
V1ObjectMeta objectMeta = object.getMetadata();
return executeCall(
customObjectsApi.getApiClient(),
apiTypeClass,
() -> {
//// TODO(yue9944882): judge namespaced object via api discovery
boolean isNamespaced = !Strings.isNullOrEmpty(objectMeta.getNamespace());
if (isNamespaced) {
return customObjectsApi.patchNamespacedCustomObjectStatusCall(
this.apiGroup,
this.apiVersion,
objectMeta.getNamespace(),
this.resourcePlural,
objectMeta.getName(),
Arrays.asList(new StatusPatch(status.apply(object))),
updateOptions.getDryRun(),
updateOptions.getFieldManager(),
null,
null);
} else {
return customObjectsApi.patchClusterCustomObjectStatusCall(
this.apiGroup,
this.apiVersion,
this.resourcePlural,
objectMeta.getName(),
Arrays.asList(new StatusPatch(status.apply(object))),
updateOptions.getDryRun(),
updateOptions.getFieldManager(),
null,
null);
}
});
}

/**
* Patch kubernetes api response.
*
Expand Down Expand Up @@ -771,4 +830,41 @@ private Call tweakCallForCoreV1Group(Call call) {
.getHttpClient()
.newCall(call.request().newBuilder().url(tweakedUrl).build());
}

public static class StatusPatch {

private String op = "replace";

private String path = "/status";

private Object value;

public StatusPatch(Object value) {
this.value = value;
}

public String getOp() {
return op;
}

public void setOp(String op) {
this.op = op;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public Object getValue() {
return value;
}

public void setValue(Object value) {
this.value = value;
}
}
}
Expand Up @@ -22,6 +22,7 @@
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Job;
import io.kubernetes.client.openapi.models.V1JobList;
import io.kubernetes.client.openapi.models.V1JobStatus;
import io.kubernetes.client.openapi.models.V1ListMeta;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1Status;
Expand Down Expand Up @@ -152,6 +153,23 @@ public void updateNamespacedJobReturningObject() {
verify(1, putRequestedFor(urlPathEqualTo("/apis/batch/v1/namespaces/default/jobs/foo1")));
}

@Test
public void updateStatusNamespacedJobReturningObject() {
V1Job foo1 =
new V1Job().kind("Job").metadata(new V1ObjectMeta().namespace("default").name("foo1"));
foo1.setStatus(new V1JobStatus().failed(1));

stubFor(
patch(urlEqualTo("/apis/batch/v1/namespaces/default/jobs/foo1/status"))
.willReturn(aResponse().withStatus(200).withBody(new Gson().toJson(foo1))));
KubernetesApiResponse<V1Job> jobListResp = jobClient.updateStatus(foo1, t -> t.getStatus());
assertTrue(jobListResp.isSuccess());
assertEquals(foo1, jobListResp.getObject());
assertNull(jobListResp.getStatus());
verify(
1, patchRequestedFor(urlPathEqualTo("/apis/batch/v1/namespaces/default/jobs/foo1/status")));
}

@Test
public void patchNamespacedJobReturningObject() {
V1Patch v1Patch = new V1Patch("{}");
Expand Down