@@ -158,22 +158,45 @@ func (r *reconciler) currentInstallPlan(ctx context.Context) (bool, *operatorsv1
158158 if installPlans == nil || len (installPlans .Items ) == 0 {
159159 return false , nil , nil
160160 }
161+ var currentInstallPlan * operatorsv1alpha1.InstallPlan
162+ multipleInstallPlans := false
161163 for _ , installPlan := range installPlans .Items {
162164 if len (installPlan .OwnerReferences ) == 0 || len (installPlan .Spec .ClusterServiceVersionNames ) == 0 {
163165 continue
164166 }
167+ ownerRefMatches := false
165168 for _ , ownerRef := range installPlan .OwnerReferences {
166169 if ownerRef .UID == subscription .UID {
167- for _ , csvName := range installPlan .Spec .ClusterServiceVersionNames {
168- if csvName == r .config .GatewayAPIOperatorVersion {
169- return true , & installPlan , nil
170- }
170+ ownerRefMatches = true
171+ break
172+ }
173+ }
174+ if ! ownerRefMatches {
175+ continue
176+ }
177+ // Ignore InstallPlans not in the "RequiresApproval" state. OLM may not be done setting them up.
178+ if installPlan .Status .Phase != operatorsv1alpha1 .InstallPlanPhaseRequiresApproval {
179+ continue
180+ }
181+ for _ , csvName := range installPlan .Spec .ClusterServiceVersionNames {
182+ if csvName == r .config .GatewayAPIOperatorVersion {
183+ // Keep the newest InstallPlan to return at the end of the loop.
184+ if currentInstallPlan == nil {
185+ currentInstallPlan = & installPlan
186+ break
187+ }
188+ multipleInstallPlans = true
189+ if currentInstallPlan .ObjectMeta .CreationTimestamp .Before (& installPlan .ObjectMeta .CreationTimestamp ) {
190+ currentInstallPlan = & installPlan
191+ break
171192 }
172193 }
173194 }
174195 }
175- // No valid InstallPlan found.
176- return false , nil , nil
196+ if multipleInstallPlans {
197+ log .Info (fmt .Sprintf ("found multiple valid InstallPlans. using %s because it's the newest" , currentInstallPlan .Name ))
198+ }
199+ return (currentInstallPlan != nil ), currentInstallPlan , nil
177200}
178201
179202// desiredInstallPlan returns a version of the expected InstallPlan that is approved.
0 commit comments