Skip to content

Commit 94ce04d

Browse files
committed
align syncer with latest changes
1 parent 0d07d8c commit 94ce04d

File tree

6 files changed

+66
-10
lines changed

6 files changed

+66
-10
lines changed

pkg/controllers/resources/endpointSlices/syncer.go renamed to pkg/controllers/resources/endpointslices/syncer.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package endpoints
1+
package endpointslices
22

33
import (
44
"errors"
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
func New(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {
26-
mapper, err := ctx.Mappings.ByGVK(mappings.Endpoints())
26+
mapper, err := ctx.Mappings.ByGVK(mappings.EndpointSlices())
2727
if err != nil {
2828
return nil, err
2929
}
@@ -63,7 +63,7 @@ func (s *endpointSliceSyncer) SyncToHost(ctx *synccontext.SyncContext, event *sy
6363
}
6464

6565
pObj := s.translate(ctx, event.Virtual)
66-
err := pro.ApplyPatchesHostObject(ctx, nil, pObj, event.Virtual, ctx.Config.Sync.ToHost.EndpointSlices.Patches, false)
66+
err := pro.ApplyPatchesHostObject(ctx, pObj, event.Virtual, ctx.Config.Sync.ToHost.EndpointSlices.Patches, false)
6767
if err != nil {
6868
return ctrl.Result{}, err
6969
}
@@ -129,7 +129,7 @@ func (s *endpointSliceSyncer) ReconcileStart(ctx *synccontext.SyncContext, req c
129129
} else if svc.Spec.Selector != nil {
130130
// check if it was a managed endpointSlice object before and delete it
131131
endpointSlice := &discoveryv1.EndpointSlice{}
132-
err = ctx.PhysicalClient.Get(ctx, s.VirtualToHost(ctx, req.NamespacedName, nil), endpointSlice)
132+
err = ctx.HostClient.Get(ctx, s.VirtualToHost(ctx, req.NamespacedName, nil), endpointSlice)
133133
if err != nil {
134134
if !kerrors.IsNotFound(err) {
135135
klog.Infof("Error retrieving endpointSliceList: %v", err)
@@ -146,7 +146,7 @@ func (s *endpointSliceSyncer) ReconcileStart(ctx *synccontext.SyncContext, req c
146146
// to the same endpoints resulting in wrong DNS and cluster networking. Hence, deleting the previously
147147
// managed endpointSlices signals the Kubernetes controller to recreate the endpointSlices from the selector.
148148
klog.Infof("Refresh endpointSlice in physical cluster because they shouldn't be managed by vcluster anymore")
149-
err = ctx.PhysicalClient.Delete(ctx, endpointSlice)
149+
err = ctx.HostClient.Delete(ctx, endpointSlice)
150150
if err != nil {
151151
klog.Infof("Error deleting endpoints %s/%s: %v", endpointSlice.Namespace, endpointSlice.Name, err)
152152
return true, err
@@ -158,10 +158,10 @@ func (s *endpointSliceSyncer) ReconcileStart(ctx *synccontext.SyncContext, req c
158158

159159
// check if it was a Kubernetes managed endpointSlice object before and delete it
160160
endpointSlice := &discoveryv1.EndpointSlice{}
161-
err = ctx.PhysicalClient.Get(ctx, s.VirtualToHost(ctx, req.NamespacedName, nil), endpointSlice)
161+
err = ctx.HostClient.Get(ctx, s.VirtualToHost(ctx, req.NamespacedName, nil), endpointSlice)
162162
if err == nil && (endpointSlice.Annotations == nil || endpointSlice.Annotations[translate.NameAnnotation] == "") {
163163
klog.Infof("Refresh endpointSlice in physical cluster because they should be managed by vCluster now")
164-
err = ctx.PhysicalClient.Delete(ctx, endpointSlice)
164+
err = ctx.HostClient.Delete(ctx, endpointSlice)
165165
if err != nil {
166166
klog.Infof("Error deleting endpointSlice %s/%s: %v", endpointSlice.Namespace, endpointSlice.Name, err)
167167
return true, err

pkg/controllers/resources/endpointSlices/translate.go renamed to pkg/controllers/resources/endpointslices/translate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package endpoints
1+
package endpointslices
22

33
import (
44
"github.com/loft-sh/vcluster/pkg/mappings"
@@ -10,7 +10,9 @@ import (
1010
)
1111

1212
func (s *endpointSliceSyncer) translate(ctx *synccontext.SyncContext, vObj client.Object) *discoveryv1.EndpointSlice {
13-
endpointSlice := translate.HostMetadata(vObj.(*discoveryv1.EndpointSlice), s.VirtualToHost(ctx, types.NamespacedName{Name: vObj.GetName(), Namespace: vObj.GetNamespace()}, vObj), s.excludedAnnotations...)
13+
endpointSlice := translate.HostMetadata(vObj.(*discoveryv1.EndpointSlice),
14+
s.VirtualToHost(ctx, types.NamespacedName{Name: vObj.GetName(), Namespace: vObj.GetNamespace()}, vObj),
15+
s.excludedAnnotations...)
1416
s.translateSpec(ctx, endpointSlice)
1517
return endpointSlice
1618
}

pkg/controllers/resources/register.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/loft-sh/vcluster/pkg/controllers/resources/csinodes"
99
"github.com/loft-sh/vcluster/pkg/controllers/resources/csistoragecapacities"
1010
"github.com/loft-sh/vcluster/pkg/controllers/resources/endpoints"
11+
"github.com/loft-sh/vcluster/pkg/controllers/resources/endpointslices"
1112
"github.com/loft-sh/vcluster/pkg/controllers/resources/events"
1213
"github.com/loft-sh/vcluster/pkg/controllers/resources/ingressclasses"
1314
"github.com/loft-sh/vcluster/pkg/controllers/resources/ingresses"
@@ -48,7 +49,7 @@ func getSyncers(ctx *synccontext.RegisterContext) []BuildController {
4849
isEnabled(ctx.Config.Sync.FromHost.Secrets.Enabled, secrets.NewFromHost),
4950
isEnabled(ctx.Config.Sync.ToHost.Secrets.Enabled, secrets.New),
5051
isEnabled(ctx.Config.Sync.ToHost.Endpoints.Enabled, endpoints.New),
51-
isEnabled(ctx.Config.Sync.ToHost.EndpointSlices.Enabled, endpoints.New),
52+
isEnabled(ctx.Config.Sync.ToHost.EndpointSlices.Enabled, endpointslices.New),
5253
isEnabled(ctx.Config.Sync.ToHost.Pods.Enabled, pods.New),
5354
isEnabled(ctx.Config.Sync.FromHost.Events.Enabled, events.New),
5455
isEnabled(ctx.Config.Sync.ToHost.PersistentVolumeClaims.Enabled, persistentvolumeclaims.New),

pkg/mappings/registry.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/loft-sh/vcluster/pkg/scheme"
1010
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
1111
corev1 "k8s.io/api/core/v1"
12+
discoveryv1 "k8s.io/api/discovery/v1"
1213
networkingv1 "k8s.io/api/networking/v1"
1314
schedulingv1 "k8s.io/api/scheduling/v1"
1415
storagev1 "k8s.io/api/storage/v1"
@@ -105,6 +106,10 @@ func Endpoints() schema.GroupVersionKind {
105106
return corev1.SchemeGroupVersion.WithKind("Endpoints")
106107
}
107108

109+
func EndpointSlices() schema.GroupVersionKind {
110+
return discoveryv1.SchemeGroupVersion.WithKind("EndpointSlice")
111+
}
112+
108113
func Services() schema.GroupVersionKind {
109114
return corev1.SchemeGroupVersion.WithKind("Service")
110115
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package resources
2+
3+
import (
4+
"github.com/loft-sh/vcluster/pkg/mappings/generic"
5+
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
6+
"github.com/loft-sh/vcluster/pkg/util/translate"
7+
discoveryv1 "k8s.io/api/discovery/v1"
8+
"k8s.io/apimachinery/pkg/types"
9+
"sigs.k8s.io/controller-runtime/pkg/client"
10+
)
11+
12+
func CreateEndpointSlicesMapper(ctx *synccontext.RegisterContext) (synccontext.Mapper, error) {
13+
mapper, err := generic.NewMapper(ctx, &discoveryv1.EndpointSlice{}, translate.Default.HostName)
14+
if err != nil {
15+
return nil, err
16+
}
17+
18+
return &endpointSlicesMapper{
19+
Mapper: mapper,
20+
}, nil
21+
}
22+
23+
type endpointSlicesMapper struct {
24+
synccontext.Mapper
25+
}
26+
27+
func (s *endpointSlicesMapper) VirtualToHost(ctx *synccontext.SyncContext, req types.NamespacedName, vObj client.Object) types.NamespacedName {
28+
if req.Name == "kubernetes" && req.Namespace == "default" {
29+
return types.NamespacedName{
30+
Name: translate.VClusterName,
31+
Namespace: ctx.CurrentNamespace,
32+
}
33+
}
34+
35+
return s.Mapper.VirtualToHost(ctx, req, vObj)
36+
}
37+
38+
func (s *endpointSlicesMapper) HostToVirtual(ctx *synccontext.SyncContext, req types.NamespacedName, pObj client.Object) types.NamespacedName {
39+
if req.Name == translate.VClusterName && req.Namespace == ctx.CurrentNamespace {
40+
return types.NamespacedName{
41+
Name: "kubernetes",
42+
Namespace: "default",
43+
}
44+
}
45+
46+
return s.Mapper.HostToVirtual(ctx, req, pObj)
47+
}

pkg/mappings/resources/register.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func getMappers(ctx *synccontext.RegisterContext) []BuildMapper {
1717
CreateSecretsMapper,
1818
CreateConfigMapsMapper,
1919
CreateEndpointsMapper,
20+
CreateEndpointSlicesMapper,
2021
CreateEventsMapper,
2122
isEnabled(ctx.Config.Sync.ToHost.Ingresses.Enabled, CreateIngressesMapper),
2223
CreateNamespacesMapper,

0 commit comments

Comments
 (0)