@@ -45,23 +45,22 @@ func TestClusterCatalogUnpacking(t *testing.T) {
4545 require .Equal (ct , * managerDeployment .Spec .Replicas , managerDeployment .Status .ReadyReplicas )
4646 }, time .Minute , time .Second )
4747
48- var managerPod corev1.Pod
49- t .Log ("Waiting for only one controller-manager pod to remain" )
48+ t .Log ("Waiting for controller-manager pods to match the desired replica count" )
5049 require .EventuallyWithT (t , func (ct * assert.CollectT ) {
5150 var managerPods corev1.PodList
5251 err := c .List (ctx , & managerPods , client .MatchingLabels (managerLabelSelector ))
5352 require .NoError (ct , err )
54- require .Len (ct , managerPods .Items , 1 )
55- managerPod = managerPods .Items [0 ]
53+ require .Len (ct , managerPods .Items , int (* managerDeployment .Spec .Replicas ))
5654 }, time .Minute , time .Second )
5755
5856 t .Log ("Waiting for acquired leader election" )
5957 leaderCtx , leaderCancel := context .WithTimeout (ctx , 3 * time .Minute )
6058 defer leaderCancel ()
61- leaderSubstrings := []string {"successfully acquired lease" }
62- leaderElected , err := watchPodLogsForSubstring (leaderCtx , & managerPod , leaderSubstrings ... )
59+
60+ // When there are multiple replicas, find the leader pod
61+ managerPod , err := findLeaderPod (leaderCtx , "catalogd" )
6362 require .NoError (t , err )
64- require .True (t , leaderElected )
63+ require .NotNil (t , managerPod )
6564
6665 t .Log ("Reading logs to make sure that ClusterCatalog was reconciled by catalogdv1" )
6766 logCtx , cancel := context .WithTimeout (ctx , time .Minute )
@@ -70,7 +69,7 @@ func TestClusterCatalogUnpacking(t *testing.T) {
7069 "reconcile ending" ,
7170 fmt .Sprintf (`ClusterCatalog=%q` , testClusterCatalogName ),
7271 }
73- found , err := watchPodLogsForSubstring (logCtx , & managerPod , substrings ... )
72+ found , err := watchPodLogsForSubstring (logCtx , managerPod , substrings ... )
7473 require .NoError (t , err )
7574 require .True (t , found )
7675
@@ -115,10 +114,13 @@ func TestClusterExtensionAfterOLMUpgrade(t *testing.T) {
115114 leaderCtx , leaderCancel := context .WithTimeout (ctx , 3 * time .Minute )
116115 defer leaderCancel ()
117116
118- leaderSubstrings := [] string { "successfully acquired lease" }
119- leaderElected , err := watchPodLogsForSubstring (leaderCtx , managerPod , leaderSubstrings ... )
117+ // When there are multiple replicas, find the leader pod
118+ leaderPod , err := findLeaderPod (leaderCtx , "operator-controller" )
120119 require .NoError (t , err )
121- require .True (t , leaderElected )
120+ require .NotNil (t , leaderPod )
121+
122+ // Use the leader pod for subsequent operations
123+ managerPod = leaderPod
122124
123125 t .Log ("Reading logs to make sure that ClusterExtension was reconciled by operator-controller before we update it" )
124126 // Make sure that after we upgrade OLM itself we can still reconcile old objects without any changes
@@ -221,11 +223,48 @@ func waitForDeployment(t *testing.T, ctx context.Context, controlPlaneLabel stri
221223 t .Logf ("Ensure the number of remaining pods equal the desired number of replicas (%d)" , desiredNumReplicas )
222224 require .EventuallyWithT (t , func (ct * assert.CollectT ) {
223225 require .NoError (ct , c .List (ctx , & managerPods , client.MatchingLabelsSelector {Selector : deploymentLabelSelector }))
224- require .Len (ct , managerPods .Items , 1 )
226+ require .Len (ct , managerPods .Items , int ( desiredNumReplicas ) )
225227 }, time .Minute , time .Second )
226228 return & managerPods .Items [0 ]
227229}
228230
231+ // findLeaderPod finds the pod that has acquired the leader lease by checking logs of all pods
232+ func findLeaderPod (ctx context.Context , controlPlaneLabel string ) (* corev1.Pod , error ) {
233+ deploymentLabelSelector := labels.Set {"app.kubernetes.io/name" : controlPlaneLabel }.AsSelector ()
234+
235+ var managerPods corev1.PodList
236+ if err := c .List (ctx , & managerPods , client.MatchingLabelsSelector {Selector : deploymentLabelSelector }); err != nil {
237+ return nil , fmt .Errorf ("failed to list pods: %w" , err )
238+ }
239+
240+ if len (managerPods .Items ) == 0 {
241+ return nil , fmt .Errorf ("no pods found for label %s" , controlPlaneLabel )
242+ }
243+
244+ // If there's only one pod, it must be the leader
245+ if len (managerPods .Items ) == 1 {
246+ return & managerPods .Items [0 ], nil
247+ }
248+
249+ // Check each pod's logs for leader election message
250+ leaderSubstrings := []string {"successfully acquired lease" }
251+ for i := range managerPods .Items {
252+ pod := & managerPods .Items [i ]
253+
254+ // Check if this pod has acquired the lease
255+ isLeader , err := watchPodLogsForSubstring (ctx , pod , leaderSubstrings ... )
256+ if err != nil {
257+ // If we can't read logs from this pod, try the next one
258+ continue
259+ }
260+ if isLeader {
261+ return pod , nil
262+ }
263+ }
264+
265+ return nil , fmt .Errorf ("no leader pod found among %d pods" , len (managerPods .Items ))
266+ }
267+
229268func watchPodLogsForSubstring (ctx context.Context , pod * corev1.Pod , substrings ... string ) (bool , error ) {
230269 podLogOpts := corev1.PodLogOptions {
231270 Follow : true ,
0 commit comments