Skip to content

Commit 363bb39

Browse files
authored
Use utils.net to parse ports instead of atoi (kubernetes#89120)
1 parent 85ee5fd commit 363bb39

File tree

10 files changed

+20
-12
lines changed

10 files changed

+20
-12
lines changed

cmd/kubeadm/app/util/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ go_library(
3636
"//vendor/github.com/pkg/errors:go_default_library",
3737
"//vendor/k8s.io/klog:go_default_library",
3838
"//vendor/k8s.io/utils/exec:go_default_library",
39+
"//vendor/k8s.io/utils/net:go_default_library",
3940
],
4041
)
4142

cmd/kubeadm/app/util/endpoint.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"k8s.io/apimachinery/pkg/util/validation"
2828
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
29+
utilsnet "k8s.io/utils/net"
2930
)
3031

3132
// GetControlPlaneEndpoint returns a properly formatted endpoint for the control plane built according following rules:
@@ -115,7 +116,7 @@ func ParseHostPort(hostport string) (string, string, error) {
115116
// ParsePort parses a string representing a TCP port.
116117
// If the string is not a valid representation of a TCP port, ParsePort returns an error.
117118
func ParsePort(port string) (int, error) {
118-
portInt, err := strconv.Atoi(port)
119+
portInt, err := utilsnet.ParsePort(port, true)
119120
if err == nil && (1 <= portInt && portInt <= 65535) {
120121
return portInt, nil
121122
}

pkg/registry/core/rest/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ go_library(
6262
"//staging/src/k8s.io/client-go/kubernetes/typed/policy/v1beta1:go_default_library",
6363
"//staging/src/k8s.io/client-go/rest:go_default_library",
6464
"//vendor/k8s.io/klog:go_default_library",
65+
"//vendor/k8s.io/utils/net:go_default_library",
6566
],
6667
)
6768

pkg/registry/core/rest/storage_core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"net"
2222
"net/http"
2323
"net/url"
24-
"strconv"
2524
"strings"
2625
"time"
2726

@@ -66,6 +65,7 @@ import (
6665
serviceaccountstore "k8s.io/kubernetes/pkg/registry/core/serviceaccount/storage"
6766
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
6867
"k8s.io/kubernetes/pkg/serviceaccount"
68+
utilsnet "k8s.io/utils/net"
6969
)
7070

7171
// LegacyRESTStorageProvider provides information needed to build RESTStorage for core, but
@@ -360,7 +360,7 @@ func (s componentStatusStorage) serversToValidate() map[string]*componentstatus.
360360
klog.Errorf("Failed to split host/port: %s (%v)", etcdUrl.Host, err)
361361
continue
362362
}
363-
port, _ = strconv.Atoi(portString)
363+
port, _ = utilsnet.ParsePort(portString, true)
364364
} else {
365365
addr = etcdUrl.Host
366366
port = 2379

pkg/util/flag/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
"//staging/src/k8s.io/apimachinery/pkg/util/net:go_default_library",
1515
"//vendor/github.com/spf13/pflag:go_default_library",
1616
"//vendor/k8s.io/klog:go_default_library",
17+
"//vendor/k8s.io/utils/net:go_default_library",
1718
],
1819
)
1920

pkg/util/flag/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ package flag
1919
import (
2020
"fmt"
2121
"net"
22-
"strconv"
2322

2423
"github.com/spf13/pflag"
2524
"k8s.io/klog"
2625

2726
utilnet "k8s.io/apimachinery/pkg/util/net"
27+
utilsnet "k8s.io/utils/net"
2828
)
2929

3030
// PrintFlags logs the flags in the flagset
@@ -109,7 +109,7 @@ func (v IPPortVar) Set(s string) error {
109109
if net.ParseIP(host) == nil {
110110
return fmt.Errorf("%q is not a valid IP address", host)
111111
}
112-
if _, err := strconv.Atoi(port); err != nil {
112+
if _, err := utilsnet.ParsePort(port, true); err != nil {
113113
return fmt.Errorf("%q is not a valid number", port)
114114
}
115115
*v.Val = s

staging/src/k8s.io/apiserver/pkg/server/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import (
6666
"k8s.io/component-base/logs"
6767
"k8s.io/klog"
6868
openapicommon "k8s.io/kube-openapi/pkg/common"
69+
utilsnet "k8s.io/utils/net"
6970

7071
// install apis
7172
_ "k8s.io/apiserver/pkg/apis/apiserver/install"
@@ -731,7 +732,7 @@ func (s *SecureServingInfo) HostPort() (string, int, error) {
731732
if err != nil {
732733
return "", 0, fmt.Errorf("failed to get port from listener address %q: %v", addr, err)
733734
}
734-
port, err := strconv.Atoi(portStr)
735+
port, err := utilsnet.ParsePort(portStr, true)
735736
if err != nil {
736737
return "", 0, fmt.Errorf("invalid non-numeric port %q", portStr)
737738
}

staging/src/k8s.io/kubectl/pkg/generate/versioned/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ go_library(
4747
"//staging/src/k8s.io/kubectl/pkg/generate:go_default_library",
4848
"//staging/src/k8s.io/kubectl/pkg/util:go_default_library",
4949
"//staging/src/k8s.io/kubectl/pkg/util/hash:go_default_library",
50+
"//vendor/k8s.io/utils/net:go_default_library",
5051
],
5152
)
5253

staging/src/k8s.io/kubectl/pkg/generate/versioned/service_basic.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"k8s.io/apimachinery/pkg/util/intstr"
2828
"k8s.io/apimachinery/pkg/util/validation"
2929
"k8s.io/kubectl/pkg/generate"
30+
utilsnet "k8s.io/utils/net"
3031
)
3132

3233
type ServiceCommonGeneratorV1 struct {
@@ -86,15 +87,11 @@ func (ServiceExternalNameGeneratorV1) ParamNames() []generate.GeneratorParam {
8687
func parsePorts(portString string) (int32, intstr.IntOrString, error) {
8788
portStringSlice := strings.Split(portString, ":")
8889

89-
port, err := strconv.Atoi(portStringSlice[0])
90+
port, err := utilsnet.ParsePort(portStringSlice[0], true)
9091
if err != nil {
9192
return 0, intstr.FromInt(0), err
9293
}
9394

94-
if errs := validation.IsValidPortNum(port); len(errs) != 0 {
95-
return 0, intstr.FromInt(0), fmt.Errorf(strings.Join(errs, ","))
96-
}
97-
9895
if len(portStringSlice) == 1 {
9996
return int32(port), intstr.FromInt(int(port)), nil
10097
}

staging/src/k8s.io/kubectl/pkg/generate/versioned/service_basic_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,12 @@ func TestParsePorts(t *testing.T) {
175175
portString: "-5:1234",
176176
expectPort: 0,
177177
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
178-
expectErr: "must be between 1 and 65535, inclusive",
178+
expectErr: "parsing \"-5\": invalid syntax",
179+
},
180+
{
181+
portString: "0:1234",
182+
expectPort: 0,
183+
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 1234},
179184
},
180185
{
181186
portString: "5:65536",

0 commit comments

Comments
 (0)