@@ -64,15 +64,20 @@ func labeledRole(role string, labels map[string]string) string {
64
64
return fmt .Sprintf ("%s%s%d" , role , xds .KeyDelimiter , utils .HashLabels (labels ))
65
65
}
66
66
67
+ // note: if `ns“ is empty, we assume the user doesn't want to use pod locality info, so we won't modify the role.
67
68
func newUniqlyConnectedClient (node * envoy_config_core_v3.Node , ns string , labels map [string ]string , locality PodLocality ) UniqlyConnectedClient {
68
69
role := node .GetMetadata ().GetFields ()[xds .RoleKey ].GetStringValue ()
69
- snapshotKey := labeledRole (role , labels )
70
+ resourceName := role
71
+ if ns != "" {
72
+ snapshotKey := labeledRole (role , labels )
73
+ resourceName = fmt .Sprintf ("%s%s%s" , snapshotKey , xds .KeyDelimiter , ns )
74
+ }
70
75
return UniqlyConnectedClient {
71
76
Role : role ,
72
77
Namespace : ns ,
73
78
Locality : locality ,
74
79
Labels : labels ,
75
- resourceName : fmt . Sprintf ( "%s%s%s" , snapshotKey , xds . KeyDelimiter , ns ) ,
80
+ resourceName : resourceName ,
76
81
}
77
82
}
78
83
@@ -91,6 +96,7 @@ type callbacks struct {
91
96
collection atomic.Pointer [callbacksCollection ]
92
97
}
93
98
99
+ // If augmentedPods is nil, we won't use the pod locality info, and all pods for the same gateway will receive the same config.
94
100
type UniquelyConnectedClientsBulider func (ctx context.Context , handler * krt.DebugHandler , augmentedPods krt.Collection [LocalityPod ]) krt.Collection [UniqlyConnectedClient ]
95
101
96
102
// THIS IS THE SET OF THINGS WE RUN TRANSLATION FOR
@@ -173,7 +179,9 @@ func (x *callbacksCollection) del(sid int64) *UniqlyConnectedClient {
173
179
func (x * callbacksCollection ) add (sid int64 , r * envoy_service_discovery_v3.DiscoveryRequest ) (string , bool , error ) {
174
180
175
181
var pod * LocalityPod
176
- if r .GetNode () != nil {
182
+ // see if user wants to use pod locality info
183
+ usePod := x .augmentedPods != nil
184
+ if usePod && r .GetNode () != nil {
177
185
podRef := getRef (r .GetNode ())
178
186
k := krt.Key [LocalityPod ](krt.Named {Name : podRef .Name , Namespace : podRef .Namespace }.ResourceName ())
179
187
pod = x .augmentedPods .GetKey (k )
@@ -183,13 +191,22 @@ func (x *callbacksCollection) add(sid int64, r *envoy_service_discovery_v3.Disco
183
191
defer x .stateLock .Unlock ()
184
192
c , ok := x .clients [sid ]
185
193
if ! ok {
186
- if pod == nil {
187
- // error if we can't get the pod
188
- return "" , false , fmt .Errorf ("pod not found for node %v" , r .GetNode ())
194
+ var locality PodLocality
195
+ var ns string
196
+ var labels map [string ]string
197
+ if usePod {
198
+ if pod == nil {
199
+ // we need to use the pod locality info, so it's an error if we can't get the pod
200
+ return "" , false , fmt .Errorf ("pod not found for node %v" , r .GetNode ())
201
+ } else {
202
+ locality = pod .Locality
203
+ ns = pod .Namespace
204
+ labels = pod .AugmentedLabels
205
+ }
189
206
}
190
- x .logger .Debug ("adding xds client" , zap .Any ("locality" , pod . Locality ), zap .String ("ns" , pod . Namespace ), zap .Any ("labels" , pod . AugmentedLabels ))
207
+ x .logger .Debug ("adding xds client" , zap .Any ("locality" , locality ), zap .String ("ns" , ns ), zap .Any ("labels" , labels ))
191
208
// TODO: modify request to include the label that are relevant for the client?
192
- ucc := newUniqlyConnectedClient (r .GetNode (), pod . Namespace , pod . AugmentedLabels , pod . Locality )
209
+ ucc := newUniqlyConnectedClient (r .GetNode (), ns , labels , locality )
193
210
c = newConnectedClient (ucc .resourceName )
194
211
x .clients [sid ] = c
195
212
currentUnique := x .uniqClientsCount [ucc .resourceName ]
@@ -277,6 +294,11 @@ func (x *callbacks) OnFetchRequest(ctx context.Context, r *envoy_service_discove
277
294
}
278
295
279
296
func (x * callbacksCollection ) fetchRequest (_ context.Context , r * envoy_service_discovery_v3.DiscoveryRequest ) error {
297
+ // nothing special to do in a fetch request, as we don't need to maintain state
298
+ if x .augmentedPods == nil {
299
+ return nil
300
+ }
301
+
280
302
var pod * LocalityPod
281
303
podRef := getRef (r .GetNode ())
282
304
k := krt.Key [LocalityPod ](krt.Named {Name : podRef .Name , Namespace : podRef .Namespace }.ResourceName ())
0 commit comments