Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: labels & annotations translation #1970

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pkg/controllers/generic/export_patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var _ ObjectPatcher = &exportPatcher{}
func (e *exportPatcher) ServerSideApply(_ *synccontext.SyncContext, fromObj, destObj, sourceObj client.Object) error {
return patches.ApplyPatches(destObj, sourceObj, e.config.Patches, e.config.ReversePatches, &virtualToHostNameResolver{
namespace: fromObj.GetNamespace(),
targetNamespace: translate.Default.PhysicalNamespace(fromObj.GetNamespace()),
targetNamespace: translate.Default.HostNamespace(fromObj.GetNamespace()),
})
}

Expand Down Expand Up @@ -54,21 +54,21 @@ func (r *virtualToHostNameResolver) TranslateNameWithNamespace(name string, name
}

return types.NamespacedName{
Namespace: translate.Default.PhysicalNamespace(namespace),
Name: translate.Default.PhysicalName(name, ns),
Namespace: translate.Default.HostNamespace(namespace),
Name: translate.Default.HostName(name, ns),
}
}), nil
}

return translate.Default.PhysicalName(name, namespace), nil
return translate.Default.HostName(name, namespace), nil
}

func (r *virtualToHostNameResolver) TranslateLabelExpressionsSelector(selector *metav1.LabelSelector) (*metav1.LabelSelector, error) {
return translate.Default.TranslateLabelSelectorCluster(selector), nil
return translate.Default.HostLabelSelectorCluster(selector), nil
}

func (r *virtualToHostNameResolver) TranslateLabelKey(key string) (string, error) {
return translate.Default.ConvertLabelKey(key), nil
return translate.Default.HostLabel(key), nil
}

func (r *virtualToHostNameResolver) TranslateLabelSelector(selector map[string]string) (map[string]string, error) {
Expand All @@ -77,11 +77,11 @@ func (r *virtualToHostNameResolver) TranslateLabelSelector(selector map[string]s
}

return metav1.LabelSelectorAsMap(
translate.Default.TranslateLabelSelector(labelSelector))
translate.Default.HostLabelSelector(labelSelector))
}

func (r *virtualToHostNameResolver) TranslateNamespaceRef(namespace string) (string, error) {
return translate.Default.PhysicalNamespace(namespace), nil
return translate.Default.HostNamespace(namespace), nil
}

