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