mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 18:24:07 +00:00
Refactor findPort in service pkg
This commit is contained in:
parent
b6956506fa
commit
ea0c6bcbfd
@ -138,21 +138,30 @@ func endpointsEqual(e *api.Endpoints, endpoints []string) bool {
|
|||||||
|
|
||||||
// findPort locates the container port for the given manifest and portName.
|
// findPort locates the container port for the given manifest and portName.
|
||||||
func findPort(manifest *api.ContainerManifest, portName util.IntOrString) (int, error) {
|
func findPort(manifest *api.ContainerManifest, portName util.IntOrString) (int, error) {
|
||||||
if ((portName.Kind == util.IntstrString && len(portName.StrVal) == 0) ||
|
firstContianerPort := -1
|
||||||
(portName.Kind == util.IntstrInt && portName.IntVal == 0)) &&
|
if len(manifest.Containers[0].Ports) > 0 {
|
||||||
len(manifest.Containers[0].Ports) > 0 {
|
firstContianerPort = manifest.Containers[0].Ports[0].ContainerPort
|
||||||
return manifest.Containers[0].Ports[0].ContainerPort, nil
|
|
||||||
}
|
}
|
||||||
if portName.Kind == util.IntstrInt {
|
|
||||||
return portName.IntVal, nil
|
switch portName.Kind {
|
||||||
}
|
case util.IntstrString:
|
||||||
name := portName.StrVal
|
if len(portName.StrVal) == 0 && firstContianerPort != -1 {
|
||||||
for _, container := range manifest.Containers {
|
return firstContianerPort, nil
|
||||||
for _, port := range container.Ports {
|
}
|
||||||
if port.Name == name {
|
name := portName.StrVal
|
||||||
return port.ContainerPort, nil
|
for _, container := range manifest.Containers {
|
||||||
|
for _, port := range container.Ports {
|
||||||
|
if port.Name == name {
|
||||||
|
return port.ContainerPort, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case util.IntstrInt:
|
||||||
|
if portName.IntVal == 0 && firstContianerPort != -1 {
|
||||||
|
return firstContianerPort, nil
|
||||||
|
}
|
||||||
|
return portName.IntVal, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1, fmt.Errorf("no suitable port for manifest: %s", manifest.ID)
|
return -1, fmt.Errorf("no suitable port for manifest: %s", manifest.ID)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user