X Tutup
Skip to content

Work on port ranges - change all to strings#566

Closed
larochef wants to merge 1 commit intodocker-java:masterfrom
larochef:master
Closed

Work on port ranges - change all to strings#566
larochef wants to merge 1 commit intodocker-java:masterfrom
larochef:master

Conversation

@larochef
Copy link
Copy Markdown

@larochef larochef commented May 3, 2016

This is a fix for #556 allowing port ranges.


This change is Reviewable

* @see Ports#bind(ExposedPort, Binding)
* @see ExposedPort
*/
public Binding(Integer hostPort) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Change to string? to be consistent?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

or require @Nonnull

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The thing is there already is a constructor same signature, that's why I didn't change the Integer. I guess it would be safer to have static factories instead, and only keep the constructor wil everything. What do you think ?

@KostyaSha
Copy link
Copy Markdown
Member

LGTM if api allow string, then it should be string.

@marcuslinke
Copy link
Copy Markdown
Contributor

marcuslinke commented May 3, 2016

I'm not sure whether we should switch to String. Is it really needed? What about error handling when user passes invalid string? I guess we will get some unspecified error message from remote docker in this case which would be bad. Wouldn't it be better to hide single port / port ranges within the ExposedPort and Port.Binding classes via different constructors?

@marcuslinke
Copy link
Copy Markdown
Contributor

Related documentation: https://docs.docker.com/engine/reference/run/#expose-incoming-ports

-p=[] : Publish a container᾿s port or a range of ports to the host
format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
Both hostPort and containerPort can be specified as a
range of ports. When specifying ranges for both, the
number of container ports in the range must match the
number of host ports in the range, for example:
-p 1234-1236:1234-1236/tcp

When specifying a range for hostPort only, the
containerPort must not be a range. In this case the
container port is published somewhere within the
specified hostPort range. (e.g., -p 1234-1236:1234/tcp)

@KostyaSha
Copy link
Copy Markdown
Member

@marcuslinke well, if you would bundle cli logic into direct API requests that would end in difficult code.
Until we make some porcelain cli imho it makes sense be close to docker API and provide end users ability to define calls. If docker-daemon accepts strings, then it should be possible to do it.

@larochef
Copy link
Copy Markdown
Author

larochef commented May 3, 2016

That's a good question, the string validation.

I've been wondering if we should have a regexp, something like [1-9][0-9]+(-[1-9][0-9]+)? but then, if some new format appears in the docker api in the future, then we'll have to change again. So should we validate or let docker do it instead ?

@marcuslinke
Copy link
Copy Markdown
Contributor

I think the point here is whether docker deamon returns a meaningful error in the case of invalid string. @larochef Could you check that please? Maybe validation is realized in docker CLI or in remote API or not at all. If validation is done in remote API we can use string without any problem. If validation is implemented in docker CLI we need at least validation in docker-java for this.

@larochef
Copy link
Copy Markdown
Author

larochef commented May 4, 2016

I confirm that docker damon returns you a proper error.
I tried and got : com.github.dockerjava.api.exception.InternalServerErrorException: Invalid port specification: "pouet"

@marcuslinke
Copy link
Copy Markdown
Contributor

marcuslinke commented May 4, 2016

@larochef Thanks! But what is with the more complex constraints:

When specifying ranges for both, the
number of container ports in the range must match the
number of host ports in the range, for example:
-p 1234-1236:1234-1236/tcp

When specifying a range for hostPort only, the
containerPort must not be a range. In this case the
container port is published somewhere within the
specified hostPort range. (e.g., -p 1234-1236:1234/tcp)

@larochef
Copy link
Copy Markdown
Author

larochef commented May 4, 2016

Ok, so after some investigation:

  • mapping a port from the container to a range on the host works as it is expected, I could have it work without any problem.
  • mapping a range to a range is translated by the cli to a list of mappings, taking them all 1 by 1.

Should we support ranges and do the same magic as docker-cli or let the user handle ranges manually ?

