mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-07 11:13:48 +00:00
Fix findPort: correctly handle the empty ports case
This commit is contained in:
parent
c5205870c0
commit
5a7c00754f
@ -145,9 +145,12 @@ func findPort(manifest *api.ContainerManifest, portName util.IntOrString) (int,
|
|||||||
|
|
||||||
switch portName.Kind {
|
switch portName.Kind {
|
||||||
case util.IntstrString:
|
case util.IntstrString:
|
||||||
if len(portName.StrVal) == 0 && firstContainerPort != -1 {
|
if len(portName.StrVal) == 0 {
|
||||||
|
if firstContainerPort != 0 {
|
||||||
return firstContainerPort, nil
|
return firstContainerPort, nil
|
||||||
}
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
name := portName.StrVal
|
name := portName.StrVal
|
||||||
for _, container := range manifest.Containers {
|
for _, container := range manifest.Containers {
|
||||||
for _, port := range container.Ports {
|
for _, port := range container.Ports {
|
||||||
@ -157,9 +160,12 @@ func findPort(manifest *api.ContainerManifest, portName util.IntOrString) (int,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case util.IntstrInt:
|
case util.IntstrInt:
|
||||||
if portName.IntVal == 0 && firstContainerPort != -1 {
|
if portName.IntVal == 0 {
|
||||||
|
if firstContainerPort != 0 {
|
||||||
return firstContainerPort, nil
|
return firstContainerPort, nil
|
||||||
}
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
return portName.IntVal, nil
|
return portName.IntVal, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,50 +79,77 @@ func TestFindPort(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
emptyPortsManifest := api.ContainerManifest{
|
||||||
|
Containers: []api.Container{
|
||||||
|
{
|
||||||
|
Ports: []api.Port{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
manifest api.ContainerManifest
|
||||||
portName util.IntOrString
|
portName util.IntOrString
|
||||||
|
|
||||||
wport int
|
wport int
|
||||||
werr bool
|
werr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
manifest,
|
||||||
util.IntOrString{Kind: util.IntstrString, StrVal: "foo"},
|
util.IntOrString{Kind: util.IntstrString, StrVal: "foo"},
|
||||||
8080,
|
8080,
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
manifest,
|
||||||
util.IntOrString{Kind: util.IntstrString, StrVal: "bar"},
|
util.IntOrString{Kind: util.IntstrString, StrVal: "bar"},
|
||||||
8000,
|
8000,
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
manifest,
|
||||||
util.IntOrString{Kind: util.IntstrInt, IntVal: 8000},
|
util.IntOrString{Kind: util.IntstrInt, IntVal: 8000},
|
||||||
8000,
|
8000,
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
manifest,
|
||||||
util.IntOrString{Kind: util.IntstrInt, IntVal: 7000},
|
util.IntOrString{Kind: util.IntstrInt, IntVal: 7000},
|
||||||
7000,
|
7000,
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
manifest,
|
||||||
util.IntOrString{Kind: util.IntstrString, StrVal: "baz"},
|
util.IntOrString{Kind: util.IntstrString, StrVal: "baz"},
|
||||||
0,
|
0,
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
manifest,
|
||||||
util.IntOrString{Kind: util.IntstrString, StrVal: ""},
|
util.IntOrString{Kind: util.IntstrString, StrVal: ""},
|
||||||
8080,
|
8080,
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
util.IntOrString{},
|
manifest,
|
||||||
|
util.IntOrString{Kind: util.IntstrInt, IntVal: 0},
|
||||||
8080,
|
8080,
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
emptyPortsManifest,
|
||||||
|
util.IntOrString{Kind: util.IntstrString, StrVal: ""},
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
emptyPortsManifest,
|
||||||
|
util.IntOrString{Kind: util.IntstrInt, IntVal: 0},
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
port, err := findPort(&manifest, test.portName)
|
port, err := findPort(&test.manifest, test.portName)
|
||||||
if port != test.wport {
|
if port != test.wport {
|
||||||
t.Errorf("Expected port %d, Got %d", test.wport, port)
|
t.Errorf("Expected port %d, Got %d", test.wport, port)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user