Skip to content

Commit 97e4a57

Browse files
add listFn, refactor, add unit tests
1 parent 2d49703 commit 97e4a57

File tree

6 files changed

+257
-115
lines changed

6 files changed

+257
-115
lines changed
Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +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+
*/
113
package io.kubernetes.client.examples;
214

3-
import com.google.gson.reflect.TypeToken;
4-
import com.squareup.okhttp.Call;
515
import io.kubernetes.client.ApiClient;
616
import io.kubernetes.client.Configuration;
717
import io.kubernetes.client.apis.CoreV1Api;
818
import io.kubernetes.client.models.V1Namespace;
919
import io.kubernetes.client.models.V1NamespaceList;
1020
import io.kubernetes.client.pager.Pager;
11-
import io.kubernetes.client.pager.PagerParams;
1221
import io.kubernetes.client.util.Config;
22+
import io.kubernetes.client.util.PagerParams;
23+
import java.io.IOException;
1324
import java.util.List;
1425
import java.util.concurrent.TimeUnit;
1526

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+
*/
1635
public class PagerExample {
17-
public static void main(String[] args) {
36+
public static void main(String[] args) throws IOException {
1837

19-
try {
20-
21-
ApiClient client = Config.defaultClient();
22-
client.getHttpClient().setReadTimeout(60, TimeUnit.SECONDS);
23-
Configuration.setDefaultApiClient(client);
24-
CoreV1Api api = new CoreV1Api();
25-
int i=0;
26-
Call call =
27-
api.listConfigMapForAllNamespacesCall(null, null, null, null, 1, null, null, null, null, null, null);
28-
29-
api.listNamespaceCall(
30-
null, "pretty", null, null, null, 20, null, null, null, null, null);
31-
32-
//Pager pager = new Pager(client, call, 1, new TypeToken<V1NamespaceList>() {}.getType());
33-
Pager pager = new Pager(
34-
(PagerParams param)-> {
35-
try {
36-
return api.listNamespaceCall(
37-
null, "pretty", param.getContinue(), null, null, param.getLimit(), null, null, null, null, null);
38-
}
39-
catch(Exception e) {
40-
throw new RuntimeException(e);
41-
}
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(
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+
null,
56+
null,
57+
null,
58+
null);
59+
} catch (Exception e) {
60+
throw new RuntimeException(e);
61+
}
4262
},
43-
client,
44-
20,
45-
new TypeToken<V1NamespaceList>() {}.getType()
46-
);
47-
while (pager.hasNext()) {
48-
V1NamespaceList list = pager.next();
49-
List<V1Namespace> items = list.getItems();
50-
System.out.println("count:"+items.size());
51-
for (V1Namespace namespace : items) {
52-
System.out.println(namespace.getMetadata().getName());
53-
}
54-
System.out.println("------------------"+(++i));
63+
client,
64+
3,
65+
V1NamespaceList.class);
66+
while (pager.hasNext()) {
67+
V1NamespaceList list = 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());
5572
}
56-
} catch (Exception e) {
57-
e.printStackTrace();
73+
System.out.println("------------------" + (++i));
5874
}
5975
}
6076
}
Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package io.kubernetes.client.pager;
22

3-
import java.io.IOException;
4-
import java.lang.reflect.Type;
5-
import java.util.function.Function;
6-
import com.google.gson.JsonObject;
7-
import com.google.gson.JsonParser;
83
import com.squareup.okhttp.Call;
9-
import com.squareup.okhttp.Request;
10-
import com.squareup.okhttp.Response;
11-
import com.squareup.okhttp.ResponseBody;
124
import io.kubernetes.client.ApiClient;
135
import io.kubernetes.client.ApiException;
146
import io.kubernetes.client.models.V1ListMeta;
15-
import io.kubernetes.client.util.ResponseUtils;
7+
import io.kubernetes.client.util.PagerParams;
8+
import io.kubernetes.client.util.Reflect;
9+
import io.kubernetes.client.util.exception.ObjectMetaReflectException;
10+
import java.io.IOException;
11+
import java.lang.reflect.Type;
12+
import java.util.function.Function;
1613

