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: importer adjustments #2078

Merged
merged 1 commit into from
Aug 16, 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: 15 additions & 1 deletion pkg/controllers/resources/ingresses/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ 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"
networkingv1 "k8s.io/api/networking/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"
Expand All @@ -28,11 +30,13 @@ func NewSyncer(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {

return &ingressSyncer{
GenericTranslator: translator.NewGenericTranslator(ctx, "ingress", &networkingv1.Ingress{}, mapper),
Importer: pro.NewImporter(mapper),
}, nil
}

type ingressSyncer struct {
syncertypes.GenericTranslator
syncertypes.Importer
}

var _ syncertypes.Syncer = &ingressSyncer{}
Expand Down Expand Up @@ -82,5 +86,15 @@ func (s *ingressSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.Sy

func (s *ingressSyncer) SyncToVirtual(ctx *synccontext.SyncContext, event *synccontext.SyncToVirtualEvent[*networkingv1.Ingress]) (_ ctrl.Result, retErr error) {
// virtual object is not here anymore, so we delete
return syncer.DeleteHostObject(ctx, event.Host, "virtual object was deleted")
if event.IsDelete() || event.Host.DeletionTimestamp != nil {
return syncer.DeleteHostObject(ctx, event.Host, "virtual object was deleted")
}

vIngress := translate.VirtualMetadata(event.Host, s.HostToVirtual(ctx, types.NamespacedName{Name: event.Host.Name, Namespace: event.Host.Namespace}, event.Host))
err := pro.ApplyPatchesVirtualObject(ctx, nil, vIngress, event.Host, ctx.Config.Sync.ToHost.Ingresses.Translate)
if err != nil {
return ctrl.Result{}, err
}

return syncer.CreateVirtualObject(ctx, event.Host, vIngress, s.EventRecorder())
}
4 changes: 4 additions & 0 deletions pkg/pro/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ type noopImporter struct{}
func (n *noopImporter) Import(_ *synccontext.SyncContext, _ client.Object) (bool, error) {
return false, nil
}

func (n *noopImporter) IgnoreHostObject(_ *synccontext.SyncContext, _ client.Object) bool {
return false
}
4 changes: 4 additions & 0 deletions pkg/syncer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ type noopImporter struct{}
func (n *noopImporter) Import(_ *synccontext.SyncContext, _ client.Object) (bool, error) {
return false, nil
}

func (n *noopImporter) IgnoreHostObject(_ *synccontext.SyncContext, _ client.Object) bool {
return false
}
56 changes: 23 additions & 33 deletions pkg/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ func (r *SyncController) Reconcile(ctx context.Context, origReq ctrl.Request) (_
return ctrl.Result{}, err
}

// check if we should ignore object
if importer, ok := r.syncer.(syncertypes.Importer); ok && importer.IgnoreHostObject(syncContext, pObj) {
return ctrl.Result{Requeue: true}, nil
}

// add mapping to context
if !r.options.SkipMappingsRecording {
syncContext.Context, err = synccontext.WithMappingFromObjects(syncContext.Context, pObj, vObj)
Expand Down Expand Up @@ -391,21 +396,13 @@ func (r *SyncController) extractRequest(ctx *synccontext.SyncContext, req ctrl.R
return req, pReq, nil
}

func (r *SyncController) enqueueVirtual(ctx context.Context, obj client.Object, q workqueue.RateLimitingInterface, isDelete bool) {
func (r *SyncController) enqueueVirtual(_ context.Context, obj client.Object, q workqueue.RateLimitingInterface, isDelete bool) {
if obj == nil {
return
}

// add a new request for the host object as otherwise this information might be lost after a delete event
if isDelete {
// add a new request for the host object
name := r.syncer.VirtualToHost(r.newSyncContext(ctx, obj.GetName()), types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj)
if name.Name != "" {
q.Add(toDeleteRequest(toHostRequest(reconcile.Request{
NamespacedName: name,
})))
}

// add a new request for the virtual object
q.Add(toDeleteRequest(reconcile.Request{
NamespacedName: types.NamespacedName{
Expand Down Expand Up @@ -441,39 +438,32 @@ func (r *SyncController) enqueuePhysical(ctx context.Context, obj client.Object,
return
} else if !managed {
// check if we should import

// don't import if object was deleted
if isDelete {
return
imported := false
if importer, ok := r.syncer.(syncertypes.Importer); ok && !isDelete {
imported, err = importer.Import(syncContext, obj)
if err != nil {
klog.Errorf("error importing object %v: %v", obj, err)
return
}
}

// is importer?
importer, ok := r.syncer.(syncertypes.Importer)
if !ok {
// if not imported we exit here
if !imported {
return
}
}

// try to import
imported, err := importer.Import(syncContext, obj)
if err != nil {
klog.Errorf("error importing object %v: %v", obj, err)
return
} else if !imported {
// not imported, so just return
return
}
// check if we should ignore the host object
if importer, ok := r.syncer.(syncertypes.Importer); ok && importer.IgnoreHostObject(syncContext, obj) {
// since we check later anyways in the actual syncer again if we should ignore the object we only need to set
// isDelete = false here to make sure the event is propagated and not missed and the syncer is recreating the
// object correctly as soon as its deleted. However, we don't want it to be a delete event as this will delete
// the virtual object so we need to set that to false here.
isDelete = false
}

// add a new request for the virtual object as otherwise this information might be lost after a delete event
if isDelete {
// add a new request for the virtual object
name := r.syncer.HostToVirtual(syncContext, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj)
if name.Name != "" {
q.Add(toDeleteRequest(reconcile.Request{
NamespacedName: name,
}))
}

// add a new request for the host object
q.Add(toDeleteRequest(toHostRequest(reconcile.Request{
NamespacedName: types.NamespacedName{
Expand Down
3 changes: 3 additions & 0 deletions pkg/syncer/types/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Syncer interface {
type Importer interface {
// Import imports the given host object into the virtual cluster
Import(ctx *synccontext.SyncContext, pObj client.Object) (bool, error)

// IgnoreHostObject is used to ignore certain objects during the import process
IgnoreHostObject(ctx *synccontext.SyncContext, pObj client.Object) bool
}

type Sync[T client.Object] interface {
Expand Down
Loading