Skip to content
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

Add Watch Support #40

Merged
merged 2 commits into from
Jun 29, 2017
Merged

Add Watch Support #40

merged 2 commits into from
Jun 29, 2017

Conversation

mbohlool
Copy link
Contributor

@mbohlool mbohlool commented Jun 28, 2017

This is my first try to implement watch. It is mostly implemented like the Watch in python client. We can think of changing templates (for both python and java) to create a watch call for each list operation in API classes.

Fixes #8

depends on kubernetes-client/gen#16

built on top of #39

System.out.printf("%s : %s%n", item.type, item.object.getMetadata().getName());
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: newline at end of file?

(I actually don't care but I feel like you asked for this in a different PR and we should probably be consistent...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I think we should be consistent. Done.

throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody);
}
return new Watch<>(this.json, response.body(), watchType);
} catch (IOException e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not have this throw IOException also? Seems better than catching and wrapping...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just wanted to be consistent with the rest of the ApiClient code. Execute method, for example, do the same (as well as other methods).

* set watch to True and watch the changes to namespaces.
*/
public class Watch<T> implements Iterable<Watch.Response<T>> {

Copy link
Contributor

Choose a reason for hiding this comment

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

Javadoc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

public boolean hasNext() {
if (loadNext) {
try {
nextItem = Watch.this.next();
Copy link
Contributor

Choose a reason for hiding this comment

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

Hrm, I don't think that this obeys the Java iterator contract.

There is no requirement that a user call hasNext() to advance the iterator afaik. Why not just have all of this logic in the next() method?

Copy link
Contributor Author

@mbohlool mbohlool Jun 29, 2017

Choose a reason for hiding this comment

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

I am calling hasNext() in next() method so there is no need for user to call this before next(). I faced two options here, to raise exception on next() or hasNext(). I chose this way so when I use for-each with this iterator and the watch closes because of timeout, it won't raise an exception (because for-each calls hasNext() before next()). Of course if user cares about what happen they can always call next() and get the reason (or just call next() on Watch class itself).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've looked at this at another angle and made is much much simpler now. Please look at the Watch class as a whole again. It is implements both Iterable and Iterator and converts all IOExceptions to RuntimeException to be able to implement Iterator.

@mbohlool
Copy link
Contributor Author

Comments addressed or replied to. Please take another look.

@brendandburns
Copy link
Contributor

LGTM.

@brendandburns brendandburns merged commit ca0f256 into kubernetes-client:master Jun 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants