@@ -47,7 +47,7 @@ func TestReplicaSetEvents(t *testing.T) {
4747 reporter , err := newPodEventLogger (ctx , podEventLoggerOptions {
4848 client : client ,
4949 coderURL : agentURL ,
50- namespace : namespace ,
50+ namespaces : [] string { namespace } ,
5151 logger : slogtest .Make (t , nil ).Leveled (slog .LevelDebug ),
5252 logDebounce : 5 * time .Second ,
5353 clock : cMock ,
@@ -144,7 +144,7 @@ func TestPodEvents(t *testing.T) {
144144 reporter , err := newPodEventLogger (ctx , podEventLoggerOptions {
145145 client : client ,
146146 coderURL : agentURL ,
147- namespace : namespace ,
147+ namespaces : [] string { namespace } ,
148148 logger : slogtest .Make (t , nil ).Leveled (slog .LevelDebug ),
149149 logDebounce : 5 * time .Second ,
150150 clock : cMock ,
@@ -221,6 +221,153 @@ func TestPodEvents(t *testing.T) {
221221 require .NoError (t , err )
222222}
223223
224+ func Test_newPodEventLogger_multipleNamespaces (t * testing.T ) {
225+ t .Parallel ()
226+
227+ api := newFakeAgentAPI (t )
228+
229+ ctx := testutil .Context (t , testutil .WaitShort )
230+ agentURL , err := url .Parse (api .server .URL )
231+ require .NoError (t , err )
232+ namespaces := []string {"test-namespace1" , "test-namespace2" }
233+ client := fake .NewSimpleClientset ()
234+
235+ cMock := quartz .NewMock (t )
236+ reporter , err := newPodEventLogger (ctx , podEventLoggerOptions {
237+ client : client ,
238+ coderURL : agentURL ,
239+ namespaces : namespaces ,
240+ logger : slogtest .Make (t , nil ).Leveled (slog .LevelDebug ),
241+ logDebounce : 5 * time .Second ,
242+ clock : cMock ,
243+ })
244+ require .NoError (t , err )
245+
246+ // Create a pod in the test-namespace1 namespace
247+ pod1 := & corev1.Pod {
248+ ObjectMeta : v1.ObjectMeta {
249+ Name : "test-pod-1" ,
250+ Namespace : "test-namespace1" ,
251+ CreationTimestamp : v1.Time {
252+ Time : time .Now ().Add (time .Hour ),
253+ },
254+ },
255+ Spec : corev1.PodSpec {
256+ Containers : []corev1.Container {
257+ {
258+ Env : []corev1.EnvVar {
259+ {
260+ Name : "CODER_AGENT_TOKEN" ,
261+ Value : "test-token-1" ,
262+ },
263+ },
264+ },
265+ },
266+ },
267+ }
268+ _ , err = client .CoreV1 ().Pods ("test-namespace1" ).Create (ctx , pod1 , v1.CreateOptions {})
269+ require .NoError (t , err )
270+
271+ // Create a pod in the test-namespace2 namespace
272+ pod2 := & corev1.Pod {
273+ ObjectMeta : v1.ObjectMeta {
274+ Name : "test-pod-2" ,
275+ Namespace : "test-namespace2" ,
276+ CreationTimestamp : v1.Time {
277+ Time : time .Now ().Add (time .Hour ),
278+ },
279+ },
280+ Spec : corev1.PodSpec {
281+ Containers : []corev1.Container {
282+ {
283+ Env : []corev1.EnvVar {
284+ {
285+ Name : "CODER_AGENT_TOKEN" ,
286+ Value : "test-token-2" ,
287+ },
288+ },
289+ },
290+ },
291+ },
292+ }
293+ _ , err = client .CoreV1 ().Pods ("test-namespace2" ).Create (ctx , pod2 , v1.CreateOptions {})
294+ require .NoError (t , err )
295+
296+ // Wait for both pods to be registered
297+ source1 := testutil .RequireRecvCtx (ctx , t , api .logSource )
298+ require .Equal (t , sourceUUID , source1 .ID )
299+ require .Equal (t , "Kubernetes" , source1 .DisplayName )
300+ require .Equal (t , "/icon/k8s.png" , source1 .Icon )
301+
302+ source2 := testutil .RequireRecvCtx (ctx , t , api .logSource )
303+ require .Equal (t , sourceUUID , source2 .ID )
304+ require .Equal (t , "Kubernetes" , source2 .DisplayName )
305+ require .Equal (t , "/icon/k8s.png" , source2 .Icon )
306+
307+ // Wait for both creation logs
308+ logs1 := testutil .RequireRecvCtx (ctx , t , api .logs )
309+ require .Len (t , logs1 , 1 )
310+ require .Contains (t , logs1 [0 ].Output , "Created pod" )
311+
312+ logs2 := testutil .RequireRecvCtx (ctx , t , api .logs )
313+ require .Len (t , logs2 , 1 )
314+ require .Contains (t , logs2 [0 ].Output , "Created pod" )
315+
316+ // Create an event in the first namespace
317+ event1 := & corev1.Event {
318+ ObjectMeta : v1.ObjectMeta {
319+ Name : "test-event-1" ,
320+ Namespace : "test-namespace1" ,
321+ CreationTimestamp : v1.Time {
322+ Time : time .Now ().Add (time .Hour ),
323+ },
324+ },
325+ InvolvedObject : corev1.ObjectReference {
326+ Kind : "Pod" ,
327+ Name : "test-pod-1" ,
328+ Namespace : "test-namespace1" ,
329+ },
330+ Reason : "Test" ,
331+ Message : "Test event for namespace1" ,
332+ }
333+ _ , err = client .CoreV1 ().Events ("test-namespace1" ).Create (ctx , event1 , v1.CreateOptions {})
334+ require .NoError (t , err )
335+
336+ // Wait for the event log
337+ eventLogs := testutil .RequireRecvCtx (ctx , t , api .logs )
338+ require .Len (t , eventLogs , 1 )
339+ require .Contains (t , eventLogs [0 ].Output , "Test event for namespace1" )
340+
341+ // Create an event in the first namespace
342+ event2 := & corev1.Event {
343+ ObjectMeta : v1.ObjectMeta {
344+ Name : "test-event-2" ,
345+ Namespace : "test-namespace2" ,
346+ CreationTimestamp : v1.Time {
347+ Time : time .Now ().Add (time .Hour ),
348+ },
349+ },
350+ InvolvedObject : corev1.ObjectReference {
351+ Kind : "Pod" ,
352+ Name : "test-pod-2" ,
353+ Namespace : "test-namespace2" ,
354+ },
355+ Reason : "Test" ,
356+ Message : "Test event for namespace2" ,
357+ }
358+ _ , err = client .CoreV1 ().Events ("test-namespace2" ).Create (ctx , event2 , v1.CreateOptions {})
359+ require .NoError (t , err )
360+
361+ // Wait for the event log
362+ eventLogs2 := testutil .RequireRecvCtx (ctx , t , api .logs )
363+ require .Len (t , eventLogs2 , 1 )
364+ require .Contains (t , eventLogs2 [0 ].Output , "Test event for namespace2" )
365+
366+ // Clean up
367+ err = reporter .Close ()
368+ require .NoError (t , err )
369+ }
370+
224371func Test_tokenCache (t * testing.T ) {
225372 t .Parallel ()
226373
0 commit comments