Skip to content

requeue endpoint reconcile when the service is created after the endpoint #2846

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 2 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions pkg/controllers/resources/endpoints/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package endpoints
import (
"errors"
"fmt"
"context"

"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/patcher"
Expand All @@ -19,6 +20,8 @@ import (
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/handler"
)

func New(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {
Expand Down Expand Up @@ -56,6 +59,48 @@ func (s *endpointsSyncer) Syncer() syncertypes.Sync[client.Object] {
return syncer.ToGenericSyncer(s)
}

var _ syncertypes.ControllerModifier = &endpointsSyncer{}

func (s *endpointsSyncer) ModifyController(ctx *synccontext.RegisterContext, bld *builder.Builder) (*builder.Builder, error) {
klog.Info("Starting to modify the controller to watch for Service changes and reconcile Endpoints")

// Watch for changes to Services and reconcile Endpoints
bld = bld.Watches(&corev1.Service{}, handler.EnqueueRequestsFromMapFunc(func(_ context.Context, obj client.Object) []ctrl.Request {
service, ok := obj.(*corev1.Service)
if !ok || service == nil {
klog.Info("Received an object that is not a Service or is nil, skipping")
return []ctrl.Request{}
}
endpoint:= &corev1.Endpoints{}
err:= ctx.VirtualManager.GetClient().Get(ctx, types.NamespacedName{
Namespace: service.Namespace,
Name: service.Name,
}, endpoint)
if err != nil {
if kerrors.IsNotFound(err) {
klog.Infof("Endpoints for Service %s/%s not found, skipping", service.Namespace, service.Name)
return []ctrl.Request{}
}
klog.Errorf("Error retrieving Endpoints for Service %s/%s: %v", service.Namespace, service.Name, err)
return []ctrl.Request{}
}

klog.Infof("Enqueuing reconciliation request of Endpoint for Service: %s/%s", service.Namespace, service.Name)

// Enqueue a request to reconcile the corresponding Endpoints
return []ctrl.Request{
{
NamespacedName: types.NamespacedName{
Namespace: service.Namespace,
Name: service.Name,
},
},
}
}))

return bld, nil
}

func (s *endpointsSyncer) SyncToHost(ctx *synccontext.SyncContext, event *synccontext.SyncToHostEvent[*corev1.Endpoints]) (ctrl.Result, error) {
if event.HostOld != nil {
return patcher.DeleteVirtualObject(ctx, event.Virtual, event.HostOld, "host object was deleted")
Expand Down