Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df99c20

Browse files
committed
Add option to set a 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** ``` $ 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. ```
1 parent 3149a2b commit df99c20

4 files changed

Lines changed: 26 additions & 8 deletions

File tree

pkg/kubectl/cmd/create_service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func NewCmdCreateServiceNodePort(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Co
134134
cmdutil.AddValidateFlags(cmd)
135135
cmdutil.AddPrinterFlags(cmd)
136136
cmdutil.AddGeneratorFlags(cmd, cmdutil.ServiceNodePortGeneratorV1Name)
137+
cmd.Flags().Int32("node-port", 0, "Port used to expose the service on each node in a cluster.")
137138
addPortFlags(cmd)
138139
return cmd
139140
}
@@ -152,6 +153,7 @@ func CreateServiceNodePort(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Comm
152153
TCP: cmdutil.GetFlagStringSlice(cmd, "tcp"),
153154
Type: api.ServiceTypeNodePort,
154155
ClusterIP: "",
156+
NodePort: cmdutil.GetFlagInt32(cmd, "node-port"),
155157
}
156158
default:
157159
return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName))

pkg/kubectl/cmd/util/helpers.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import (
4343
"k8s.io/kubernetes/pkg/util/sets"
4444
"k8s.io/kubernetes/pkg/util/strategicpatch"
4545

46-
"github.com/evanphx/json-patch"
46+
jsonpatch "github.com/evanphx/json-patch"
4747
"github.com/golang/glog"
4848
"github.com/spf13/cobra"
4949
)
@@ -290,7 +290,7 @@ func isWatch(cmd *cobra.Command) bool {
290290
func GetFlagString(cmd *cobra.Command, flag string) string {
291291
s, err := cmd.Flags().GetString(flag)
292292
if err != nil {
293-
glog.Fatalf("err accessing flag %s for command %s: %v", flag, cmd.Name(), err)
293+
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
294294
}
295295
return s
296296
}
@@ -299,7 +299,7 @@ func GetFlagString(cmd *cobra.Command, flag string) string {
299299
func GetFlagStringSlice(cmd *cobra.Command, flag string) []string {
300300
s, err := cmd.Flags().GetStringSlice(flag)
301301
if err != nil {
302-
glog.Fatalf("err accessing flag %s for command %s: %v", flag, cmd.Name(), err)
302+
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
303303
}
304304
return s
305305
}
@@ -316,7 +316,7 @@ func GetWideFlag(cmd *cobra.Command) bool {
316316
func GetFlagBool(cmd *cobra.Command, flag string) bool {
317317
b, err := cmd.Flags().GetBool(flag)
318318
if err != nil {
319-
glog.Fatalf("err accessing flag %s for command %s: %v", flag, cmd.Name(), err)
319+
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
320320
}
321321
return b
322322
}
@@ -325,7 +325,7 @@ func GetFlagBool(cmd *cobra.Command, flag string) bool {
325325
func GetFlagInt(cmd *cobra.Command, flag string) int {
326326
i, err := cmd.Flags().GetInt(flag)
327327
if err != nil {
328-
glog.Fatalf("err accessing flag %s for command %s: %v", flag, cmd.Name(), err)
328+
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
329329
}
330330
return i
331331
}
@@ -334,15 +334,24 @@ func GetFlagInt(cmd *cobra.Command, flag string) int {
334334
func GetFlagInt64(cmd *cobra.Command, flag string) int64 {
335335
i, err := cmd.Flags().GetInt64(flag)
336336
if err != nil {
337-
glog.Fatalf("err accessing flag %s for command %s: %v", flag, cmd.Name(), err)
337+
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
338+
}
339+
return i
340+
}
341+
342+
// Assumes the flag has a default value.
343+
func GetFlagInt32(cmd *cobra.Command, flag string) int32 {
344+
i, err := cmd.Flags().GetInt32(flag)
345+
if err != nil {
346+
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
338347
}
339348
return i
340349
}
341350

342351
func GetFlagDuration(cmd *cobra.Command, flag string) time.Duration {
343352
d, err := cmd.Flags().GetDuration(flag)
344353
if err != nil {
345-
glog.Fatalf("err accessing flag %s for command %s: %v", flag, cmd.Name(), err)
354+
glog.Fatalf("error accessing flag %s for command %s: %v", flag, cmd.Name(), err)
346355
}
347356
return d
348357
}

pkg/kubectl/service_basic.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type ServiceCommonGeneratorV1 struct {
3131
TCP []string
3232
Type api.ServiceType
3333
ClusterIP string
34+
NodePort int32
3435
}
3536

3637
type ServiceClusterIPGeneratorV1 struct {
@@ -56,6 +57,7 @@ func (ServiceNodePortGeneratorV1) ParamNames() []GeneratorParam {
5657
return []GeneratorParam{
5758
{"name", true},
5859
{"tcp", true},
60+
{"nodeport", true}
5961
}
6062
}
6163
func (ServiceLoadBalancerGeneratorV1) ParamNames() []GeneratorParam {
@@ -174,12 +176,14 @@ func (s ServiceCommonGeneratorV1) StructuredGenerate() (runtime.Object, error) {
174176
if err != nil {
175177
return nil, err
176178
}
179+
177180
portName := strings.Replace(tcpString, ":", "-", -1)
178181
ports = append(ports, api.ServicePort{
179182
Name: portName,
180183
Port: port,
181184
TargetPort: targetPort,
182185
Protocol: api.Protocol("TCP"),
186+
NodePort: s.NodePort
183187
})
184188
}
185189

pkg/registry/core/service/portallocator/allocator.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ func (r *PortAllocator) Free() int {
8282
func (r *PortAllocator) Allocate(port int) error {
8383
ok, offset := r.contains(port)
8484
if !ok {
85-
return ErrNotInRange
85+
// include valid port range in error
86+
validPorts := r.portRange.String()
87+
msg := fmt.Sprintf("Valid ports range is %s", validPorts)
88+
return fmt.Errorf("%v. %s", ErrNotInRange, msg)
8689
}
8790

8891
allocated, err := r.alloc.Allocate(offset)

0 commit comments

Comments
 (0)