-
Notifications
You must be signed in to change notification settings - Fork 2k
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
k8s-ci-robot
merged 6 commits into
kubernetes-client:master
from
karthikkondapally:master
Mar 18, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19a749c
Pagination initial commit https://github.com/kubernetes-client/java/i…
karthikkondapally 50f7bdf
move add to set query
karthikkondapally 46233de
fix issues
karthikkondapally 2d49703
add PagerParams ResponseUtils
karthikkondapally 97e4a57
add listFn, refactor, add unit tests
karthikkondapally 2749d5b
address code review comments
karthikkondapally File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
examples/src/main/java/io/kubernetes/client/examples/PagerExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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
106
util/src/main/java/io/kubernetes/client/pager/Pager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add copyright boilerplate here. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
40
util/src/main/java/io/kubernetes/client/pager/PagerParams.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :)