diff --git a/pkg/controllers/resources/configmaps/syncer.go b/pkg/controllers/resources/configmaps/syncer.go index 57a2427c2..5ff04fd67 100644 --- a/pkg/controllers/resources/configmaps/syncer.go +++ b/pkg/controllers/resources/configmaps/syncer.go @@ -60,10 +60,8 @@ func (s *configMapSyncer) ModifyController(ctx *synccontext.RegisterContext, bui } func (s *configMapSyncer) SyncToHost(ctx *synccontext.SyncContext, event *synccontext.SyncToHostEvent[*corev1.ConfigMap]) (ctrl.Result, error) { - createNeeded, err := s.isConfigMapUsed(ctx, event.Virtual) - if err != nil { - return ctrl.Result{}, err - } else if !createNeeded { + createNeeded := s.isConfigMapUsed(ctx, event.Virtual) + if !createNeeded { return ctrl.Result{}, nil } @@ -72,7 +70,7 @@ func (s *configMapSyncer) SyncToHost(ctx *synccontext.SyncContext, event *syncco } pObj := translate.HostMetadata(event.Virtual, s.VirtualToHost(ctx, types.NamespacedName{Name: event.Virtual.Name, Namespace: event.Virtual.Namespace}, event.Virtual)) - err = pro.ApplyPatchesHostObject(ctx, nil, pObj, event.Virtual, ctx.Config.Sync.ToHost.ConfigMaps.Patches) + err := pro.ApplyPatchesHostObject(ctx, nil, pObj, event.Virtual, ctx.Config.Sync.ToHost.ConfigMaps.Patches) if err != nil { return ctrl.Result{}, err } @@ -87,14 +85,12 @@ func (s *configMapSyncer) SyncToVirtual(ctx *synccontext.SyncContext, event *syn } vObj := translate.VirtualMetadata(event.Host, s.HostToVirtual(ctx, types.NamespacedName{Name: event.Host.Name, Namespace: event.Host.Namespace}, event.Host)) - createNeeded, err := s.isConfigMapUsed(ctx, vObj) - if err != nil { - return ctrl.Result{}, err - } else if !createNeeded { + createNeeded := s.isConfigMapUsed(ctx, vObj) + if !createNeeded { return ctrl.Result{}, nil } - err = pro.ApplyPatchesVirtualObject(ctx, nil, vObj, event.Host, ctx.Config.Sync.ToHost.ConfigMaps.Patches) + err := pro.ApplyPatchesVirtualObject(ctx, nil, vObj, event.Host, ctx.Config.Sync.ToHost.ConfigMaps.Patches) if err != nil { return ctrl.Result{}, err } @@ -103,12 +99,10 @@ func (s *configMapSyncer) SyncToVirtual(ctx *synccontext.SyncContext, event *syn } func (s *configMapSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.SyncEvent[*corev1.ConfigMap]) (_ ctrl.Result, retErr error) { - used, err := s.isConfigMapUsed(ctx, event.Virtual) - if err != nil { - return ctrl.Result{}, err - } else if !used { + used := s.isConfigMapUsed(ctx, event.Virtual) + if !used { ctx.Log.Infof("delete physical config map %s/%s, because it is not used anymore", event.Host.GetNamespace(), event.Host.GetName()) - err = ctx.PhysicalClient.Delete(ctx, event.Host) + err := ctx.PhysicalClient.Delete(ctx, event.Host) if err != nil { ctx.Log.Infof("error deleting physical object %s/%s in physical cluster: %v", event.Host.GetNamespace(), event.Host.GetName(), err) return ctrl.Result{}, err @@ -131,13 +125,12 @@ func (s *configMapSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext. } }() - // check annotations & labels - event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) - - // check labels + // bi-directional sync of annotations and labels if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) } @@ -147,11 +140,11 @@ func (s *configMapSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext. return ctrl.Result{}, nil } -func (s *configMapSyncer) isConfigMapUsed(ctx *synccontext.SyncContext, vObj *corev1.ConfigMap) (bool, error) { +func (s *configMapSyncer) isConfigMapUsed(ctx *synccontext.SyncContext, vObj *corev1.ConfigMap) bool { if vObj.Annotations[constants.SyncResourceAnnotation] == "true" { - return true, nil + return true } else if ctx.Config.Sync.ToHost.ConfigMaps.All { - return true, nil + return true } // retrieve references for config map @@ -163,5 +156,5 @@ func (s *configMapSyncer) isConfigMapUsed(ctx *synccontext.SyncContext, vObj *co }, }) - return len(references) > 0, nil + return len(references) > 0 } diff --git a/pkg/controllers/resources/csistoragecapacities/syncer.go b/pkg/controllers/resources/csistoragecapacities/syncer.go index fb786dcc4..5ee36fb0c 100644 --- a/pkg/controllers/resources/csistoragecapacities/syncer.go +++ b/pkg/controllers/resources/csistoragecapacities/syncer.go @@ -147,7 +147,7 @@ func (s *csistoragecapacitySyncer) enqueuePhysical(ctx *synccontext.SyncContext, return } - name := s.Mapper.HostToVirtual(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj) + name := s.HostToVirtual(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj) if name.Name != "" && name.Namespace != "" { q.Add(reconcile.Request{NamespacedName: name}) } diff --git a/pkg/controllers/resources/endpoints/syncer.go b/pkg/controllers/resources/endpoints/syncer.go index 904cf0e96..ade0e0b86 100644 --- a/pkg/controllers/resources/endpoints/syncer.go +++ b/pkg/controllers/resources/endpoints/syncer.go @@ -82,6 +82,14 @@ func (s *endpointsSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext. return ctrl.Result{}, err } + if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) + event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) + } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host, s.excludedAnnotations...) + event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) + } + return ctrl.Result{}, nil } diff --git a/pkg/controllers/resources/endpoints/translate.go b/pkg/controllers/resources/endpoints/translate.go index 44c111a37..129b2a74a 100644 --- a/pkg/controllers/resources/endpoints/translate.go +++ b/pkg/controllers/resources/endpoints/translate.go @@ -49,8 +49,5 @@ func (s *endpointsSyncer) translateUpdate(ctx *synccontext.SyncContext, pObj, vO s.translateSpec(ctx, translated) pObj.Subsets = translated.Subsets - // check annotations & labels - pObj.Annotations = translate.HostAnnotations(vObj, pObj, s.excludedAnnotations...) - pObj.Labels = translate.HostLabels(vObj, pObj) return nil } diff --git a/pkg/controllers/resources/ingresses/syncer.go b/pkg/controllers/resources/ingresses/syncer.go index 256d91c80..c1de22cfd 100644 --- a/pkg/controllers/resources/ingresses/syncer.go +++ b/pkg/controllers/resources/ingresses/syncer.go @@ -80,7 +80,7 @@ func (s *ingressSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.Sy event.TargetObject().Spec.IngressClassName = event.SourceObject().Spec.IngressClassName event.Virtual.Status = event.Host.Status - s.translateUpdate(ctx, event.Host, event.Virtual) + s.translateUpdate(ctx, event.Source, event.Host, event.Virtual) return ctrl.Result{}, nil } diff --git a/pkg/controllers/resources/ingresses/translate.go b/pkg/controllers/resources/ingresses/translate.go index 9fa3638b2..65f8d7338 100644 --- a/pkg/controllers/resources/ingresses/translate.go +++ b/pkg/controllers/resources/ingresses/translate.go @@ -40,13 +40,18 @@ func (s *ingressSyncer) TranslateMetadataUpdate(ctx *synccontext.SyncContext, vO return translate.HostAnnotations(vIngress, pObj), translate.HostLabels(vIngress, pObj) } -func (s *ingressSyncer) translateUpdate(ctx *synccontext.SyncContext, pObj, vObj *networkingv1.Ingress) { +func (s *ingressSyncer) translateUpdate(ctx *synccontext.SyncContext, source synccontext.SyncEventSource, pObj, vObj *networkingv1.Ingress) { pObj.Spec = *translateSpec(ctx, vObj.Namespace, &vObj.Spec) - var translatedAnnotations map[string]string - translatedAnnotations, pObj.Labels = s.TranslateMetadataUpdate(ctx, vObj, pObj) - translatedAnnotations, _ = resources.TranslateIngressAnnotations(ctx, translatedAnnotations, vObj.Namespace) - pObj.Annotations = translatedAnnotations + if source == synccontext.SyncEventSourceHost { + vObj.Annotations = translate.VirtualAnnotations(pObj, vObj) + vObj.Labels = translate.VirtualLabels(pObj, vObj) + } else { + var translatedAnnotations map[string]string + translatedAnnotations, pObj.Labels = s.TranslateMetadataUpdate(ctx, vObj, pObj) + translatedAnnotations, _ = resources.TranslateIngressAnnotations(ctx, translatedAnnotations, vObj.Namespace) + pObj.Annotations = translatedAnnotations + } } func translateSpec(ctx *synccontext.SyncContext, namespace string, vIngressSpec *networkingv1.IngressSpec) *networkingv1.IngressSpec { diff --git a/pkg/controllers/resources/namespaces/syncer.go b/pkg/controllers/resources/namespaces/syncer.go index 81acdae13..6807493b7 100644 --- a/pkg/controllers/resources/namespaces/syncer.go +++ b/pkg/controllers/resources/namespaces/syncer.go @@ -63,7 +63,7 @@ type namespaceSyncer struct { var _ syncertypes.Syncer = &namespaceSyncer{} func (s *namespaceSyncer) Syncer() syncertypes.Sync[client.Object] { - return syncer.ToGenericSyncer[*corev1.Namespace](s) + return syncer.ToGenericSyncer(s) } func (s *namespaceSyncer) SyncToHost(ctx *synccontext.SyncContext, event *synccontext.SyncToHostEvent[*corev1.Namespace]) (ctrl.Result, error) { diff --git a/pkg/controllers/resources/networkpolicies/syncer.go b/pkg/controllers/resources/networkpolicies/syncer.go index 0d98ba60e..9c08aab7c 100644 --- a/pkg/controllers/resources/networkpolicies/syncer.go +++ b/pkg/controllers/resources/networkpolicies/syncer.go @@ -67,6 +67,15 @@ func (s *networkPolicySyncer) Sync(ctx *synccontext.SyncContext, event *synccont }() s.translateUpdate(event.Host, event.Virtual) + + if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) + event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) + } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) + event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) + } + return ctrl.Result{}, nil } diff --git a/pkg/controllers/resources/networkpolicies/translate.go b/pkg/controllers/resources/networkpolicies/translate.go index bd130b8cf..3f6596837 100644 --- a/pkg/controllers/resources/networkpolicies/translate.go +++ b/pkg/controllers/resources/networkpolicies/translate.go @@ -19,9 +19,6 @@ func (s *networkPolicySyncer) translateUpdate(pObj, vObj *networkingv1.NetworkPo if translatedSpec := translateSpec(&vObj.Spec, vObj.GetNamespace()); translatedSpec != nil { pObj.Spec = *translatedSpec } - - pObj.Annotations = translate.HostAnnotations(vObj, pObj) - pObj.Labels = translate.HostLabels(vObj, pObj) } func translateSpec(spec *networkingv1.NetworkPolicySpec, namespace string) *networkingv1.NetworkPolicySpec { diff --git a/pkg/controllers/resources/persistentvolumeclaims/syncer.go b/pkg/controllers/resources/persistentvolumeclaims/syncer.go index 1fce29373..f96fe4ebe 100644 --- a/pkg/controllers/resources/persistentvolumeclaims/syncer.go +++ b/pkg/controllers/resources/persistentvolumeclaims/syncer.go @@ -171,17 +171,12 @@ func (s *persistentVolumeClaimSyncer) Sync(ctx *synccontext.SyncContext, event * // allow storage size to be increased event.Host.Spec.Resources.Requests = event.Virtual.Spec.Resources.Requests - // change annotations - if event.Source == synccontext.SyncEventSourceHost { - event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual, s.excludedAnnotations...) - } else { - event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host, s.excludedAnnotations...) - } - - // check labels + // bi-directional sync of annotations and labels if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) } diff --git a/pkg/controllers/resources/persistentvolumeclaims/translate.go b/pkg/controllers/resources/persistentvolumeclaims/translate.go index bf44cb6d0..ad81c8b46 100644 --- a/pkg/controllers/resources/persistentvolumeclaims/translate.go +++ b/pkg/controllers/resources/persistentvolumeclaims/translate.go @@ -11,9 +11,7 @@ import ( "k8s.io/apimachinery/pkg/types" ) -var ( - deprecatedStorageClassAnnotation = "volume.beta.kubernetes.io/storage-class" -) +var deprecatedStorageClassAnnotation = "volume.beta.kubernetes.io/storage-class" func (s *persistentVolumeClaimSyncer) translate(ctx *synccontext.SyncContext, vPvc *corev1.PersistentVolumeClaim) (*corev1.PersistentVolumeClaim, error) { pPVC := translate.HostMetadata(vPvc, s.VirtualToHost(ctx, types.NamespacedName{Name: vPvc.GetName(), Namespace: vPvc.GetNamespace()}, vPvc), s.excludedAnnotations...) @@ -21,9 +19,10 @@ func (s *persistentVolumeClaimSyncer) translate(ctx *synccontext.SyncContext, vP if vPvc.Annotations[constants.SkipTranslationAnnotation] != "true" { if pPVC.Spec.DataSource != nil { - if pPVC.Spec.DataSource.Kind == "VolumeSnapshot" { + switch pPVC.Spec.DataSource.Kind { + case "VolumeSnapshot": pPVC.Spec.DataSource.Name = mappings.VirtualToHostName(ctx, pPVC.Spec.DataSource.Name, vPvc.Namespace, mappings.VolumeSnapshots()) - } else if pPVC.Spec.DataSource.Kind == "PersistentVolumeClaim" { + case "PersistentVolumeClaim": pPVC.Spec.DataSource.Name = mappings.VirtualToHostName(ctx, pPVC.Spec.DataSource.Name, vPvc.Namespace, mappings.PersistentVolumeClaims()) } } @@ -34,9 +33,10 @@ func (s *persistentVolumeClaimSyncer) translate(ctx *synccontext.SyncContext, vP namespace = *pPVC.Spec.DataSourceRef.Namespace } - if pPVC.Spec.DataSourceRef.Kind == "VolumeSnapshot" { + switch pPVC.Spec.DataSourceRef.Kind { + case "VolumeSnapshot": pPVC.Spec.DataSourceRef.Name = mappings.VirtualToHostName(ctx, pPVC.Spec.DataSourceRef.Name, namespace, mappings.VolumeSnapshots()) - } else if pPVC.Spec.DataSourceRef.Kind == "PersistentVolumeClaim" { + case "PersistentVolumeClaim": pPVC.Spec.DataSourceRef.Name = mappings.VirtualToHostName(ctx, pPVC.Spec.DataSourceRef.Name, namespace, mappings.PersistentVolumeClaims()) } } diff --git a/pkg/controllers/resources/persistentvolumes/syncer.go b/pkg/controllers/resources/persistentvolumes/syncer.go index f87b72a11..72825d1c3 100644 --- a/pkg/controllers/resources/persistentvolumes/syncer.go +++ b/pkg/controllers/resources/persistentvolumes/syncer.go @@ -141,7 +141,7 @@ func (s *persistentVolumeSyncer) Sync(ctx *synccontext.SyncContext, event *syncc if event.Virtual.GetDeletionTimestamp() != nil { if event.Host.GetDeletionTimestamp() == nil { // check if the PV is dynamically provisioned and the reclaim policy is Delete - if !(event.Virtual.Spec.ClaimRef != nil && event.Virtual.Spec.PersistentVolumeReclaimPolicy == corev1.PersistentVolumeReclaimDelete) { + if event.Virtual.Spec.ClaimRef == nil || event.Virtual.Spec.PersistentVolumeReclaimPolicy != corev1.PersistentVolumeReclaimDelete { ctx.Log.Infof("delete physical persistent volume %s, because virtual persistent volume is deleted", event.Host.GetName()) err := ctx.PhysicalClient.Delete(ctx, event.Host) if err != nil { @@ -212,6 +212,13 @@ func (s *persistentVolumeSyncer) Sync(ctx *synccontext.SyncContext, event *syncc if event.Virtual.Annotations[constants.HostClusterPersistentVolumeAnnotation] == "" { // TODO: translate the storage secrets event.Host.Spec.StorageClassName = mappings.VirtualToHostName(ctx, event.Virtual.Spec.StorageClassName, "", mappings.StorageClasses()) + } + + // bi-directional sync of annotations and labels + if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) + event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) + } else { event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host, s.excludedAnnotations...) event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) } diff --git a/pkg/controllers/resources/poddisruptionbudgets/syncer.go b/pkg/controllers/resources/poddisruptionbudgets/syncer.go index 12431af91..cf24ced8e 100644 --- a/pkg/controllers/resources/poddisruptionbudgets/syncer.go +++ b/pkg/controllers/resources/poddisruptionbudgets/syncer.go @@ -68,6 +68,15 @@ func (s *pdbSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.SyncEv }() s.translateUpdate(event.Host, event.Virtual) + + if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) + event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) + } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) + event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) + } + return ctrl.Result{}, nil } diff --git a/pkg/controllers/resources/poddisruptionbudgets/translate.go b/pkg/controllers/resources/poddisruptionbudgets/translate.go index 32b151a25..61dfac4a7 100644 --- a/pkg/controllers/resources/poddisruptionbudgets/translate.go +++ b/pkg/controllers/resources/poddisruptionbudgets/translate.go @@ -14,8 +14,6 @@ func (s *pdbSyncer) translate(ctx *synccontext.SyncContext, vObj *policyv1.PodDi } func (s *pdbSyncer) translateUpdate(pObj, vObj *policyv1.PodDisruptionBudget) { - pObj.Annotations = translate.HostAnnotations(vObj, pObj) - pObj.Labels = translate.HostLabels(vObj, pObj) pObj.Spec.MaxUnavailable = vObj.Spec.MaxUnavailable pObj.Spec.MinAvailable = vObj.Spec.MinAvailable pObj.Spec.Selector = translate.HostLabelSelector(vObj.Spec.Selector) diff --git a/pkg/controllers/resources/pods/syncer.go b/pkg/controllers/resources/pods/syncer.go index 12a1a1fa4..d208e78dd 100644 --- a/pkg/controllers/resources/pods/syncer.go +++ b/pkg/controllers/resources/pods/syncer.go @@ -346,6 +346,14 @@ func (s *podSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.SyncEv } }() + if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) + event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) + } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) + event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) + } + // update the virtual pod if the spec has changed err = s.podTranslator.Diff(ctx, event) if err != nil { diff --git a/pkg/controllers/resources/runtimeclasses/syncer.go b/pkg/controllers/resources/runtimeclasses/syncer.go index db1c7548f..4f0a54edd 100644 --- a/pkg/controllers/resources/runtimeclasses/syncer.go +++ b/pkg/controllers/resources/runtimeclasses/syncer.go @@ -70,8 +70,14 @@ func (i *runtimeClassSyncer) Sync(ctx *synccontext.SyncContext, event *syncconte } }() - event.Virtual.Annotations = event.Host.Annotations - event.Virtual.Labels = event.Host.Labels + if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) + event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) + } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) + event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) + } + event.Virtual.Handler = event.Host.Handler event.Virtual.Overhead = event.Host.Overhead event.Virtual.Scheduling = event.Host.Scheduling diff --git a/pkg/controllers/resources/secrets/syncer.go b/pkg/controllers/resources/secrets/syncer.go index 87e23bd9d..fc96afb1d 100644 --- a/pkg/controllers/resources/secrets/syncer.go +++ b/pkg/controllers/resources/secrets/syncer.go @@ -52,7 +52,7 @@ type secretSyncer struct { var _ syncertypes.Syncer = &secretSyncer{} func (s *secretSyncer) Syncer() syncertypes.Sync[client.Object] { - return syncer.ToGenericSyncer[*corev1.Secret](s) + return syncer.ToGenericSyncer(s) } var _ syncertypes.ControllerModifier = &secretSyncer{} @@ -128,13 +128,11 @@ func (s *secretSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.Syn event.TargetObject().Type = event.SourceObject().Type } - // check annotations - event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) - - // check labels if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) } diff --git a/pkg/controllers/resources/serviceaccounts/syncer.go b/pkg/controllers/resources/serviceaccounts/syncer.go index 29867dc8f..e9f01b8e7 100644 --- a/pkg/controllers/resources/serviceaccounts/syncer.go +++ b/pkg/controllers/resources/serviceaccounts/syncer.go @@ -77,10 +77,11 @@ func (s *serviceAccountSyncer) Sync(ctx *synccontext.SyncContext, event *synccon } }() - event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) } diff --git a/pkg/controllers/resources/services/syncer.go b/pkg/controllers/resources/services/syncer.go index 4ad987e93..db898db6c 100644 --- a/pkg/controllers/resources/services/syncer.go +++ b/pkg/controllers/resources/services/syncer.go @@ -133,21 +133,18 @@ func (s *serviceSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.Sy // update status event.Virtual.Status = event.Host.Status - // check labels if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host, s.excludedAnnotations...) event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) } - // check annotations - updatedAnnotations := translate.HostAnnotations(event.Virtual, event.Host, s.excludedAnnotations...) - // remove the ServiceBlockDeletion annotation if it's not needed if event.Virtual.Spec.ClusterIP == event.Host.Spec.ClusterIP { - delete(updatedAnnotations, ServiceBlockDeletion) + delete(event.Host.Annotations, ServiceBlockDeletion) } - event.Host.Annotations = updatedAnnotations // translate selector if event.Source == synccontext.SyncEventSourceHost { diff --git a/pkg/controllers/resources/storageclasses/syncer.go b/pkg/controllers/resources/storageclasses/syncer.go index 3d30f7640..1ea3800fd 100644 --- a/pkg/controllers/resources/storageclasses/syncer.go +++ b/pkg/controllers/resources/storageclasses/syncer.go @@ -81,8 +81,13 @@ func (s *storageClassSyncer) Sync(ctx *synccontext.SyncContext, event *syncconte } }() - event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host, s.excludedAnnotations...) - event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) + if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) + event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) + } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host, s.excludedAnnotations...) + event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) + } // bidirectional sync event.TargetObject().Provisioner = event.SourceObject().Provisioner diff --git a/pkg/controllers/resources/volumesnapshotcontents/syncer.go b/pkg/controllers/resources/volumesnapshotcontents/syncer.go index 26f950012..9b6261347 100644 --- a/pkg/controllers/resources/volumesnapshotcontents/syncer.go +++ b/pkg/controllers/resources/volumesnapshotcontents/syncer.go @@ -188,6 +188,12 @@ func (s *volumeSnapshotContentSyncer) Sync(ctx *synccontext.SyncContext, event * if event.Virtual.Annotations[constants.HostClusterVSCAnnotation] == "" { event.Host.Spec.DeletionPolicy = event.Virtual.Spec.DeletionPolicy event.Host.Spec.VolumeSnapshotClassName = event.Virtual.Spec.VolumeSnapshotClassName + } + + if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) + event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) + } else { event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) } diff --git a/pkg/controllers/resources/volumesnapshots/syncer.go b/pkg/controllers/resources/volumesnapshots/syncer.go index 42d9ae7c7..ed3c6772b 100644 --- a/pkg/controllers/resources/volumesnapshots/syncer.go +++ b/pkg/controllers/resources/volumesnapshots/syncer.go @@ -150,8 +150,13 @@ func (s *volumeSnapshotSyncer) Sync(ctx *synccontext.SyncContext, event *synccon event.Host.Spec.VolumeSnapshotClassName = event.Virtual.Spec.VolumeSnapshotClassName // check if metadata changed - event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) - event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) + if event.Source == synccontext.SyncEventSourceHost { + event.Virtual.Annotations = translate.VirtualAnnotations(event.Host, event.Virtual) + event.Virtual.Labels = translate.VirtualLabels(event.Host, event.Virtual) + } else { + event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host) + event.Host.Labels = translate.HostLabels(event.Virtual, event.Host) + } return ctrl.Result{}, nil } diff --git a/pkg/syncer/wrapper.go b/pkg/syncer/wrapper.go index 12276a90e..826ed5a67 100644 --- a/pkg/syncer/wrapper.go +++ b/pkg/syncer/wrapper.go @@ -1,10 +1,13 @@ package syncer import ( + "errors" + "github.com/loft-sh/vcluster/pkg/syncer/synccontext" syncertypes "github.com/loft-sh/vcluster/pkg/syncer/types" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/reconcile" ) func ToGenericSyncer[T client.Object](syncer syncertypes.Sync[T]) syncertypes.Sync[client.Object] { @@ -28,8 +31,11 @@ func (t *toSyncer[T]) SyncToVirtual(ctx *synccontext.SyncContext, event *synccon } func (t *toSyncer[T]) Sync(ctx *synccontext.SyncContext, event *synccontext.SyncEvent[client.Object]) (ctrl.Result, error) { - hostConverted, _ := event.Host.(T) - virtualConverted, _ := event.Virtual.(T) + hostConverted, ok := event.Host.(T) + virtualConverted, ok2 := event.Virtual.(T) + if !ok || !ok2 { + return reconcile.Result{}, errors.New("syncer: type assertion failed") + } return t.syncer.Sync(ctx, &synccontext.SyncEvent[T]{ Type: event.Type, @@ -40,7 +46,10 @@ func (t *toSyncer[T]) Sync(ctx *synccontext.SyncContext, event *synccontext.Sync } func (t *toSyncer[T]) SyncToHost(ctx *synccontext.SyncContext, event *synccontext.SyncToHostEvent[client.Object]) (ctrl.Result, error) { - virtualConverted, _ := event.Virtual.(T) + virtualConverted, ok := event.Virtual.(T) + if !ok { + return reconcile.Result{}, errors.New("syncer: type assertion failed") + } return t.syncer.SyncToHost(ctx, &synccontext.SyncToHostEvent[T]{ Type: event.Type,