func validateExportConfig(config *vclusterconfig.Export) error {
Expand Down
5 changes: 3 additions & 2 deletions pkg/controllers/generic/export_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func createExporterFromConfig(ctx *synccontext.RegisterContext, config *vcluster

gvk := schema.FromAPIVersionAndKind(config.APIVersion, config.Kind)
controllerID := fmt.Sprintf("%s/%s/GenericExport", strings.ToLower(gvk.Kind), strings.ToLower(gvk.Group))
mapper, err := generic.NewMapper(ctx, obj, translate.Default.PhysicalName)
mapper, err := generic.NewMapper(ctx, obj, translate.Default.HostName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -286,7 +286,7 @@ func (f *exporter) Name() string {

// TranslateMetadata converts the virtual object into a physical object
func (f *exporter) TranslateMetadata(ctx *synccontext.SyncContext, vObj client.Object) client.Object {
pObj := f.GenericTranslator.TranslateMetadata(ctx, vObj)
pObj := translate.HostMetadata(ctx, vObj, f.VirtualToHost(ctx, types.NamespacedName{Name: vObj.GetName(), Namespace: vObj.GetNamespace()}, vObj))
if pObj == nil {
return nil
}
Expand All @@ -298,6 +298,7 @@ func (f *exporter) TranslateMetadata(ctx *synccontext.SyncContext, vObj client.O
a[translate.ControllerLabel] = f.Name()
pObj.SetAnnotations(a)
}

return pObj
}

Expand Down
11 changes: 1 addition & 10 deletions pkg/controllers/generic/import_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/loft-sh/vcluster/pkg/util/clienthelper"
"github.com/loft-sh/vcluster/pkg/util/translate"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -370,7 +369,7 @@ func (s *importer) VirtualToHost(ctx *synccontext.SyncContext, req types.Namespa
return s.virtualToHost(ctx, req, vObj)
}

return types.NamespacedName{Name: translate.Default.PhysicalName(req.Name, req.Namespace), Namespace: translate.Default.PhysicalNamespace(req.Namespace)}
return types.NamespacedName{Name: translate.Default.HostName(req.Name, req.Namespace), Namespace: translate.Default.HostNamespace(req.Namespace)}
}

func (s *importer) HostToVirtual(ctx *synccontext.SyncContext, req types.NamespacedName, pObj client.Object) types.NamespacedName {
Expand Down Expand Up @@ -414,14 +413,6 @@ func (s *importer) TranslateMetadata(ctx *synccontext.SyncContext, pObj client.O
return vObj
}

// TranslateMetadataUpdate translates the object's metadata annotations and labels and determines
// if they have changed between the physical and virtual object
func (s *importer) TranslateMetadataUpdate(_ *synccontext.SyncContext, vObj client.Object, pObj client.Object) (changed bool, annotations map[string]string, labels map[string]string) {
updatedAnnotations := s.updateVirtualAnnotations(pObj.GetAnnotations())
updatedLabels := pObj.GetLabels()
return !equality.Semantic.DeepEqual(updatedAnnotations, vObj.GetAnnotations()) || !equality.Semantic.DeepEqual(updatedLabels, vObj.GetLabels()), updatedAnnotations, updatedLabels
}

func (s *importer) updateVirtualAnnotations(a map[string]string) map[string]string {
if a == nil {
return map[string]string{translate.ControllerLabel: s.Name()}
Expand Down
3 changes: 1 addition & 2 deletions pkg/controllers/generic/patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/loft-sh/vcluster/pkg/log"
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
"github.com/loft-sh/vcluster/pkg/syncer/types"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -18,7 +17,7 @@ import (
var fieldManager = "vcluster-syncer"

type ObjectPatcherAndMetadataTranslator interface {
types.MetadataTranslator
TranslateMetadata(ctx *synccontext.SyncContext, pObj client.Object) client.Object
ObjectPatcher
}

Expand Down
13 changes: 10 additions & 3 deletions pkg/controllers/resources/configmaps/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
"github.com/loft-sh/vcluster/pkg/syncer/translator"
syncertypes "github.com/loft-sh/vcluster/pkg/syncer/types"
"github.com/loft-sh/vcluster/pkg/util/translate"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -71,7 +72,8 @@ func (s *configMapSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.O
return syncer.DeleteVirtualObject(ctx, vObj, "host object was deleted")
}

return s.SyncToHostCreate(ctx, vObj, s.translate(ctx, vObj.(*corev1.ConfigMap)))
pObj := translate.HostMetadata(ctx, vObj, s.VirtualToHost(ctx, types.NamespacedName{Name: vObj.GetName(), Namespace: vObj.GetNamespace()}, vObj))
return syncer.CreateHostObject(ctx, vObj, pObj, s.EventRecorder())
}

func (s *configMapSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (_ ctrl.Result, retErr error) {
Expand Down Expand Up @@ -109,9 +111,14 @@ func (s *configMapSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object,
}
}()

s.translateUpdate(ctx, pConfigMap, vConfigMap)
// check annotations & labels
pConfigMap.Annotations = translate.HostAnnotations(vConfigMap, pConfigMap)
pConfigMap.Labels = translate.HostLabels(ctx, vConfigMap, pConfigMap)

// did the configmap change?
// bidirectional sync
source, target := synccontext.SyncSourceTarget(ctx, pConfigMap, vConfigMap)
target.Data = source.Data
target.BinaryData = source.BinaryData
return ctrl.Result{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/configmaps/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestSync(t *testing.T) {
}
syncedConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: translate.Default.PhysicalName(baseConfigMap.Name, baseConfigMap.Namespace),
Name: translate.Default.HostName(baseConfigMap.Name, baseConfigMap.Namespace),
Namespace: "test",
Annotations: map[string]string{
translate.NameAnnotation: baseConfigMap.Name,
Expand Down
23 changes: 0 additions & 23 deletions pkg/controllers/resources/configmaps/translate.go

This file was deleted.

32 changes: 22 additions & 10 deletions pkg/controllers/resources/csidrivers/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,43 @@ import (
"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/patcher"
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
"github.com/loft-sh/vcluster/pkg/syncer/translator"
"github.com/loft-sh/vcluster/pkg/syncer/types"
syncertypes "github.com/loft-sh/vcluster/pkg/syncer/types"
"github.com/loft-sh/vcluster/pkg/util/translate"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func New(ctx *synccontext.RegisterContext) (types.Object, error) {
func New(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {
mapper, err := ctx.Mappings.ByGVK(mappings.CSIDrivers())
if err != nil {
return nil, err
}

return &csidriverSyncer{
Translator: translator.NewMirrorPhysicalTranslator("csidriver", &storagev1.CSIDriver{}, mapper),
Mapper: mapper,
}, nil
}

type csidriverSyncer struct {
types.Translator
synccontext.Mapper
}

var _ types.ToVirtualSyncer = &csidriverSyncer{}
var _ types.Syncer = &csidriverSyncer{}
func (s *csidriverSyncer) Name() string {
return "csidriver"
}

func (s *csidriverSyncer) Resource() client.Object {
return &storagev1.CSIDriver{}
}

var _ syncertypes.ToVirtualSyncer = &csidriverSyncer{}
var _ syncertypes.Syncer = &csidriverSyncer{}

func (s *csidriverSyncer) SyncToVirtual(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
vObj := s.translateBackwards(ctx, pObj.(*storagev1.CSIDriver))
vObj := translate.CopyObjectWithName(pObj.(*storagev1.CSIDriver), types.NamespacedName{Name: pObj.GetName(), Namespace: pObj.GetNamespace()}, false)
ctx.Log.Infof("create CSIDriver %s, because it does not exist in virtual cluster", vObj.Name)
return ctrl.Result{}, ctx.VirtualClient.Create(ctx, vObj)
}
Expand All @@ -48,9 +57,12 @@ func (s *csidriverSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object,
retErr = utilerrors.NewAggregate([]error{retErr, err})
}
}()
// check if there is a change
s.translateUpdateBackwards(ctx, pObj.(*storagev1.CSIDriver), vObj.(*storagev1.CSIDriver))

// check if there is a change
pCSIDriver, vCSIDriver, _, _ := synccontext.Cast[*storagev1.CSIDriver](ctx, pObj, vObj)
vCSIDriver.Annotations = pCSIDriver.Annotations
vCSIDriver.Labels = pCSIDriver.Labels
pCSIDriver.Spec.DeepCopyInto(&vCSIDriver.Spec)
return ctrl.Result{}, nil
}

Expand Down
20 changes: 0 additions & 20 deletions pkg/controllers/resources/csidrivers/translate.go

This file was deleted.

46 changes: 27 additions & 19 deletions pkg/controllers/resources/csinodes/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/patcher"
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
"github.com/loft-sh/vcluster/pkg/syncer/translator"
syncertypes "github.com/loft-sh/vcluster/pkg/syncer/types"
"github.com/loft-sh/vcluster/pkg/util/translate"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -24,16 +24,25 @@ func New(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {
}

return &csinodeSyncer{
Translator: translator.NewMirrorPhysicalTranslator("csinode", &storagev1.CSINode{}, mapper),
Mapper: mapper,

virtualClient: ctx.VirtualManager.GetClient(),
}, nil
}

type csinodeSyncer struct {
syncertypes.Translator
synccontext.Mapper
virtualClient client.Client
}

func (s *csinodeSyncer) Name() string {
return "csinode"
}

func (s *csinodeSyncer) Resource() client.Object {
return &storagev1.CSINode{}
}

var _ syncertypes.ToVirtualSyncer = &csinodeSyncer{}
var _ syncertypes.Syncer = &csinodeSyncer{}

Expand All @@ -46,39 +55,38 @@ func (s *csinodeSyncer) SyncToVirtual(ctx *synccontext.SyncContext, pObj client.
} else if err != nil {
return ctrl.Result{}, err
}
vObj := s.translateBackwards(ctx, pObj.(*storagev1.CSINode))

vObj := translate.CopyObjectWithName(pObj.(*storagev1.CSINode), types.NamespacedName{Name: pObj.GetName(), Namespace: pObj.GetNamespace()}, false)
ctx.Log.Infof("create CSINode %s, because it does not exist in virtual cluster", vObj.Name)
return ctrl.Result{}, ctx.VirtualClient.Create(ctx, vObj)
}

func (s *csinodeSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (_ ctrl.Result, retErr error) {
node := &corev1.Node{}
err := s.virtualClient.Get(ctx, types.NamespacedName{Name: pObj.GetName()}, node)
if kerrors.IsNotFound(err) {
ctx.Log.Infof("delete virtual CSINode %s, because corresponding node object is missing", vObj.GetName())
return ctrl.Result{}, ctx.VirtualClient.Delete(ctx, vObj)
} else if err != nil {
return ctrl.Result{}, err
}

// look up matching node name, delete csinode if not found
patch, err := patcher.NewSyncerPatcher(ctx, pObj, vObj)
if err != nil {
return ctrl.Result{}, fmt.Errorf("new syncer patcher: %w", err)
}
shouldPatch := true
defer func() {
if !shouldPatch {
return
}
if err := patch.Patch(ctx, pObj, vObj); err != nil {
retErr = utilerrors.NewAggregate([]error{retErr, err})
}
}()

node := &corev1.Node{}
err = s.virtualClient.Get(ctx, types.NamespacedName{Name: pObj.GetName()}, node)
if kerrors.IsNotFound(err) {
ctx.Log.Infof("delete virtual CSINode %s, because corresponding node object is missing", vObj.GetName())
shouldPatch = false
return ctrl.Result{}, ctx.VirtualClient.Delete(ctx, vObj)
} else if err != nil {
return ctrl.Result{}, err
}
// check if there is a change
s.translateUpdateBackwards(ctx, pObj.(*storagev1.CSINode), vObj.(*storagev1.CSINode))

pCSINode, vCSINode, _, _ := synccontext.Cast[*storagev1.CSINode](ctx, pObj, vObj)
vCSINode.Annotations = pCSINode.Annotations
vCSINode.Labels = pCSINode.Labels
pCSINode.Spec.DeepCopyInto(&vCSINode.Spec)
return ctrl.Result{}, nil
}

Expand Down
20 changes: 0 additions & 20 deletions pkg/controllers/resources/csinodes/translate.go

This file was deleted.

Loading
Loading