Simplify maps which had almost the exact same info

This commit is contained in:
Tim Hockin 2017-02-02 17:37:34 -08:00
parent 48ea304711
commit 8d24fc3984

View File

@ -643,11 +643,14 @@ func accumulateEndpointsMap(endpoints *api.Endpoints, hostname string,
// We need to build a map of portname -> all ip:ports for that
// portname. Explode Endpoints.Subsets[*] into this structure.
portsToEndpoints := map[string][]hostPortInfo{}
for i := range endpoints.Subsets {
ss := &endpoints.Subsets[i]
for i := range ss.Ports {
port := &ss.Ports[i]
svcPort := proxy.ServicePortName{
NamespacedName: types.NamespacedName{Namespace: endpoints.Namespace, Name: endpoints.Name},
Port: port.Name,
}
for i := range ss.Addresses {
addr := &ss.Addresses[i]
hostPortObject := hostPortInfo{
@ -655,17 +658,13 @@ func accumulateEndpointsMap(endpoints *api.Endpoints, hostname string,
port: int(port.Port),
isLocal: addr.NodeName != nil && *addr.NodeName == hostname,
}
portsToEndpoints[port.Name] = append(portsToEndpoints[port.Name], hostPortObject)
(*svcPortToInfoMap)[svcPort] = append((*svcPortToInfoMap)[svcPort], hostPortObject)
}
}
}
for portname := range portsToEndpoints {
svcPort := proxy.ServicePortName{
NamespacedName: types.NamespacedName{Namespace: endpoints.Namespace, Name: endpoints.Name},
Port: portname,
}
(*svcPortToInfoMap)[svcPort] = portsToEndpoints[portname]
newEPList := flattenValidEndpoints(portsToEndpoints[portname])
// Decompose the lists of endpoints into details of what was changed for the caller.
for svcPort, hostPortInfos := range *svcPortToInfoMap {
newEPList := flattenValidEndpoints(hostPortInfos)
// Flatten the list of current endpoint infos to just a list of ips as strings
curEndpointIPs := flattenEndpointsInfo(curEndpoints[svcPort])
if len(curEndpointIPs) != len(newEPList) || !slicesEquiv(slice.CopyStrings(curEndpointIPs), newEPList) {
@ -678,7 +677,7 @@ func accumulateEndpointsMap(endpoints *api.Endpoints, hostname string,
}
glog.V(3).Infof("Setting endpoints for %q to %+v", svcPort, newEPList)
// Once the set operations using the list of ips are complete, build the list of endpoint infos
(*newEndpoints)[svcPort] = buildEndpointInfoList(portsToEndpoints[portname], newEPList)
(*newEndpoints)[svcPort] = buildEndpointInfoList(hostPortInfos, newEPList)
}
}