mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
Use utils.net to parse ports instead of atoi (#89120)
This commit is contained in:
parent
85ee5fdd90
commit
363bb39142
@ -36,6 +36,7 @@ go_library(
|
||||
"//vendor/github.com/pkg/errors:go_default_library",
|
||||
"//vendor/k8s.io/klog:go_default_library",
|
||||
"//vendor/k8s.io/utils/exec:go_default_library",
|
||||
"//vendor/k8s.io/utils/net:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
@ -26,6 +26,7 @@ import (
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||
utilsnet "k8s.io/utils/net"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
// ParsePort parses a string representing a TCP port.
|
||||
// If the string is not a valid representation of a TCP port, ParsePort returns an error.
|
||||
func ParsePort(port string) (int, error) {
|
||||
portInt, err := strconv.Atoi(port)
|
||||
portInt, err := utilsnet.ParsePort(port, true)
|
||||
if err == nil && (1 <= portInt && portInt <= 65535) {
|
||||
return portInt, nil
|
||||
}
|
||||
|
@ -62,6 +62,7 @@ go_library(
|
||||
"//staging/src/k8s.io/client-go/kubernetes/typed/policy/v1beta1:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/rest:go_default_library",
|
||||
"//vendor/k8s.io/klog:go_default_library",
|
||||
"//vendor/k8s.io/utils/net:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
@ -21,7 +21,6 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -66,6 +65,7 @@ import (
|
||||
serviceaccountstore "k8s.io/kubernetes/pkg/registry/core/serviceaccount/storage"
|
||||
kubeschedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
|
||||
"k8s.io/kubernetes/pkg/serviceaccount"
|
||||
utilsnet "k8s.io/utils/net"
|
||||
)
|
||||
|
||||
// LegacyRESTStorageProvider provides information needed to build RESTStorage for core, but
|
||||
@ -360,7 +360,7 @@ func (s componentStatusStorage) serversToValidate() map[string]*componentstatus.
|
||||
klog.Errorf("Failed to split host/port: %s (%v)", etcdUrl.Host, err)
|
||||
continue
|
||||
}
|
||||
port, _ = strconv.Atoi(portString)
|
||||
port, _ = utilsnet.ParsePort(portString, true)
|
||||
} else {
|
||||
addr = etcdUrl.Host
|
||||
port = 2379
|
||||
|
@ -14,6 +14,7 @@ go_library(
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/net:go_default_library",
|
||||
"//vendor/github.com/spf13/pflag:go_default_library",
|
||||
"//vendor/k8s.io/klog:go_default_library",
|
||||
"//vendor/k8s.io/utils/net:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
@ -19,12 +19,12 @@ package flag
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
"k8s.io/klog"
|
||||
|
||||
utilnet "k8s.io/apimachinery/pkg/util/net"
|
||||
utilsnet "k8s.io/utils/net"
|
||||
)
|
||||
|
||||
// PrintFlags logs the flags in the flagset
|
||||
@ -109,7 +109,7 @@ func (v IPPortVar) Set(s string) error {
|
||||
if net.ParseIP(host) == nil {
|
||||
return fmt.Errorf("%q is not a valid IP address", host)
|
||||
}
|
||||
if _, err := strconv.Atoi(port); err != nil {
|
||||
if _, err := utilsnet.ParsePort(port, true); err != nil {
|
||||
return fmt.Errorf("%q is not a valid number", port)
|
||||
}
|
||||
*v.Val = s
|
||||
|
@ -66,6 +66,7 @@ import (
|
||||
"k8s.io/component-base/logs"
|
||||
"k8s.io/klog"
|
||||
openapicommon "k8s.io/kube-openapi/pkg/common"
|
||||
utilsnet "k8s.io/utils/net"
|
||||
|
||||
// install apis
|
||||
_ "k8s.io/apiserver/pkg/apis/apiserver/install"
|
||||
@ -731,7 +732,7 @@ func (s *SecureServingInfo) HostPort() (string, int, error) {
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("failed to get port from listener address %q: %v", addr, err)
|
||||
}
|
||||
port, err := strconv.Atoi(portStr)
|
||||
port, err := utilsnet.ParsePort(portStr, true)
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("invalid non-numeric port %q", portStr)
|
||||
}
|
||||
|
@ -47,6 +47,7 @@ go_library(
|
||||
"//staging/src/k8s.io/kubectl/pkg/generate:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util/hash:go_default_library",
|
||||
"//vendor/k8s.io/utils/net:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
@ -27,6 +27,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
"k8s.io/kubectl/pkg/generate"
|
||||
utilsnet "k8s.io/utils/net"
|
||||
)
|
||||
|
||||
type ServiceCommonGeneratorV1 struct {
|
||||
@ -86,15 +87,11 @@ func (ServiceExternalNameGeneratorV1) ParamNames() []generate.GeneratorParam {
|
||||
func parsePorts(portString string) (int32, intstr.IntOrString, error) {
|
||||
portStringSlice := strings.Split(portString, ":")
|
||||
|
||||
port, err := strconv.Atoi(portStringSlice[0])
|
||||
port, err := utilsnet.ParsePort(portStringSlice[0], true)
|
||||
if err != nil {
|
||||
return 0, intstr.FromInt(0), err
|
||||
}
|
||||
|
||||
if errs := validation.IsValidPortNum(port); len(errs) != 0 {
|
||||
return 0, intstr.FromInt(0), fmt.Errorf(strings.Join(errs, ","))
|
||||
}
|
||||
|
||||
if len(portStringSlice) == 1 {
|
||||
return int32(port), intstr.FromInt(int(port)), nil
|
||||
}
|
||||
|
@ -175,7 +175,12 @@ func TestParsePorts(t *testing.T) {
|
||||
portString: "-5:1234",
|
||||
expectPort: 0,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
|
||||
expectErr: "must be between 1 and 65535, inclusive",
|
||||
expectErr: "parsing \"-5\": invalid syntax",
|
||||
},
|
||||
{
|
||||
portString: "0:1234",
|
||||
expectPort: 0,
|
||||
expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 1234},
|
||||
},
|
||||
{
|
||||
portString: "5:65536",
|
||||
|
Loading…
Reference in New Issue
Block a user