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 option to set a service nodeport #33319

Conversation

juanvallejo
Copy link
Contributor

@juanvallejo juanvallejo commented Sep 22, 2016

Release note:

Add kubectl --node-port option for specifying the service nodeport

This patch adds the option to set a nodeport when creating a NodePort
service. In case of a port allocation error due to a specified port
being out of the valid range, the error now includes the valid
range. If a --node-port value is not specified, it defaults to zero, in
which case the allocator will default to its current behavior of
assigning an available port.

This patch also adds a new helper function in cmd/util/helpers.go to
retrieve Int32 cobra flags.

Example

# create a nodeport service with an invalid port
$ kubectl create service nodeport mynodeport --tcp=8080:7777 --node-port=1
The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid
value: 1: provided port is not in the valid range. Valid ports range
from 30000-32767

# create a nodeport service with a valid port
$ kubectl create service nodeport mynodeport --tcp=8080:7777 --node-port=30000
service "mynodeport" created

# create a nodeport service with a port already in use
$ kubectl create service nodeport mynodeport --tcp=8080:7777 --node-port=30000
The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid value: 3000: provided port is already allocated

$ kubectl describe service mynodeport
Name:                   mynodeport
Namespace:              default
Labels:                 app=mynodeport
Selector:               app=mynodeport
Type:                   NodePort
IP:                     172.30.81.254
Port:                   8080-7777       8080/TCP
NodePort:               8080-7777       30000/TCP
Endpoints:              <none>
Session Affinity:       None
No events.

@fabianofranz


This change is Reviewable

@k8s-ci-robot
Copy link
Contributor

Can a kubernetes member verify that this patch is reasonable to test? If so, please reply with "@k8s-bot ok to test" on its own line.

Regular contributors should join the org to skip this step.

While we transition away from the Jenkins GitHub PR Builder plugin, "ok to test" commenters will need to be on the admin list defined in this file.

@k8s-github-robot k8s-github-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. release-note-label-needed labels Sep 22, 2016
juanvallejo added a commit to juanvallejo/origin that referenced this pull request Sep 22, 2016
UPSTREAM: kubernetes/kubernetes#33319

This patch adds the option to set a nodeport when creating a NodePort
service. In case of a port allocation error due to a specified port
being out of the valid range, the error now includes the valid
range. If a `--node-port` value is not specified, it defaults to zero, in
which case the allocator will default to its current behavior of
assigning an available port.

This patch also adds a new helper function in `cmd/util/helpers.go` to
retrieve `Int32` cobra flags.

**Example**
```
$ oc create service nodeport mynodeport --tcp=8080:7777 --node-port=1
The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid
value: 1: provided port is not in the valid range. Valid ports range
from 30000-32767

$ oc create service nodeport mynodeport --tcp=8080:7777 --node-port=30000
service "mynodeport" created

$ oc describe service mynodeport
Name:                   mynodeport
Namespace:              default
Labels:                 app=mynodeport
Selector:               app=mynodeport
Type:                   NodePort
IP:                     172.30.81.254
Port:                   8080-7777       8080/TCP
NodePort:               8080-7777       30000/TCP
Endpoints:              <none>
Session Affinity:       None
No events.
```
@fabianofranz
Copy link
Contributor

@k8s-bot ok to test

Copy link
Member

@lavalamp lavalamp left a comment

Choose a reason for hiding this comment

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

a few requests

