@@ -61,6 +61,7 @@ type Reconciler struct {
6161 recorder record.EventRecorder
6262 lcName logicalcluster.Name
6363 agentName string
64+ prFilter labels.Selector
6465}
6566
6667// Add creates a new controller and adds it to the given manager.
@@ -81,6 +82,7 @@ func Add(
8182 log : log .Named (ControllerName ),
8283 recorder : mgr .GetEventRecorderFor (ControllerName ),
8384 agentName : agentName ,
85+ prFilter : prFilter ,
8486 }
8587
8688 _ , err := builder .ControllerManagedBy (mgr ).
@@ -98,7 +100,7 @@ func (r *Reconciler) enqueueMatchingPublishedResources(ctx context.Context, obj
98100 crd := obj .(* apiextensionsv1.CustomResourceDefinition )
99101
100102 pubResources := & syncagentv1alpha1.PublishedResourceList {}
101- if err := r .localClient .List (ctx , pubResources ); err != nil {
103+ if err := r .localClient .List (ctx , pubResources , & ctrlruntimeclient. ListOptions { LabelSelector : r . prFilter } ); err != nil {
102104 runtime .HandleError (err )
103105 return nil
104106 }
@@ -171,7 +173,8 @@ func (r *Reconciler) reconcile(ctx context.Context, log *zap.SugaredLogger, pubR
171173 ars := & kcpdevv1alpha1.APIResourceSchema {}
172174 err = r .kcpClient .Get (ctx , types.NamespacedName {Name : arsName }, ars , & ctrlruntimeclient.GetOptions {})
173175
174- if apierrors .IsNotFound (err ) {
176+ switch {
177+ case apierrors .IsNotFound (err ):
175178 ars , err := kcp .CreateAPIResourceSchema (projectedCRD , arsName , r .agentName )
176179 if err != nil {
177180 return nil , fmt .Errorf ("failed to construct APIResourceSchema: %w" , err )
@@ -182,8 +185,40 @@ func (r *Reconciler) reconcile(ctx context.Context, log *zap.SugaredLogger, pubR
182185 if err := r .kcpClient .Create (ctx , ars ); err != nil {
183186 return nil , fmt .Errorf ("failed to create APIResourceSchema: %w" , err )
184187 }
185- } else if err != nil {
188+
189+ case err != nil :
186190 return nil , fmt .Errorf ("failed to check for APIResourceSchema: %w" , err )
191+
192+ default :
193+ // A bug in earlier api-syncagent versions made any agent potentially reconcile any PublishedResource,
194+ // ignoring the configured PR filter. This would lead to the wrong agent name in the ARS labels
195+ // and annotations.
196+ // ARS are immutable and normally we would never need to update the metadata on one, we do it
197+ // temporarily anyway to fix broken metadata. The metadata is of informational nature only,
198+ // so having the wrong values is just a cosmetic issue.
199+ //
200+ // TODO: Remove this at some point when we're sure enough all ARS out there have been fixed.
201+
202+ validAnnotation := ars .Annotations [syncagentv1alpha1 .AgentNameAnnotation ] == r .agentName
203+ validLabel := ars .Labels [syncagentv1alpha1 .AgentNameLabel ] == r .agentName
204+
205+ if ! validAnnotation || ! validLabel {
206+ if ars .Labels == nil {
207+ ars .Labels = map [string ]string {}
208+ }
209+ if ars .Annotations == nil {
210+ ars .Annotations = map [string ]string {}
211+ }
212+
213+ ars .Labels [syncagentv1alpha1 .AgentNameLabel ] = r .agentName
214+ ars .Annotations [syncagentv1alpha1 .AgentNameAnnotation ] = r .agentName
215+
216+ log .With ("name" , arsName ).Info ("Fixing incorrect agent metadata…" )
217+
218+ if err := r .kcpClient .Update (ctx , ars ); err != nil {
219+ return nil , fmt .Errorf ("failed to update APIResourceSchema: %w" , err )
220+ }
221+ }
187222 }
188223
189224 // update Status with ARS name
0 commit comments