@@ -28,13 +28,15 @@ import (
2828 "github.com/tidwall/gjson"
2929 "go.uber.org/zap"
3030
31+ "github.com/kcp-dev/api-syncagent/internal/projection"
3132 "github.com/kcp-dev/api-syncagent/internal/sync/templating"
3233 syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1"
3334
3435 corev1 "k8s.io/api/core/v1"
3536 apierrors "k8s.io/apimachinery/pkg/api/errors"
3637 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3738 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
39+ "k8s.io/apimachinery/pkg/runtime/schema"
3840 "k8s.io/apimachinery/pkg/types"
3941 ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
4042)
@@ -97,11 +99,13 @@ func (s *ResourceSyncer) processRelatedResource(ctx context.Context, log *zap.Su
9799 return strings .Compare (aKey , bKey )
98100 })
99101
100- // Synchronize objects the same way the parent object was synchronized.
102+ // Synchronize related objects the same way the parent object was synchronized.
103+ projectedAPIVersion , projectedKind := projection .RelatedResourceProjectedGVK (& relRes ).ToAPIVersionAndKind ()
104+
101105 for idx , resolved := range resolvedObjects {
102106 destObject := & unstructured.Unstructured {}
103- destObject .SetAPIVersion ("v1" ) // we only support ConfigMaps and Secrets, both are in core/v1
104- destObject .SetKind (relRes . Kind )
107+ destObject .SetAPIVersion (projectedAPIVersion )
108+ destObject .SetKind (projectedKind )
105109
106110 if err = dest .client .Get (ctx , resolved .destination , destObject ); err != nil {
107111 destObject = nil
@@ -119,6 +123,8 @@ func (s *ResourceSyncer) processRelatedResource(ctx context.Context, log *zap.Su
119123 object : destObject ,
120124 }
121125
126+ fmt .Printf ("destobjct: %+v\n " , destObject )
127+
122128 syncer := objectSyncer {
123129 // Related objects within kcp are not labelled with the agent name because it's unnecessary.
124130 // agentName: "",
@@ -127,13 +133,16 @@ func (s *ResourceSyncer) processRelatedResource(ctx context.Context, log *zap.Su
127133 stateStore : stateStore ,
128134 // how to create a new destination object
129135 destCreator : func (source * unstructured.Unstructured ) (* unstructured.Unstructured , error ) {
136+ fmt .Printf ("source: %+v\n " , * source )
130137 dest := source .DeepCopy ()
138+ dest .SetAPIVersion (projectedAPIVersion )
139+ dest .SetKind (projectedKind )
131140 dest .SetName (resolved .destination .Name )
132141 dest .SetNamespace (resolved .destination .Namespace )
133142
134143 return dest , nil
135144 },
136- // ConfigMaps and Secrets have no subresources
145+ // reloated resources have no subresources
137146 subresources : nil ,
138147 // only sync the status back if the object originates in kcp,
139148 // as the service side should never have to rely on new status infos coming
@@ -171,8 +180,8 @@ func (s *ResourceSyncer) processRelatedResource(ctx context.Context, log *zap.Su
171180 value , err := json .Marshal (relatedObjectAnnotation {
172181 Namespace : resolved .destination .Namespace ,
173182 Name : resolved .destination .Name ,
174- APIVersion : "v1" , // we only support ConfigMaps and Secrets
175- Kind : relRes . Kind ,
183+ APIVersion : resolved . original . GetAPIVersion (),
184+ Kind : resolved . original . GetKind () ,
176185 })
177186 if err != nil {
178187 return false , fmt .Errorf ("failed to encode related object annotation: %w" , err )
@@ -355,8 +364,10 @@ func resolveRelatedResourceObjectsInNamespaces(ctx context.Context, relatedOrigi
355364 }
356365
357366 for originName , destName := range nameMap {
367+ apiVersion := schema.GroupVersion {Group : relRes .Group , Version : relRes .Version }.String ()
368+
358369 originObj := & unstructured.Unstructured {}
359- originObj .SetAPIVersion ("v1" ) // we only support ConfigMaps and Secrets, both are in core/v1
370+ originObj .SetAPIVersion (apiVersion )
360371 originObj .SetKind (relRes .Kind )
361372
362373 err = relatedOrigin .client .Get (ctx , types.NamespacedName {Name : originName , Namespace : originNamespace }, originObj )
@@ -407,8 +418,10 @@ func resolveRelatedResourceObjectsInNamespace(ctx context.Context, relatedOrigin
407418 return mapSlices (originNames , destNames ), nil
408419
409420 case spec .Selector != nil :
421+ apiVersion := schema.GroupVersion {Group : relRes .Group , Version : relRes .Version }.String ()
422+
410423 originObjects := & unstructured.UnstructuredList {}
411- originObjects .SetAPIVersion ("v1" ) // we only support ConfigMaps and Secrets, both are in core/v1
424+ originObjects .SetAPIVersion (apiVersion )
412425 originObjects .SetKind (relRes .Kind )
413426
414427 labelSelector , err := templateLabelSelector (relatedOrigin , relatedDest , relRes .Origin , & spec .Selector .LabelSelector )
0 commit comments