I don't think it's a good idea to let this too magic since each port binding requires some proxy magic by docker and consumes memory.

@marcuslinke
Copy link
Copy Markdown
Contributor

marcuslinke commented May 4, 2016

OK, let summarize the possible cases for a mapping string from docker CLI perspective:

case 1234:1234: valid
case 1234-1236:1234: valid
case 1234-1236:1234-1236: valid, needs additional range validation and translation to single mappings
else: invalid

I think the third case (range to range) shouldn't be supported by docker-java. So we have to ensure that only the first and the second case will be possible with our API. Do you agree?

import com.github.dockerjava.api.model.RestartPolicy;
import com.github.dockerjava.api.model.Volume;
import com.github.dockerjava.api.model.VolumesFrom;
import com.github.dockerjava.api.model.*;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please don't replace imports with hashes.

@larochef
Copy link
Copy Markdown
Author

larochef commented May 5, 2016

Sorry for the imports, I'll check again my ide configuguration..

@marcuslinke I agree with that. I guess then we can leave the exposed port (the one from the container) as an int and have then the bound ports as strings since they can be ranges.

If we want all as Strings, I guess then we'll have to add a check in the corresponding serializer.

What do you think ?

@marcuslinke
Copy link
Copy Markdown
Contributor

I guess then we can leave the exposed port (the one from the container) as an int and have then the bound ports as strings since they can be ranges.

@larochef Thats what I thought also. Letting ExposedPort be an int and Binding with both String and int constructors that internally stores it as String sounds good to me.

@codecov-io
Copy link
Copy Markdown

codecov-io commented May 8, 2016

Current coverage is 30.51%

Merging #566 into master will increase coverage by +6.70%

  1. 2 files in ...dockerjava/api/model were modified. more
    • Misses -1
    • Partials -2
    • Hits +3
  2. 3 files (not in diff) in ...erjava/netty/handler were modified. more
    • Misses -19
    • Partials -2
    • Hits +21
  3. 17 files (not in diff) in ...ockerjava/netty/exec were modified. more
    • Misses -35
    • Hits +35
  4. 3 files (not in diff) in ...hub/dockerjava/netty were modified. more
    • Misses -17
    • Hits +17
  5. 5 files (not in diff) in ...kerjava/jaxrs/filter were modified. more
    • Misses -23
    • Hits +23
  6. 2 files (not in diff) in ...java/jaxrs/connector were modified. more
    • Misses -49
    • Hits +49
  7. 22 files (not in diff) in ...hub/dockerjava/jaxrs were modified. more
    • Misses -66
    • Hits +66
  8. 5 files (not in diff) in ...dockerjava/core/util were modified. more
    • Misses -15
    • Partials -6
    • Hits +21
  9. 2 files (not in diff) in ...java/core/dockerfile were modified. more
    • Misses -9
    • Partials -4
    • Hits +13
  10. 11 files (not in diff) in ...kerjava/core/command were modified. more
    • Misses -27
    • Partials -6
    • Hits +33
@@             master       #566   diff @@
==========================================
  Files           296        296          
  Lines          6129       6129          
  Methods           0          0          
  Messages          0          0          
  Branches        532        532          
==========================================
+ Hits           1459       1870   +411   
+ Misses         4585       4259   -326   
+ Partials         85          0    -85   

Powered by Codecov. Last updated by 1ab6e63...40dec22

@larochef larochef force-pushed the master branch 3 times, most recently from 7d0b988 to 3901348 Compare May 8, 2016 11:21
@larochef
Copy link
Copy Markdown
Author

larochef commented May 8, 2016

Ok, I've re-worked the PR in that way, tell what you think of it now.

@marcuslinke
Copy link
Copy Markdown
Contributor

@larochef I've polished it a bit. Closing this PR in favor of #575. WDYT?

@marcuslinke marcuslinke closed this May 8, 2016
@larochef
Copy link
Copy Markdown
Author

larochef commented May 8, 2016

Good for me :)

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.

4 participants

X Tutup