@@ -164,7 +164,8 @@ func (r *Reconciler) reconcile(ctx context.Context, log *zap.SugaredLogger, pubR
164164 ars := & kcpapisv1alpha1.APIResourceSchema {}
165165 err = r .kcpClient .Get (ctx , types.NamespacedName {Name : arsName }, ars , & ctrlruntimeclient.GetOptions {})
166166
167- if apierrors .IsNotFound (err ) {
167+ switch {
168+ case apierrors .IsNotFound (err ):
168169 ars , err := kcp .CreateAPIResourceSchema (projectedCRD , arsName , r .agentName )
169170 if err != nil {
170171 return nil , fmt .Errorf ("failed to construct APIResourceSchema: %w" , err )
@@ -175,8 +176,40 @@ func (r *Reconciler) reconcile(ctx context.Context, log *zap.SugaredLogger, pubR
175176 if err := r .kcpClient .Create (ctx , ars ); err != nil {
176177 return nil , fmt .Errorf ("failed to create APIResourceSchema: %w" , err )
177178 }
178- } else if err != nil {
179+
180+ case err != nil :
179181 return nil , fmt .Errorf ("failed to check for APIResourceSchema: %w" , err )
182+
183+ default :
184+ // A bug in earlier api-syncagent versions made any agent potentially reconcile any PublishedResource,
185+ // ignoring the configured PR filter. This would lead to the wrong agent name in the ARS labels
186+ // and annotations.
187+ // ARS are immutable and normally we would never need to update the metadata on one, we do it
188+ // temporarily anyway to fix broken metadata. The metadata is of informational nature only,
189+ // so having the wrong values is just a cosmetic issue.
190+ //
191+ // TODO: Remove this at some point when we're sure enough all ARS out there have been fixed.
192+
193+ validAnnotation := ars .Annotations [syncagentv1alpha1 .AgentNameAnnotation ] == r .agentName
194+ validLabel := ars .Labels [syncagentv1alpha1 .AgentNameLabel ] == r .agentName
195+
196+ if ! validAnnotation || ! validLabel {
197+ if ars .Labels == nil {
198+ ars .Labels = map [string ]string {}
199+ }
200+ if ars .Annotations == nil {
201+ ars .Annotations = map [string ]string {}
202+ }
203+
204+ ars .Labels [syncagentv1alpha1 .AgentNameLabel ] = r .agentName
205+ ars .Annotations [syncagentv1alpha1 .AgentNameAnnotation ] = r .agentName
206+
207+ log .With ("name" , arsName ).Info ("Fixing incorrect agent metadata…" )
208+
209+ if err := r .kcpClient .Update (ctx , ars ); err != nil {
210+ return nil , fmt .Errorf ("failed to update APIResourceSchema: %w" , err )
211+ }
212+ }
180213 }
181214
182215 // update Status with ARS name
0 commit comments