Skip to content

feat: add ability to force sync an event into default namespace #2786

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions pkg/controllers/resources/events/translate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package events

import (
"errors"
"strings"

"github.com/loft-sh/vcluster/pkg/constants"
"github.com/loft-sh/vcluster/pkg/mappings/resources"
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
"github.com/loft-sh/vcluster/pkg/util/translate"
Expand All @@ -13,6 +15,10 @@ func (s *eventSyncer) translateEvent(ctx *synccontext.SyncContext, pEvent, vEven
// retrieve involved object
involvedObject, err := resources.GetInvolvedObject(ctx, pEvent)
if err != nil {
if pEvent.GetAnnotations()[constants.SyncResourceAnnotation] == "true" &&
(errors.Is(err, resources.ErrNilPhysicalObject) || errors.Is(err, resources.ErrKindNotAccepted) || errors.Is(err, resources.ErrNotFound)) {
return s.forceTranslateEvent(pEvent, vEvent)
}
return err
}
tempEvent := pEvent.DeepCopy()
Expand Down Expand Up @@ -50,3 +56,20 @@ func hostEventNameToVirtual(hostName string, hostInvolvedObjectName, virtualInvo

return hostName
}

func (s *eventSyncer) forceTranslateEvent(pEvent, vEvent *corev1.Event) error {
tempEvent := pEvent.DeepCopy()

// keep the metadata from the virtual object
translate.ResetObjectMetadata(tempEvent)
tempEvent.ObjectMeta = vEvent.ObjectMeta
tempEvent.TypeMeta = vEvent.TypeMeta

tempEvent.DeepCopyInto(vEvent)
delete(vEvent.Annotations, constants.SyncResourceAnnotation)
vEvent.Namespace = "default"
vEvent.InvolvedObject = corev1.ObjectReference{
Namespace: vEvent.Namespace,
}
return nil
}
6 changes: 6 additions & 0 deletions pkg/mappings/resources/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/loft-sh/vcluster/pkg/constants"
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -49,6 +50,11 @@ func (s *eventMapper) HostToVirtual(ctx *synccontext.SyncContext, req types.Name
err = IgnoreAcceptableErrors(err)
if err != nil {
klog.Infof("Error retrieving involved object for %s/%s: %v", req.Namespace, req.Name, err)
} else if pObj.GetAnnotations()[constants.SyncResourceAnnotation] == "true" {
return types.NamespacedName{
Namespace: "default",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move this into a variable or a constant?

Just a question, why are we hardcoding it to the default ns?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move this into a variable or a constant?

Ok, will do.

Just a question, why are we hardcoding it to the default ns?

If the event is not related to any specific synced resource then we don't have anything to go by to make the ns decision. Thus "default" seemed like a reasonable fallback. I've also considered adding an annotation for the virtual namespace, but default is enough for the initial use case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that makes sense.

Name: pObj.GetName(),
}
}

return types.NamespacedName{}
Expand Down