-
Notifications
You must be signed in to change notification settings - Fork 40k
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
Add option to set a service nodeport #33319
Conversation
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. |
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. ```
@k8s-bot ok to test |
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.
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 |
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.
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.
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.
imo, this will break people who use ErrNotInRange
to check allocate error.
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.
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.") |
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.
Why int32? There's already an int, just use that.
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.
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.
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.
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.
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.
I agree with @lavalamp , we could use int
directly here.
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? |
df99c20
to
878477b
Compare
Yes, if the port is already taken, it returns:
|
@lavalamp Thanks for the feedback, review comments addressed |
Jenkins GCE e2e failed for commit 35a9794a5a922ff8b0855597bc2945f387098131. Full PR test history. The magic incantation to run this job again is |
35a9794
to
d63848f
Compare
Jenkins verification failed for commit d63848f480ad0b8af3ba644827573a066571e9ab. Full PR test history. The magic incantation to run this job again is |
@adohe Would you mind doing a pass to make sure this makes sense. |
@pwittrock sure, I will check this. |
the error output should be 30000, right? |
@juanvallejo could you link the original issue here? I want to get more context. |
Ah, correct, I just checked, and doing:
gives the error:
while
gives the error
Once I try to allocate it for the second time.
|
24d0993
to
dc7f493
Compare
Jenkins GCI GKE smoke e2e failed for commit dc7f493. Full PR test history. The magic incantation to run this job again is |
@juanvallejo I think you still need to fix the e2e tests. |
@k8s-bot gke e2e test this |
@k8s-bot gci gke e2e test this |
|
} | ||
|
||
// Assumes the flag has a default value. | ||
func GetFlagInt32(cmd *cobra.Command, flag string) int32 { |
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.
any need to keep this function?
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.
Jenkins GKE smoke e2e failed for commit 577b5a548a763086ad4ddf60ef8aab15237e4dc4. Full PR test history. The magic incantation to run this job again is |
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. ```
577b5a5
to
7d1461b
Compare
pkg/kubectl/cmd/create_service.go, line 137 at r1 (raw file):
|
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):
|
@k8s-bot test this [submit-queue is verifying that this PR is safe to merge] |
Automatic merge from submit-queue |
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. ```
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. ```
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
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
Release note:
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, inwhich 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
toretrieve
Int32
cobra flags.Example
@fabianofranz
This change is