Rename cache's endpointSliceInfo/endpointInfo to endpointSliceData/endpointData

EndpointSliceCache was using the name "endpointInfo" to refer to two
different data types (most egregiously in addEndpoints(), which had a
variable named `endpoint` of type `*endpointInfo` and a variable named
`endpointInfo` of type `Endpoint`).

Continue using "endpointInfo" in places that refer to proxy.Endpoint /
BaseEndpointInfo, since that's consistent with other code, but rename
the local "cache of the Endpoints field of an EndpointSlice" type from
"endpointInfo" to "endpointData". Likewise, rename endpointSliceInfo
to endpointSliceData, for consistency.
This commit is contained in:
Dan Winship 2023-11-20 10:53:41 -05:00
parent 764cb0457f
commit a8a12be3d3
3 changed files with 50 additions and 50 deletions

View File

@ -1745,6 +1745,6 @@ func initializeCache(endpointSliceCache *EndpointSliceCache, endpointSlices []*d
for _, tracker := range endpointSliceCache.trackerByServiceMap {
tracker.applied = tracker.pending
tracker.pending = endpointSliceInfoByName{}
tracker.pending = endpointSliceDataByName{}
}
}

View File

@ -58,26 +58,26 @@ type EndpointSliceCache struct {
// by a proxier along with any pending EndpointSlices that have been updated
// in this cache but not yet applied by a proxier.
type endpointSliceTracker struct {
applied endpointSliceInfoByName
pending endpointSliceInfoByName
applied endpointSliceDataByName
pending endpointSliceDataByName
}
// endpointSliceInfoByName groups endpointSliceInfo by the names of the
// endpointSliceDataByName groups endpointSliceData by the names of the
// corresponding EndpointSlices.
type endpointSliceInfoByName map[string]*endpointSliceInfo
type endpointSliceDataByName map[string]*endpointSliceData
// endpointSliceInfo contains just the attributes kube-proxy cares about.
// endpointSliceData contains just the attributes kube-proxy cares about.
// Used for caching. Intentionally small to limit memory util.
type endpointSliceInfo struct {
type endpointSliceData struct {
Ports []discovery.EndpointPort
Endpoints []*endpointInfo
Endpoints []*endpointData
Remove bool
}
// endpointInfo contains just the attributes kube-proxy cares about.
// endpointData contains just the attributes kube-proxy cares about.
// Used for caching. Intentionally small to limit memory util.
// Addresses, NodeName, and Zone are copied from EndpointSlice Endpoints.
type endpointInfo struct {
type endpointData struct {
Addresses []string
NodeName *string
Zone *string
@ -105,26 +105,26 @@ func NewEndpointSliceCache(hostname string, ipFamily v1.IPFamily, recorder event
// newEndpointSliceTracker initializes an endpointSliceTracker.
func newEndpointSliceTracker() *endpointSliceTracker {
return &endpointSliceTracker{
applied: endpointSliceInfoByName{},
pending: endpointSliceInfoByName{},
applied: endpointSliceDataByName{},
pending: endpointSliceDataByName{},
}
}
// newEndpointSliceInfo generates endpointSliceInfo from an EndpointSlice.
func newEndpointSliceInfo(endpointSlice *discovery.EndpointSlice, remove bool) *endpointSliceInfo {
esInfo := &endpointSliceInfo{
// newEndpointSliceData generates endpointSliceData from an EndpointSlice.
func newEndpointSliceData(endpointSlice *discovery.EndpointSlice, remove bool) *endpointSliceData {
esData := &endpointSliceData{
Ports: make([]discovery.EndpointPort, len(endpointSlice.Ports)),
Endpoints: []*endpointInfo{},
Endpoints: []*endpointData{},
Remove: remove,
}
// copy here to avoid mutating shared EndpointSlice object.
copy(esInfo.Ports, endpointSlice.Ports)
sort.Sort(byPort(esInfo.Ports))
copy(esData.Ports, endpointSlice.Ports)
sort.Sort(byPort(esData.Ports))
if !remove {
for _, endpoint := range endpointSlice.Endpoints {
epInfo := &endpointInfo{
epData := &endpointData{
Addresses: endpoint.Addresses,
Zone: endpoint.Zone,
NodeName: endpoint.NodeName,
@ -137,20 +137,20 @@ func newEndpointSliceInfo(endpointSlice *discovery.EndpointSlice, remove bool) *
if utilfeature.DefaultFeatureGate.Enabled(features.TopologyAwareHints) {
if endpoint.Hints != nil && len(endpoint.Hints.ForZones) > 0 {
epInfo.ZoneHints = sets.New[string]()
epData.ZoneHints = sets.New[string]()
for _, zone := range endpoint.Hints.ForZones {
epInfo.ZoneHints.Insert(zone.Name)
epData.ZoneHints.Insert(zone.Name)
}
}
}
esInfo.Endpoints = append(esInfo.Endpoints, epInfo)
esData.Endpoints = append(esData.Endpoints, epData)
}
sort.Sort(byAddress(esInfo.Endpoints))
sort.Sort(byAddress(esData.Endpoints))
}
return esInfo
return esData
}
// standardEndpointInfo is the default makeEndpointFunc.
@ -166,7 +166,7 @@ func (cache *EndpointSliceCache) updatePending(endpointSlice *discovery.Endpoint
return false
}
esInfo := newEndpointSliceInfo(endpointSlice, remove)
esData := newEndpointSliceData(endpointSlice, remove)
cache.lock.Lock()
defer cache.lock.Unlock()
@ -175,10 +175,10 @@ func (cache *EndpointSliceCache) updatePending(endpointSlice *discovery.Endpoint
cache.trackerByServiceMap[serviceKey] = newEndpointSliceTracker()
}
changed := cache.esInfoChanged(serviceKey, sliceKey, esInfo)
changed := cache.esDataChanged(serviceKey, sliceKey, esData)
if changed {
cache.trackerByServiceMap[serviceKey].pending[sliceKey] = esInfo
cache.trackerByServiceMap[serviceKey].pending[sliceKey] = esData
}
return changed
@ -201,11 +201,11 @@ func (cache *EndpointSliceCache) checkoutChanges() map[types.NamespacedName]*end
change.previous = cache.getEndpointsMap(serviceNN, esTracker.applied)
for name, sliceInfo := range esTracker.pending {
if sliceInfo.Remove {
for name, sliceData := range esTracker.pending {
if sliceData.Remove {
delete(esTracker.applied, name)
} else {
esTracker.applied[name] = sliceInfo
esTracker.applied[name] = sliceData
}
delete(esTracker.pending, name)
@ -223,17 +223,17 @@ func (cache *EndpointSliceCache) checkoutChanges() map[types.NamespacedName]*end
type spToEndpointMap map[ServicePortName]map[string]Endpoint
// getEndpointsMap computes an EndpointsMap for a given set of EndpointSlices.
func (cache *EndpointSliceCache) getEndpointsMap(serviceNN types.NamespacedName, sliceInfoByName endpointSliceInfoByName) EndpointsMap {
endpointInfoBySP := cache.endpointInfoByServicePort(serviceNN, sliceInfoByName)
func (cache *EndpointSliceCache) getEndpointsMap(serviceNN types.NamespacedName, sliceDataByName endpointSliceDataByName) EndpointsMap {
endpointInfoBySP := cache.endpointInfoByServicePort(serviceNN, sliceDataByName)
return endpointsMapFromEndpointInfo(endpointInfoBySP)
}
// endpointInfoByServicePort groups endpoint info by service port name and address.
func (cache *EndpointSliceCache) endpointInfoByServicePort(serviceNN types.NamespacedName, sliceInfoByName endpointSliceInfoByName) spToEndpointMap {
func (cache *EndpointSliceCache) endpointInfoByServicePort(serviceNN types.NamespacedName, sliceDataByName endpointSliceDataByName) spToEndpointMap {
endpointInfoBySP := spToEndpointMap{}
for _, sliceInfo := range sliceInfoByName {
for _, port := range sliceInfo.Ports {
for _, sliceData := range sliceDataByName {
for _, port := range sliceData.Ports {
if port.Name == nil {
klog.ErrorS(nil, "Ignoring port with nil name", "portName", port.Name)
continue
@ -250,7 +250,7 @@ func (cache *EndpointSliceCache) endpointInfoByServicePort(serviceNN types.Names
Protocol: *port.Protocol,
}
endpointInfoBySP[svcPortName] = cache.addEndpoints(&svcPortName, int(*port.Port), endpointInfoBySP[svcPortName], sliceInfo.Endpoints)
endpointInfoBySP[svcPortName] = cache.addEndpoints(&svcPortName, int(*port.Port), endpointInfoBySP[svcPortName], sliceData.Endpoints)
}
}
@ -258,7 +258,7 @@ func (cache *EndpointSliceCache) endpointInfoByServicePort(serviceNN types.Names
}
// addEndpoints adds endpointInfo for each unique endpoint.
func (cache *EndpointSliceCache) addEndpoints(svcPortName *ServicePortName, portNum int, endpointSet map[string]Endpoint, endpoints []*endpointInfo) map[string]Endpoint {
func (cache *EndpointSliceCache) addEndpoints(svcPortName *ServicePortName, portNum int, endpointSet map[string]Endpoint, endpoints []*endpointData) map[string]Endpoint {
if endpointSet == nil {
endpointSet = map[string]Endpoint{}
}
@ -299,29 +299,29 @@ func (cache *EndpointSliceCache) isLocal(hostname string) bool {
return len(cache.hostname) > 0 && hostname == cache.hostname
}
// esInfoChanged returns true if the esInfo parameter should be set as a new
// esDataChanged returns true if the esData parameter should be set as a new
// pending value in the cache.
func (cache *EndpointSliceCache) esInfoChanged(serviceKey types.NamespacedName, sliceKey string, esInfo *endpointSliceInfo) bool {
func (cache *EndpointSliceCache) esDataChanged(serviceKey types.NamespacedName, sliceKey string, esData *endpointSliceData) bool {
if _, ok := cache.trackerByServiceMap[serviceKey]; ok {
appliedInfo, appliedOk := cache.trackerByServiceMap[serviceKey].applied[sliceKey]
pendingInfo, pendingOk := cache.trackerByServiceMap[serviceKey].pending[sliceKey]
appliedData, appliedOk := cache.trackerByServiceMap[serviceKey].applied[sliceKey]
pendingData, pendingOk := cache.trackerByServiceMap[serviceKey].pending[sliceKey]
// If there's already a pending value, return whether or not this would
// change that.
if pendingOk {
return !reflect.DeepEqual(esInfo, pendingInfo)
return !reflect.DeepEqual(esData, pendingData)
}
// If there's already an applied value, return whether or not this would
// change that.
if appliedOk {
return !reflect.DeepEqual(esInfo, appliedInfo)
return !reflect.DeepEqual(esData, appliedData)
}
}
// If this is marked for removal and does not exist in the cache, no changes
// are necessary.
if esInfo.Remove {
if esData.Remove {
return false
}
@ -373,8 +373,8 @@ func endpointSliceCacheKeys(endpointSlice *discovery.EndpointSlice) (types.Names
return types.NamespacedName{Namespace: endpointSlice.Namespace, Name: serviceName}, endpointSlice.Name, err
}
// byAddress helps sort endpointInfo
type byAddress []*endpointInfo
// byAddress helps sort endpointData
type byAddress []*endpointData
func (e byAddress) Len() int {
return len(e)

View File

@ -328,7 +328,7 @@ func TestEndpointInfoByServicePort(t *testing.T) {
}
}
func TestEsInfoChanged(t *testing.T) {
func TestEsDataChanged(t *testing.T) {
p80 := int32(80)
p443 := int32(443)
port80 := discovery.EndpointPort{Port: &p80, Name: ptr.To("http"), Protocol: ptr.To(v1.ProtocolTCP)}
@ -454,11 +454,11 @@ func TestEsInfoChanged(t *testing.T) {
t.Fatalf("Expected no error calling endpointSliceCacheKeys(): %v", err)
}
esInfo := newEndpointSliceInfo(tc.updatedSlice, false)
changed := tc.cache.esInfoChanged(serviceKey, sliceKey, esInfo)
esData := newEndpointSliceData(tc.updatedSlice, false)
changed := tc.cache.esDataChanged(serviceKey, sliceKey, esData)
if tc.expectChanged != changed {
t.Errorf("Expected esInfoChanged() to return %t, got %t", tc.expectChanged, changed)
t.Errorf("Expected esDataChanged() to return %t, got %t", tc.expectChanged, changed)
}
cmc.Check(t)