Skip to content

Commit aedef3a

Browse files
committed
modify eps syncer labels
1 parent 13cce86 commit aedef3a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

pkg/controllers/resources/endpointslices/syncer.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,23 @@ func (s *endpointSliceSyncer) ReconcileStart(ctx *synccontext.SyncContext, req c
115115
}
116116
}
117117

118-
svc := &corev1.Service{}
118+
eps := &discoveryv1.EndpointSlice{}
119119
err := ctx.VirtualClient.Get(ctx, types.NamespacedName{
120120
Namespace: req.Namespace,
121121
Name: req.Name,
122+
}, eps)
123+
124+
epsLabels := eps.GetLabels()
125+
svcName, ok := epsLabels[translate.K8sServiceNameAnnotation]
126+
if !ok {
127+
klog.Infof("Error retrieving service name: %v", err)
128+
return true, nil
129+
}
130+
131+
svc := &corev1.Service{}
132+
err = ctx.VirtualClient.Get(ctx, types.NamespacedName{
133+
Namespace: req.Namespace,
134+
Name: svcName,
122135
}, svc)
123136
if err != nil {
124137
if kerrors.IsNotFound(err) {

pkg/controllers/resources/endpointslices/translate.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ func (s *endpointSliceSyncer) translate(ctx *synccontext.SyncContext, vObj clien
1313
endpointSlice := translate.HostMetadata(vObj.(*discoveryv1.EndpointSlice),
1414
s.VirtualToHost(ctx, types.NamespacedName{Name: vObj.GetName(), Namespace: vObj.GetNamespace()}, vObj),
1515
s.excludedAnnotations...)
16+
17+
virtualSvcName := endpointSlice.GetLabels()[translate.K8sServiceNameAnnotation]
18+
vcName := endpointSlice.GetLabels()[translate.MarkerLabel]
19+
namespace := endpointSlice.GetLabels()[translate.NamespaceLabel]
20+
hostSvcName := translateSvcName(virtualSvcName, namespace, vcName)
21+
22+
// in case of selector-less service, we need to add "kubernetes.io/service-name" label manually
23+
endpointSlice.Labels[translate.K8sServiceNameAnnotation] = hostSvcName
1624
s.translateSpec(ctx, endpointSlice)
1725
return endpointSlice
1826
}
@@ -35,3 +43,7 @@ func (s *endpointSliceSyncer) translateUpdate(ctx *synccontext.SyncContext, pObj
3543
pObj.Endpoints = translated.Endpoints
3644
return nil
3745
}
46+
47+
func translateSvcName(virtualSvcName, namespace, vcName string) string {
48+
return virtualSvcName + "-x-" + namespace + "-x-" + vcName
49+
}

pkg/util/translate/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var (
3030

3131
ManagedAnnotationsAnnotation = "vcluster.loft.sh/managed-annotations"
3232
ManagedLabelsAnnotation = "vcluster.loft.sh/managed-labels"
33+
34+
K8sServiceNameAnnotation = "kubernetes.io/service-name"
3335
)
3436

3537
var Default Translator = &singleNamespace{}

0 commit comments

Comments
 (0)