Skip to content

Commit b785485

Browse files
committed
fix(topology-extractor): idempotent endpoint tracking and README wording
Change endpoints map inner type from []Endpoint to map[NamespacedName]Endpoint. EventAddOrUpdate re-fires on endpoint updates, so the previous append caused duplicate entries for the same endpoint. Map assignment is idempotent. Remove the now-unused slices import. Fix README wording: "pod label" -> "endpoint label". Signed-off-by: Etai Lev Ran <elevran@gmail.com>
1 parent eaa98e2 commit b785485

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

pkg/epp/framework/plugins/datalayer/attribute/topology/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ Carries the locality of an endpoint. Populated once at endpoint creation.
1717
The following plugins produce this attribute:
1818

1919
- **`topology-extractor`** (Data Layer): Sets the `Topology` attribute using
20-
`spec.hostname` from the Pod object, or the value of a configured pod label.
20+
`spec.hostname` from the Pod object, or the value of a configured endpoint label.

pkg/epp/framework/plugins/datalayer/extractor/topology/extractor.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ package topology
2727
import (
2828
"context"
2929
"encoding/json"
30-
"slices"
3130
"sync"
3231

3332
corev1 "k8s.io/api/core/v1"
@@ -86,9 +85,10 @@ type TopologyExtractor struct {
8685
// mu guards endpoints and hostnames.
8786
mu sync.Mutex
8887
// endpoints maps pod identity to all live Endpoints for that pod.
89-
// Keyed by {PodName, Namespace} — one pod may have N rank endpoints.
88+
// Outer key: {PodName, Namespace}; inner key: endpoint NamespacedName.
89+
// One pod may have N rank endpoints, each with a distinct inner key.
9090
// Only endpoints whose hostname label was absent are tracked here.
91-
endpoints map[types.NamespacedName][]fwkdl.Endpoint
91+
endpoints map[types.NamespacedName]map[types.NamespacedName]fwkdl.Endpoint
9292
// hostnames caches spec.hostname per pod (ready pods only).
9393
// Populated by Pod notifications; cleared on pod delete.
9494
hostnames map[types.NamespacedName]string
@@ -120,7 +120,7 @@ func Factory(name string, parameters *json.Decoder, _ fwkplugin.Handle) (fwkplug
120120
zoneLabel: p.Zone,
121121
regionLabel: p.Region,
122122
dk: attrtopology.TopologyAttributeKey.WithNonEmptyProducerName(name),
123-
endpoints: make(map[types.NamespacedName][]fwkdl.Endpoint),
123+
endpoints: make(map[types.NamespacedName]map[types.NamespacedName]fwkdl.Endpoint),
124124
hostnames: make(map[types.NamespacedName]string),
125125
}, nil
126126
}
@@ -195,8 +195,12 @@ func (h *endpointHandler) Extract(_ context.Context, event fwkdl.EndpointEvent)
195195
if hn == "" {
196196
// Hostname label absent: track endpoint for spec.hostname fallback via pod notification.
197197
key := podKey(meta)
198+
epKey := meta.GetNamespacedName()
198199
h.ext.mu.Lock()
199-
h.ext.endpoints[key] = append(h.ext.endpoints[key], event.Endpoint)
200+
if h.ext.endpoints[key] == nil {
201+
h.ext.endpoints[key] = make(map[types.NamespacedName]fwkdl.Endpoint)
202+
}
203+
h.ext.endpoints[key][epKey] = event.Endpoint
200204
hn = h.ext.hostnames[key]
201205
h.ext.mu.Unlock()
202206
}
@@ -220,13 +224,9 @@ func (h *endpointHandler) removeEndpoint(meta *fwkdl.EndpointMetadata, _ fwkdl.E
220224
defer h.ext.mu.Unlock()
221225

222226
eps := h.ext.endpoints[key]
223-
eps = slices.DeleteFunc(eps, func(e fwkdl.Endpoint) bool {
224-
return e.GetMetadata().GetNamespacedName() == epKey
225-
})
227+
delete(eps, epKey)
226228
if len(eps) == 0 {
227229
delete(h.ext.endpoints, key)
228-
} else {
229-
h.ext.endpoints[key] = eps
230230
}
231231
}
232232

@@ -268,7 +268,10 @@ func (h *podNotificationHandler) Extract(_ context.Context, event fwkdl.Notifica
268268

269269
h.ext.mu.Lock()
270270
h.ext.hostnames[key] = hostname
271-
eps := slices.Clone(h.ext.endpoints[key])
271+
eps := make([]fwkdl.Endpoint, 0, len(h.ext.endpoints[key]))
272+
for _, ep := range h.ext.endpoints[key] {
273+
eps = append(eps, ep)
274+
}
272275
h.ext.mu.Unlock()
273276

274277
for _, ep := range eps {

0 commit comments

Comments
 (0)