getting a continue token to run CRD watch in a loop
Hi, following the advice in other issues I'm trying to run watch() in a loop. I'm watching for a CRD using the custom objects API. I've looked through the objects returned in the debugger and I can't find a continue token to use, or a resource version to use, so that after a timeout I can resume the watch from where I left off. Is there a way to do this?
Here's an minimal example:
package com.netflix.accountname;
import com.google.gson.reflect.TypeToken;
import io.kubernetes.client.openapi.ApiCallback;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.apis.CustomObjectsApi;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.Watch;
import io.kubernetes.client.util.Watch.Response;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
public class Main {
public void main() throws Exception {
CustomObjectsApi crdApi = new CustomObjectsApi(Config.defaultClient());
ApiClient apiClient = crdApi.getApiClient();
OkHttpClient newHttpClient =
apiClient.getHttpClient().newBuilder().readTimeout(30, TimeUnit.SECONDS).build();
apiClient.setHttpClient(newHttpClient);
String pretty = null;
String _continue = null;
String fieldSelector = null;
String labelSelector = null;
Integer limit = null;
String resourceVersion = null;
Integer timeoutSeconds = null;
Boolean keepWatching = true;
ApiCallback<?> apiCallback = null;
while (true) {
try {
try (Watch<Object> watch =
Watch.createWatch(
crdApi.getApiClient(),
crdApi.listNamespacedCustomObjectCall(
"example.com",
"v1",
"default",
"foos",
pretty,
_continue,
fieldSelector,
labelSelector,
limit,
resourceVersion,
timeoutSeconds,
keepWatching,
apiCallback),
new TypeToken<Response<Object>>() {}.getType())) {
for (Watch.Response<Object> resp : watch) {
System.out.println(resp);
}
}
} catch (RuntimeException e) {
if (e.getCause().getClass() == SocketTimeoutException.class) {
// how to get a resourceVersion or _continue value?
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception {
new Main().main();
}
}
Your object should have a metadata.resourceVersion field which contains the resource version.
That said, it's likely that an Informer is easier to use than doing watches directly yourself.
Indeed there is a metadata.resourceVersion. I must have missed it. Thanks. I'll check out Informer too.