mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-21 01:26:28 +00:00
Merge pull request #55591 from miaoyq/validate-port-of-service
Automatic merge from submit-queue (batch tested with PRs 54826, 53576, 55591, 54946, 54825). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Make sure the ports are valid when parsing portString **What this PR does / why we need it**: We need ensure the ports are valid when parsing `portString`. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes # **Special notes for your reviewer**: **Release note**: ```release-note ```
This commit is contained in:
commit
5888af447d
@ -89,14 +89,25 @@ func parsePorts(portString string) (int32, intstr.IntOrString, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, intstr.FromInt(0), err
|
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 {
|
if len(portStringSlice) == 1 {
|
||||||
return int32(port), intstr.FromInt(int(port)), nil
|
return int32(port), intstr.FromInt(int(port)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var targetPort intstr.IntOrString
|
var targetPort intstr.IntOrString
|
||||||
if portNum, err := strconv.Atoi(portStringSlice[1]); err != nil {
|
if portNum, err := strconv.Atoi(portStringSlice[1]); err != nil {
|
||||||
|
if errs := validation.IsValidPortName(portStringSlice[1]); len(errs) != 0 {
|
||||||
|
return 0, intstr.FromInt(0), fmt.Errorf(strings.Join(errs, ","))
|
||||||
|
}
|
||||||
targetPort = intstr.FromString(portStringSlice[1])
|
targetPort = intstr.FromString(portStringSlice[1])
|
||||||
} else {
|
} else {
|
||||||
|
if errs := validation.IsValidPortNum(portNum); len(errs) != 0 {
|
||||||
|
return 0, intstr.FromInt(0), fmt.Errorf(strings.Join(errs, ","))
|
||||||
|
}
|
||||||
targetPort = intstr.FromInt(portNum)
|
targetPort = intstr.FromInt(portNum)
|
||||||
}
|
}
|
||||||
return int32(port), targetPort, nil
|
return int32(port), targetPort, nil
|
||||||
|
@ -115,6 +115,20 @@ func TestServiceBasicGenerate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
expectErr: false,
|
expectErr: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "invalid-port",
|
||||||
|
tcp: []string{"65536"},
|
||||||
|
clusterip: "None",
|
||||||
|
serviceType: v1.ServiceTypeClusterIP,
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid-port-mapping",
|
||||||
|
tcp: []string{"8080:-abc"},
|
||||||
|
clusterip: "None",
|
||||||
|
serviceType: v1.ServiceTypeClusterIP,
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
expectErr: true,
|
expectErr: true,
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user