improve readability, code nesting too deep

Signed-off-by: aimuz <mr.imuz@gmail.com>
This commit is contained in:
aimuz 2022-09-22 23:10:20 +08:00
parent 429f71d958
commit b88b08b870
No known key found for this signature in database
GPG Key ID: 63C3DC9FBA22D9D7

View File

@ -619,35 +619,38 @@ func formatEndpoints(endpoints *api.Endpoints, ports sets.String) string {
ss := &endpoints.Subsets[i]
if len(ss.Ports) == 0 {
// It's possible to have headless services with no ports.
count += len(ss.Addresses)
for i := range ss.Addresses {
if len(list) == max {
more = true
// the next loop is redundant
break
}
if !more {
list = append(list, ss.Addresses[i].IP)
}
count++
list = append(list, ss.Addresses[i].IP)
}
} else {
// "Normal" services with ports defined.
for i := range ss.Ports {
port := &ss.Ports[i]
if ports == nil || ports.Has(port.Name) {
for i := range ss.Addresses {
if len(list) == max {
more = true
}
addr := &ss.Addresses[i]
if !more {
hostPort := net.JoinHostPort(addr.IP, strconv.Itoa(int(port.Port)))
list = append(list, hostPort)
}
count++
// avoid nesting code too deeply
continue
}
// "Normal" services with ports defined.
for i := range ss.Ports {
port := &ss.Ports[i]
if ports == nil || ports.Has(port.Name) {
count += len(ss.Addresses)
for i := range ss.Addresses {
if len(list) == max {
more = true
// the next loop is redundant
break
}
addr := &ss.Addresses[i]
hostPort := net.JoinHostPort(addr.IP, strconv.Itoa(int(port.Port)))
list = append(list, hostPort)
}
}
}
}
ret := strings.Join(list, ",")
if more {
return fmt.Sprintf("%s + %d more...", ret, count-max)