@@ -82,7 +82,10 @@ func (r *PortAllocator) Free() int {
func (r *PortAllocator) Allocate(port int) error {
ok, offset := r.contains(port)
if !ok {
return ErrNotInRange
// include valid port range in error
Copy link
Member

Choose a reason for hiding this comment

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

You need to turn ErrNotInRange into a type if you want to add information to it like this. People might be checking for equality with ErrNotInRange and this code would break them.

Choose a reason for hiding this comment

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

imo, this will break people who use ErrNotInRange to check allocate error.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, it's much better now. The compiler will show you any code that needs to be updated. :)

@@ -134,6 +134,7 @@ func NewCmdCreateServiceNodePort(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Co
cmdutil.AddValidateFlags(cmd)
cmdutil.AddPrinterFlags(cmd)
cmdutil.AddGeneratorFlags(cmd, cmdutil.ServiceNodePortGeneratorV1Name)
cmd.Flags().Int32("node-port", 0, "Port used to expose the service on each node in a cluster.")
Copy link
Member

Choose a reason for hiding this comment

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

Why int32? There's already an int, just use that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since NodePort is of type int32, I thought to receive it as that from the flag value, although I could receive it as int and convert it afterwards.

Copy link
Member

Choose a reason for hiding this comment

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

Only 0-65k are actually valid. We specify the bit width in the API because it's a transport format, but that's not important for kubectl flags.

Choose a reason for hiding this comment

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

I agree with @lavalamp , we could use int directly here.

@lavalamp lavalamp assigned pwittrock and unassigned lavalamp Sep 22, 2016
@lavalamp
Copy link
Member

Give this to Phil, for verification that this is something we want to do.

Also what happens if you make a second service that uses a port that's already taken? does it give a nice error message?

@juanvallejo juanvallejo force-pushed the jvallejo/add-option-to-set-nodeport branch from df99c20 to 878477b Compare September 22, 2016 21:03
@juanvallejo
Copy link
Contributor Author

juanvallejo commented Sep 22, 2016

@lavalamp

Also what happens if you make a second service that uses a port that's already taken? does it give a
nice error message?

Yes, if the port is already taken, it returns:

The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid value: <port>: provided port is already allocated

@juanvallejo
Copy link
Contributor Author

@lavalamp Thanks for the feedback, review comments addressed

@k8s-ci-robot
Copy link
Contributor

Jenkins GCE e2e failed for commit 35a9794a5a922ff8b0855597bc2945f387098131. Full PR test history.

The magic incantation to run this job again is @k8s-bot gce e2e test this. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@juanvallejo juanvallejo force-pushed the jvallejo/add-option-to-set-nodeport branch from 35a9794 to d63848f Compare September 23, 2016 17:23
@k8s-ci-robot
Copy link
Contributor

Jenkins verification failed for commit d63848f480ad0b8af3ba644827573a066571e9ab. Full PR test history.

The magic incantation to run this job again is @k8s-bot verify test this. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@pwittrock
Copy link
Member

@adohe Would you mind doing a pass to make sure this makes sense.

@adohe-zz
Copy link

@pwittrock sure, I will check this.

@pwittrock
Copy link
Member

Adding the flag makes sense to me. @adohe looks like @lavalamp has already done a review on this, but makes sense to take a pass and make sure things conform to kubectl best practices

@adohe-zz
Copy link

@juanvallejo

# create a nodeport service with a port already in use
$ kubectl create service nodeport mynodeport --tcp=8080:7777 --node-port=30000
The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid value: 3000: provided port is already allocated

the error output should be 30000, right?

@adohe-zz
Copy link

@juanvallejo could you link the original issue here? I want to get more context.

@k8s-github-robot k8s-github-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 29, 2016
@juanvallejo
Copy link
Contributor Author

@adohe

the error output should be 30000, right?

Ah, correct, I just checked, and doing:

$ kubectl create service nodeport mynodeport --tcp=8080:7777 --node-port=3000

gives the error:

The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid value: 3000: provided port is not in the valid range

while

kubectl create service nodeport mynodeport --tcp=8080:7777 --node-port=30000

gives the error

The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid value: 30000: provided port is already allocated

Once I try to allocate it for the second time.

could you link the original issue here? I want to get more context.

https://bugzilla.redhat.com/show_bug.cgi?id=1378012

@juanvallejo juanvallejo force-pushed the jvallejo/add-option-to-set-nodeport branch from 24d0993 to dc7f493 Compare September 29, 2016 14:45
@k8s-github-robot k8s-github-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 29, 2016
@k8s-ci-robot
Copy link
Contributor

Jenkins GCI GKE smoke e2e failed for commit dc7f493. Full PR test history.

The magic incantation to run this job again is @k8s-bot gci gke e2e test this. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@adohe-zz
Copy link

@juanvallejo I think you still need to fix the e2e tests.

@juanvallejo
Copy link
Contributor Author

@k8s-bot gke e2e test this

@juanvallejo
Copy link
Contributor Author

juanvallejo commented Sep 29, 2016

@k8s-bot gci gke e2e test this

@adohe-zz
Copy link

ERROR: (gcloud.container.clusters.create) Operation [<Operation
 name: u'operation-1475161376626-e6423faf'
 operationType: OperationTypeValueValuesEnum(CREATE_CLUSTER, 1)
 selfLink: u'https://test-container.sandbox.googleapis.com/v1/projects/154064302321/zones/us-central1-f/operations/operation-1475161376626-e6423faf'
 status: StatusValueValuesEnum(DONE, 3)
 statusMessage: u'Timed out waiting for cluster initialization. Cluster API may not be available.'
 targetLink: u'https://test-container.sandbox.googleapis.com/v1/projects/154064302321/zones/us-central1-f/clusters/e2e-gke-agent-pr-31-0'
 zone: u'us-central1-f'>] finished with error: Timed out waiting for cluster initialization. Cluster API may not be available.
2016/09/29 08:24:34 e2e.go:646: Step 'up' finished in 21m42.214576779s
2016/09/29 08:24:34 e2e.go:143: Saved XML output to /workspace/_artifacts/junit_runner.xml.
2016/09/29 08:24:34 e2e.go:160: Something went wrong: starting e2e cluster: error running up: exit status 1

}

// Assumes the flag has a default value.
func GetFlagInt32(cmd *cobra.Command, flag string) int32 {

Choose a reason for hiding this comment

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

any need to keep this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adohe It is no longer needed, I went ahead and removed it 577b5a5

@k8s-ci-robot
Copy link
Contributor

Jenkins GKE smoke e2e failed for commit 577b5a548a763086ad4ddf60ef8aab15237e4dc4. Full PR test history.

The magic incantation to run this job again is @k8s-bot gke e2e test this. Please help us cut down flakes by linking to an open flake issue when you hit one in your PR.

@juanvallejo
Copy link
Contributor Author

@adohe I don't think I can tell the bot to re-test. I believe the gke e2e test is flaking on #23737

This patch adds the option to set a nodeport when creating a NodePort
service. In case of a port allocation error due to a specified port
being out of the valid range, the error now includes the valid
range. If a `--node-port` value is not specified, it defaults to zero, in
which case the allocator will default to its current behavior of
assigning an available port.

This patch also adds a new helper function in `cmd/util/helpers.go` to
retrieve `Int32` cobra flags.

**Example**
```
$ kubectl create service nodeport mynodeport --tcp=8080:7777 --node-port=1
The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid
value: 1: provided port is not in the valid range. Valid ports range
from 30000-32767

$ kubectl create service nodeport mynodeport --tcp=8080:7777 --node-port=30000
service "mynodeport" created

$ oc describe service mynodeport
Name:                   mynodeport
Namespace:              default
Labels:                 app=mynodeport
Selector:               app=mynodeport
Type:                   NodePort
IP:                     172.30.81.254
Port:                   8080-7777       8080/TCP
NodePort:               8080-7777       30000/TCP
Endpoints:              <none>
Session Affinity:       None
No events.
```
@juanvallejo juanvallejo force-pushed the jvallejo/add-option-to-set-nodeport branch from 577b5a5 to 7d1461b Compare October 4, 2016 17:19
@pwittrock
Copy link
Member

pkg/kubectl/cmd/create_service.go, line 137 at r1 (raw file):

Previously, AdoHe (TonyAdo) wrote…

I agree with @lavalamp , we could use int directly here.

Update?

Comments from Reviewable

@juanvallejo
Copy link
Contributor Author

Review status: 0 of 7 files reviewed at latest revision, 3 unresolved discussions.


pkg/kubectl/cmd/create_service.go, line 137 at r1 (raw file):

Previously, pwittrock (Phillip Wittrock) wrote…

Update?

I went ahead and updated the flag to receive an `int` https://github.com//pull/33319/files#diff-65c6682164bc3185c68546044a5ffb11R137,

I then store it in the generator as an int as well https://github.com/kubernetes/kubernetes/pull/33319/files#diff-dac1b3be3f5aa420a4ada15749324a1eR34

and then store it in a api.ServicePod object as an int32 https://github.com/kubernetes/kubernetes/pull/33319/files#diff-dac1b3be3f5aa420a4ada15749324a1eR186


Comments from Reviewable

@pwittrock pwittrock added lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-label-needed labels Oct 5, 2016
@k8s-github-robot
Copy link

@k8s-bot test this [submit-queue is verifying that this PR is safe to merge]

@k8s-github-robot
Copy link

Automatic merge from submit-queue

@k8s-github-robot k8s-github-robot merged commit 05192d9 into kubernetes:master Oct 5, 2016
@juanvallejo juanvallejo deleted the jvallejo/add-option-to-set-nodeport branch October 6, 2016 15:17
juanvallejo added a commit to juanvallejo/origin that referenced this pull request Oct 6, 2016
UPSTREAM: kubernetes/kubernetes#33319

This patch adds the option to set a nodeport when creating a NodePort
service. In case of a port allocation error due to a specified port
being out of the valid range, the error now includes the valid
range. If a `--node-port` value is not specified, it defaults to zero, in
which case the allocator will default to its current behavior of
assigning an available port.

This patch also adds a new helper function in `cmd/util/helpers.go` to
retrieve `Int32` cobra flags.

**Example**
```
$ oc create service nodeport mynodeport --tcp=8080:7777 --node-port=1
The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid
value: 1: provided port is not in the valid range. Valid ports range
from 30000-32767

$ oc create service nodeport mynodeport --tcp=8080:7777 --node-port=30000
service "mynodeport" created

$ oc describe service mynodeport
Name:                   mynodeport
Namespace:              default
Labels:                 app=mynodeport
Selector:               app=mynodeport
Type:                   NodePort
IP:                     172.30.81.254
Port:                   8080-7777       8080/TCP
NodePort:               8080-7777       30000/TCP
Endpoints:              <none>
Session Affinity:       None
No events.
```
juanvallejo added a commit to juanvallejo/origin that referenced this pull request Oct 6, 2016
UPSTREAM: kubernetes/kubernetes#33319

This patch adds the option to set a nodeport when creating a NodePort
service. In case of a port allocation error due to a specified port
being out of the valid range, the error now includes the valid
range. If a `--node-port` value is not specified, it defaults to zero, in
which case the allocator will default to its current behavior of
assigning an available port.

This patch also adds a new helper function in `cmd/util/helpers.go` to
retrieve `Int32` cobra flags.

**Example**
```
$ oc create service nodeport mynodeport --tcp=8080:7777 --node-port=1
The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid
value: 1: provided port is not in the valid range. Valid ports range
from 30000-32767

$ oc create service nodeport mynodeport --tcp=8080:7777 --node-port=30000
service "mynodeport" created

$ oc describe service mynodeport
Name:                   mynodeport
Namespace:              default
Labels:                 app=mynodeport
Selector:               app=mynodeport
Type:                   NodePort
IP:                     172.30.81.254
Port:                   8080-7777       8080/TCP
NodePort:               8080-7777       30000/TCP
Endpoints:              <none>
Session Affinity:       None
No events.
```
smarterclayton pushed a commit to smarterclayton/kubernetes that referenced this pull request Oct 25, 2016
UPSTREAM: kubernetes#33319

This patch adds the option to set a nodeport when creating a NodePort
service. In case of a port allocation error due to a specified port
being out of the valid range, the error now includes the valid
range. If a `--node-port` value is not specified, it defaults to zero, in
which case the allocator will default to its current behavior of
assigning an available port.

This patch also adds a new helper function in `cmd/util/helpers.go` to
retrieve `Int32` cobra flags.

**Example**
```
$ oc create service nodeport mynodeport --tcp=8080:7777 --node-port=1
The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid
value: 1: provided port is not in the valid range. Valid ports range
from 30000-32767

$ oc create service nodeport mynodeport --tcp=8080:7777 --node-port=30000
service "mynodeport" created

$ oc describe service mynodeport
Name:                   mynodeport
Namespace:              default
Labels:                 app=mynodeport
Selector:               app=mynodeport
Type:                   NodePort
IP:                     172.30.81.254
Port:                   8080-7777       8080/TCP
NodePort:               8080-7777       30000/TCP
Endpoints:              <none>
Session Affinity:       None
No events.
```

:100644 100644 8dd3aa6... 5d1d0e1... M	pkg/kubectl/cmd/create_service.go
:100644 100644 6789920... 24b8ccf... M	pkg/kubectl/cmd/util/helpers.go
:100644 100644 ee080aa... 9f1c3d2... M	pkg/kubectl/service_basic.go
:100644 100644 a6d565b... 3a5407d... M	pkg/registry/service/portallocator/allocator.go
:100644 100644 d060528... 7386172... M	pkg/registry/service/portallocator/allocator_test.go
:100644 100644 90ebcb1... 1098605... M	pkg/registry/service/portallocator/controller/repair.go
ncdc pushed a commit to ncdc/kubernetes that referenced this pull request Nov 17, 2016
UPSTREAM: kubernetes#33319

This patch adds the option to set a nodeport when creating a NodePort
service. In case of a port allocation error due to a specified port
being out of the valid range, the error now includes the valid
range. If a `--node-port` value is not specified, it defaults to zero, in
which case the allocator will default to its current behavior of
assigning an available port.

This patch also adds a new helper function in `cmd/util/helpers.go` to
retrieve `Int32` cobra flags.

**Example**
```
$ oc create service nodeport mynodeport --tcp=8080:7777 --node-port=1
The Service "mynodeport" is invalid: spec.ports[0].nodePort: Invalid
value: 1: provided port is not in the valid range. Valid ports range
from 30000-32767

$ oc create service nodeport mynodeport --tcp=8080:7777 --node-port=30000
service "mynodeport" created

$ oc describe service mynodeport
Name:                   mynodeport
Namespace:              default
Labels:                 app=mynodeport
Selector:               app=mynodeport
Type:                   NodePort
IP:                     172.30.81.254
Port:                   8080-7777       8080/TCP
NodePort:               8080-7777       30000/TCP
Endpoints:              <none>
Session Affinity:       None
No events.
```

:100644 100644 8dd3aa6... 5d1d0e1... M	pkg/kubectl/cmd/create_service.go
:100644 100644 6789920... 24b8ccf... M	pkg/kubectl/cmd/util/helpers.go
:100644 100644 ee080aa... 9f1c3d2... M	pkg/kubectl/service_basic.go
:100644 100644 a6d565b... 3a5407d... M	pkg/registry/service/portallocator/allocator.go
:100644 100644 d060528... 7386172... M	pkg/registry/service/portallocator/allocator_test.go
:100644 100644 90ebcb1... 1098605... M	pkg/registry/service/portallocator/controller/repair.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants