Skip to content

Adding pagination utility #527

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

Merged
merged 6 commits into from
Mar 18, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.kubernetes.client.examples;

import io.kubernetes.client.ApiClient;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1Namespace;
import io.kubernetes.client.models.V1NamespaceList;
import io.kubernetes.client.pager.Pager;
import io.kubernetes.client.pager.PagerParams;
import io.kubernetes.client.util.Config;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* A simple example of how to use the Java API
*
* <p>Easiest way to run this: mvn exec:java
* -Dexec.mainClass="io.kubernetes.client.examples.PagerExample"
*
* <p>From inside $REPO_DIR/examples
*/
public class PagerExample {
public static void main(String[] args) throws IOException {

ApiClient client = Config.defaultClient();
client.getHttpClient().setReadTimeout(60, TimeUnit.SECONDS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(might be an unrelated question) what's the point of this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just prevents timeouts for large bodies/slow connections. I think the default is 10 seconds?

It could be that this is cargo-cult copied from other examples :)

Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
int i = 0;
Pager pager =
new Pager<V1Namespace, V1NamespaceList>(
(PagerParams param) -> {
try {
return api.listNamespaceCall(
null,
null,
param.getContinue(),
null,
null,
param.getLimit(),
null,
1,
null,
null,
null);
} catch (Exception e) {
throw new RuntimeException(e);
}
},
client,
10,
V1NamespaceList.class);
while (pager.hasNext()) {
V1NamespaceList list = (V1NamespaceList) pager.next();
List<V1Namespace> items = list.getItems();
System.out.println("count:" + items.size());
for (V1Namespace namespace : items) {
System.out.println(namespace.getMetadata().getName());
}
System.out.println("------------------" + (++i));
}
}
}
106 changes: 106 additions & 0 deletions util/src/main/java/io/kubernetes/client/pager/Pager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.kubernetes.client.pager;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add copyright boilerplate here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.


import com.squareup.okhttp.Call;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.models.V1ListMeta;
import io.kubernetes.client.util.Reflect;
import io.kubernetes.client.util.exception.ObjectMetaReflectException;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.function.Function;

public class Pager<ApiType, ApiListType> {
private String continueToken;
private Integer limit;
private ApiClient client;
private Call call;
private Type listType;
private Function<PagerParams, Call> listFunc;

/**
* Pagination in kubernetes list call depends on continue and limit variable
*
* @param listFunc lambda of type: (PagerParams p)->{return
* list<*>[namespace[s|d]]*<*>Call(...p.getContinue(),...p.getLimit()...);}
* @param client instance of {@link ApiClient}
* @param limit size of list to be fetched
* @param listType Type of list to be fetched
*/
public Pager(
Function<PagerParams, Call> listFunc, ApiClient client, Integer limit, Type listType) {
this.listFunc = listFunc;
this.client = client;
this.limit = limit;
this.listType = listType;
}

/**
* returns false if kubernetes server has exhausted List.
*
* @return
*/
public Boolean hasNext() {
if (continueToken == null && call != null) {
return Boolean.FALSE;
}
return Boolean.TRUE;
}

/**
* returns next chunk of List. size of list depends on limit set in constructor.
*
* @return Object
*/
public <ApiType> ApiListType next() {
return next(null);
}

/**
* returns next chunk of list. size of list depends on limit set in constructor or nextLimit.
*
* @param nextLimit
* @return
*/
public <ApiType> ApiListType next(Integer nextLimit) {
try {
call = getNextCall(nextLimit);
return executeRequest(call);
} catch (Exception e) {
if (e instanceof ApiException) {
throw new RuntimeException(((ApiException) e).getResponseBody());
}
throw new RuntimeException(e);
}
}

/** returns next list call by setting continue variable and limit */
private Call getNextCall(Integer nextLimit) {
PagerParams params = new PagerParams((nextLimit != null) ? nextLimit : limit);
if (continueToken != null) {
params.setContinue(continueToken);
}
return listFunc.apply(params);
}

/** executes the list call and sets the continue variable for next list call */
private <ApiType> ApiListType executeRequest(Call call)
throws IOException, ApiException, ObjectMetaReflectException {
ApiListType data = client.handleResponse(call.execute(), listType);
V1ListMeta listMetaData = Reflect.listMetadata(data);
continueToken = listMetaData.getContinue();
return data;
}
}
40 changes: 40 additions & 0 deletions util/src/main/java/io/kubernetes/client/pager/PagerParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.kubernetes.client.pager;

public class PagerParams {

private Integer limit;
private String continueToken;

public PagerParams(Integer limit) {
this.limit = limit;
}

public PagerParams(Integer limit, String continueToken) {
this.limit = limit;
this.continueToken = continueToken;
}

public Integer getLimit() {
return limit;
}

public String getContinue() {
return continueToken;
}

public void setContinue(String continueToken) {
this.continueToken = continueToken;
}
}
Loading