Skip to content

Commit d600430

Browse files
authored
Merge pull request #527 from kondapally1989/master
Adding pagination utility
2 parents ab632a5 + 2749d5b commit d600430

File tree

8 files changed

+489
-0
lines changed

8 files changed

+489
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.examples;
14+
15+
import io.kubernetes.client.ApiClient;
16+
import io.kubernetes.client.Configuration;
17+
import io.kubernetes.client.apis.CoreV1Api;
18+
import io.kubernetes.client.models.V1Namespace;
19+
import io.kubernetes.client.models.V1NamespaceList;
20+
import io.kubernetes.client.pager.Pager;
21+
import io.kubernetes.client.pager.PagerParams;
22+
import io.kubernetes.client.util.Config;
23+
import java.io.IOException;
24+
import java.util.List;
25+
import java.util.concurrent.TimeUnit;
26+
27+
/**
28+
* A simple example of how to use the Java API
29+
*
30+
* <p>Easiest way to run this: mvn exec:java
31+
* -Dexec.mainClass="io.kubernetes.client.examples.PagerExample"
32+
*
33+
* <p>From inside $REPO_DIR/examples
34+
*/
35+
public class PagerExample {
36+
public static void main(String[] args) throws IOException {
37+
38+
ApiClient client = Config.defaultClient();
39+
client.getHttpClient().setReadTimeout(60, TimeUnit.SECONDS);
40+
Configuration.setDefaultApiClient(client);
41+
CoreV1Api api = new CoreV1Api();
42+
int i = 0;
43+
Pager pager =
44+
new Pager<V1Namespace, V1NamespaceList>(
45+
(PagerParams param) -> {
46+
try {
47+
return api.listNamespaceCall(
48+
null,
49+
null,
50+
param.getContinue(),
51+
null,
52+
null,
53+
param.getLimit(),
54+
null,
55+
1,
56+
null,
57+
null,
58+
null);
59+
} catch (Exception e) {
60+
throw new RuntimeException(e);
61+
}
62+
},
63+
client,
64+
10,
65+
V1NamespaceList.class);
66+
while (pager.hasNext()) {
67+
V1NamespaceList list = (V1NamespaceList) pager.next();
68+
List<V1Namespace> items = list.getItems();
69+
System.out.println("count:" + items.size());
70+
for (V1Namespace namespace : items) {
71+
System.out.println(namespace.getMetadata().getName());
72+
}
73+
System.out.println("------------------" + (++i));
74+
}
75+
}
76+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.pager;
14+
15+
import com.squareup.okhttp.Call;
16+
import io.kubernetes.client.ApiClient;
17+
import io.kubernetes.client.ApiException;
18+
import io.kubernetes.client.models.V1ListMeta;
19+
import io.kubernetes.client.util.Reflect;
20+
import io.kubernetes.client.util.exception.ObjectMetaReflectException;
21+
import java.io.IOException;
22+
import java.lang.reflect.Type;
23+
import java.util.function.Function;
24+
25+
public class Pager<ApiType, ApiListType> {
26+
private String continueToken;
27+
private Integer limit;
28+
private ApiClient client;
29+
private Call call;
30+
private Type listType;
31+
private Function<PagerParams, Call> listFunc;
32+
33+
/**
34+
* Pagination in kubernetes list call depends on continue and limit variable
35+
*
36+
* @param listFunc lambda of type: (PagerParams p)->{return
37+
* list<*>[namespace[s|d]]*<*>Call(...p.getContinue(),...p.getLimit()...);}
38+
* @param client instance of {@link ApiClient}
39+
* @param limit size of list to be fetched
40+
* @param listType Type of list to be fetched
41+
*/
42+
public Pager(
43+
Function<PagerParams, Call> listFunc, ApiClient client, Integer limit, Type listType) {
44+
this.listFunc = listFunc;
45+
this.client = client;
46+
this.limit = limit;
47+
this.listType = listType;
48+
}
49+
50+
/**
51+
* returns false if kubernetes server has exhausted List.
52+
*
53+
* @return
54+
*/
55+
public Boolean hasNext() {
56+
if (continueToken == null && call != null) {
57+
return Boolean.FALSE;
58+
}
59+
return Boolean.TRUE;
60+
}
61+
62+
/**
63+
* returns next chunk of List. size of list depends on limit set in constructor.
64+
*
65+
* @return Object
66+
*/
67+
public <ApiType> ApiListType next() {
68+
return next(null);
69+
}
70+
71+
/**
72+
* returns next chunk of list. size of list depends on limit set in constructor or nextLimit.
73+
*
74+
* @param nextLimit
75+
* @return
76+
*/
77+
public <ApiType> ApiListType next(Integer nextLimit) {
78+
try {
79+
call = getNextCall(nextLimit);
80+
return executeRequest(call);
81+
} catch (Exception e) {
82+
if (e instanceof ApiException) {
83+
throw new RuntimeException(((ApiException) e).getResponseBody());
84+
}
85+
throw new RuntimeException(e);
86+
}
87+
}
88+
89+
/** returns next list call by setting continue variable and limit */
90+
private Call getNextCall(Integer nextLimit) {
91+
PagerParams params = new PagerParams((nextLimit != null) ? nextLimit : limit);
92+
if (continueToken != null) {
93+
params.setContinue(continueToken);
94+
}
95+
return listFunc.apply(params);
96+
}
97+
98+
/** executes the list call and sets the continue variable for next list call */
99+
private <ApiType> ApiListType executeRequest(Call call)
100+
throws IOException, ApiException, ObjectMetaReflectException {
101+
ApiListType data = client.handleResponse(call.execute(), listType);
102+
V1ListMeta listMetaData = Reflect.listMetadata(data);
103+
continueToken = listMetaData.getContinue();
104+
return data;
105+
}
106+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.pager;
14+
15+
public class PagerParams {
16+
17+
private Integer limit;
18+
private String continueToken;
19+
20+
public PagerParams(Integer limit) {
21+
this.limit = limit;
22+
}
23+
24+
public PagerParams(Integer limit, String continueToken) {
25+
this.limit = limit;
26+
this.continueToken = continueToken;
27+
}
28+
29+
public Integer getLimit() {
30+
return limit;
31+
}
32+
33+
public String getContinue() {
34+
return continueToken;
35+
}
36+
37+
public void setContinue(String continueToken) {
38+
this.continueToken = continueToken;
39+
}
40+
}

0 commit comments

Comments
 (0)