@@ -25,7 +25,9 @@ import (
2525 "go.uber.org/zap"
2626
2727 "github.com/kcp-dev/api-syncagent/internal/mutation"
28+ "github.com/kcp-dev/api-syncagent/internal/projection"
2829 syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1"
30+ "github.com/kcp-dev/logicalcluster/v3"
2931
3032 apierrors "k8s.io/apimachinery/pkg/api/errors"
3133 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -85,12 +87,22 @@ func (s *ResourceSyncer) processRelatedResource(log *zap.SugaredLogger, stateSto
8587 sourceObjs .SetKind (relRes .Kind )
8688
8789 // TODO: would need to handle replacer here as well to select right object.
90+ cn , ok := source .object .GetLabels ()[remoteObjectClusterLabel ]
91+ if ! ok {
92+ return false , fmt .Errorf ("missing cluster label on source object" )
93+ }
94+ clusterName := logicalcluster .Name (cn )
95+ labels := projection .GenerateLocalLabelSelector (& relRes , source .object , clusterName )
8896
89- if err := source .client .List (source .ctx , sourceObjs , ctrlruntimeclient .MatchingLabels (relRes . LabelSelector .MatchLabels )); err != nil {
97+ if err := source .client .List (source .ctx , sourceObjs , ctrlruntimeclient .MatchingLabels (labels .MatchLabels )); err != nil {
9098 return false , fmt .Errorf ("failed to list related objects: %w" , err )
9199 }
92- if len (sourceObjs .Items ) == 0 || len (sourceObjs .Items ) > 1 {
93- return false , fmt .Errorf ("expected exactly one related object, got %d" , len (sourceObjs .Items ))
100+ if len (sourceObjs .Items ) == 0 {
101+ return false , nil
102+ }
103+ if len (sourceObjs .Items ) > 1 {
104+ // HACK: we take first one, as we can't select multiple objects or by name only!
105+ log .Warnw ("found multiple related objects, taking first one" , "count" , len (sourceObjs .Items ))
94106 }
95107 sourceKey = & ctrlruntimeclient.ObjectKey {
96108 Namespace : sourceObjs .Items [0 ].GetNamespace (),
0 commit comments