1714
public class Pager {
1815
private String _continue;
@@ -22,58 +19,89 @@ public class Pager {
2219
private Type listType;
2320
private Function<PagerParams, Call> listFunc;
2421

25-
public Pager(Function<PagerParams, Call> listFunc, ApiClient client, Integer limit, Type listType) {
22+
/**
23+
* Pagination in kubernetes list call depends on continue and limit variable
24+
*
25+
* @param listFunc
26+
* @param client
27+
* @param limit
28+
* @param listType
29+
*/
30+
public Pager(
31+
Function<PagerParams, Call> listFunc, ApiClient client, Integer limit, Type listType) {
2632
this.listFunc = listFunc;
2733
this.client = client;
2834
this.limit = limit;
2935
this.listType = listType;
3036
}
3137

38+
/**
39+
* returns false if kubernetes server has exhausted List.
40+
*
41+
* @return
42+
*/
3243
public Boolean hasNext() {
3344
if (_continue == null && call != null) {
3445
return Boolean.FALSE;
3546
}
3647
return Boolean.TRUE;
3748
}
3849

39-
public <T> T next()
40-
throws NoSuchFieldException, SecurityException, IllegalArgumentException,
41-
IllegalAccessException, IOException, ApiException {
42-
call = getNextCall();
43-
return executeRequest(client, call, listType);
50+
/**
51+
* returns next chunk of List. size of list depends on limit set in constructor.
52+
*
53+
* @return Object
54+
*/
55+
public <T> T next() {
56+
return next(null);
4457
}
4558

46-
private Call getNextCall() {
59+
/**
60+
* returns next chunk of List. size of list depends on limit set in constructor or nextLimit.
61+
*
62+
* @param nextLimit
63+
* @return
64+
*/
65+
public <T> T next(Integer nextLimit) {
66+
try {
67+
call = getNextCall(nextLimit);
68+
return executeRequest(client, call, listType);
69+
} catch (Exception e) {
70+
throw new RuntimeException(e);
71+
}
72+
}
73+
74+
/**
75+
* returns next list call by setting continue variable and limit
76+
*
77+
* @param nextLimit
78+
* @return Object
79+
*/
80+
private Call getNextCall(Integer nextLimit) {
4781
PagerParams params = new PagerParams();
48-
if(_continue != null) {
49-
params.setContinue(_continue);
82+
if (_continue != null) {
83+
params.setContinue(_continue);
5084
}
51-
params.setLimit(limit);
85+
params.setLimit((nextLimit != null) ? nextLimit : limit);
5286
return listFunc.apply(params);
5387
}
5488

89+
/**
90+
* executes the list call and sets the continue variable for next list call
91+
*
92+
* @param client
93+
* @param call
94+
* @param listType
95+
* @return
96+
* @throws IOException
97+
* @throws ApiException
98+
* @throws ObjectMetaReflectException
99+
*/
55100
private <T> T executeRequest(ApiClient client, Call call, Type listType)
56-
throws IOException, ApiException {
57-
String line = ResponseUtils.getResponseString(call.execute());
58-
setNextContinue(line);
59-
return client.getJSON().deserialize(line, listType);
60-
}
61-
62-
private void setNextContinue(String line) {
63-
JsonParser parser = new JsonParser();
64-
JsonObject json = (JsonObject) parser.parse(line);
65-
if (json.has("kind") && json.has("metadata")) {
66-
String kind = json.get("kind").getAsString();
67-
if (kind.contains("List")) {
68-
V1ListMeta metaList =
69-
client.getJSON().deserialize(json.get("metadata").toString(), V1ListMeta.class);
70-
_continue = metaList.getContinue();
71-
}else {
72-
throw new RuntimeException("Subsequent call failed " + line);
73-
}
74-
} else {
75-
throw new RuntimeException("Subsequent call failed " + line);
76-
}
101+
throws IOException, ApiException, ObjectMetaReflectException {
102+
T data = client.handleResponse(call.execute(), listType);
103+
V1ListMeta listMetaData = Reflect.listMetadata(data);
104+
_continue = listMetaData.getContinue();
105+
return data;
77106
}
78-
79107
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
package io.kubernetes.client.pager;
1+
package io.kubernetes.client.util;
22

33
public class PagerParams {
4-
4+
55
private Integer limit;
66
private String _continue;
7-
7+
8+
public PagerParams() {}
9+
10+
public PagerParams(Integer limit, String _continue) {
11+
this.limit = limit;
12+
this._continue = _continue;
13+
}
14+
815
public Integer getLimit() {
916
return limit;
1017
}
18+
1119
public void setLimit(Integer limit) {
1220
this.limit = limit;
13-
}
21+
}
22+
1423
public String getContinue() {
1524
return _continue;
1625
}
26+
1727
public void setContinue(String _continue) {
1828
this._continue = _continue;
1929
}
20-
2130
}

util/src/main/java/io/kubernetes/client/util/ResponseUtils.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)