Skip to content

Commit

Permalink
added patch logic for events
Browse files Browse the repository at this point in the history
  • Loading branch information
facchettos committed Jul 16, 2024
1 parent 91290b7 commit 2f96dd7
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions pkg/controllers/resources/events/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
"github.com/loft-sh/vcluster/pkg/constants"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
"github.com/loft-sh/vcluster/pkg/patcher"
syncer "github.com/loft-sh/vcluster/pkg/types"
"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/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -34,12 +35,15 @@ var AcceptedKinds = map[schema.GroupVersionKind]bool{

func New(ctx *synccontext.RegisterContext) (syncer.Object, error) {
return &eventSyncer{
virtualClient: ctx.VirtualManager.GetClient(),
hostClient: ctx.PhysicalManager.GetClient(),
NamespacedTranslator: translator.NewNamespacedTranslator(ctx, "event", &corev1.Event{}),
virtualClient: ctx.VirtualManager.GetClient(),
hostClient: ctx.PhysicalManager.GetClient(),
}, nil
}

type eventSyncer struct {
translator.NamespacedTranslator

virtualClient client.Client
hostClient client.Client
}
Expand Down Expand Up @@ -88,14 +92,26 @@ func (s *eventSyncer) SyncToHost(_ *synccontext.SyncContext, _ client.Object) (c
panic("unimplemented")
}

func (s *eventSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (ctrl.Result, error) {
func (s *eventSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (_ ctrl.Result, retErr error) {
// convert current events
pEvent := pObj.(*corev1.Event)
vEvent := vObj.(*corev1.Event)

patch, err := patcher.NewSyncerPatcher(ctx, pEvent, vEvent)
if err != nil {
return ctrl.Result{}, fmt.Errorf("new syncer patcher: %w", err)
}

defer func() {
if err := patch.Patch(ctx, pEvent, vEvent); err != nil {
s.NamespacedTranslator.EventRecorder().Eventf(vEvent, "Warning", "SyncError", "Error syncing: %v", err)
retErr = utilerrors.NewAggregate([]error{retErr, err})
}
}()

// update event
vOldEvent := vEvent.DeepCopy()
vEvent, err := s.buildVirtualEvent(ctx.Context, pEvent)
vEvent, err = s.buildVirtualEvent(ctx.Context, pEvent)
if err != nil {
return ctrl.Result{}, IgnoreAcceptableErrors(err)
}
Expand All @@ -104,19 +120,6 @@ func (s *eventSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vOb
vEvent.TypeMeta = vOldEvent.TypeMeta
vEvent.ObjectMeta = vOldEvent.ObjectMeta

// update existing event only if changed
if equality.Semantic.DeepEqual(vEvent, vOldEvent) {
return ctrl.Result{}, nil
}

// check if updated
ctx.Log.Infof("update virtual event %s/%s", vEvent.Namespace, vEvent.Name)
translator.PrintChanges(vOldEvent, vEvent, ctx.Log)
err = ctx.VirtualClient.Update(ctx.Context, vEvent)
if err != nil {
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

Expand Down

0 comments on commit 2f96dd7

Please sign in to comment.