Skip to content

Commit 62cc1f0

Browse files
committed
Reapply "fix: patch virtual instead of physical and always add host ip annotations to physical (#3147)" (#3151)
This reverts commit 7f97d20. (cherry picked from commit 15de457)
1 parent fe9c540 commit 62cc1f0

File tree

4 files changed

+50
-49
lines changed

4 files changed

+50
-49
lines changed

pkg/controllers/resources/pods/syncer.go

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"reflect"
77
"slices"
8-
"strings"
98
"time"
109

1110
nodev1 "k8s.io/api/node/v1"
@@ -252,19 +251,6 @@ func (s *podSyncer) SyncToHost(ctx *synccontext.SyncContext, event *synccontext.
252251
if pPod.Spec.NodeName == "" {
253252
return ctrl.Result{}, nil
254253
}
255-
256-
if s.fakeKubeletIPs {
257-
nodeService, err := s.ensureNodeService(ctx, pPod)
258-
if err != nil {
259-
if kerrors.IsNotFound(err) {
260-
return ctrl.Result{RequeueAfter: time.Second}, nil
261-
}
262-
return ctrl.Result{}, err
263-
}
264-
265-
pPod.Annotations[translatepods.HostIPAnnotation] = nodeService.Spec.ClusterIP
266-
pPod.Annotations[translatepods.HostIPsAnnotation] = nodeService.Spec.ClusterIP
267-
}
268254
}
269255

270256
err = pro.ApplyPatchesHostObject(ctx, nil, pPod, event.Virtual, ctx.Config.Sync.ToHost.Pods.Patches, false)
@@ -349,21 +335,6 @@ func (s *podSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.SyncEv
349335
return patcher.DeleteVirtualObjectWithOptions(ctx, event.Virtual, event.Host, "node name is different between the two", &client.DeleteOptions{GracePeriodSeconds: &minimumGracePeriodInSeconds})
350336
}
351337

352-
if s.fakeKubeletIPs && event.Host.Status.HostIP != "" {
353-
nodeService, err := s.ensureNodeService(ctx, event.Host)
354-
if err != nil {
355-
if kerrors.IsNotFound(err) {
356-
return ctrl.Result{RequeueAfter: time.Second}, nil
357-
}
358-
return ctrl.Result{}, err
359-
}
360-
361-
event.Host.Status.HostIP = nodeService.Spec.ClusterIP
362-
event.Host.Status.HostIPs = []corev1.HostIP{
363-
{IP: nodeService.Spec.ClusterIP},
364-
}
365-
}
366-
367338
// validate virtual pod before syncing it to the host cluster
368339
if s.podSecurityStandard != "" {
369340
valid, err := s.isPodSecurityStandardsValid(ctx, event.Virtual, ctx.Log)
@@ -424,6 +395,10 @@ func (s *podSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.SyncEv
424395
// update the virtual pod if the spec has changed
425396
err = s.podTranslator.Diff(ctx, event)
426397
if err != nil {
398+
if kerrors.IsNotFound(err) {
399+
return ctrl.Result{RequeueAfter: time.Second}, nil
400+
}
401+
427402
return ctrl.Result{}, err
428403
}
429404

@@ -547,17 +522,6 @@ func (s *podSyncer) assignNodeToPod(ctx *synccontext.SyncContext, pObj *corev1.P
547522
return nil
548523
}
549524

550-
func (s *podSyncer) ensureNodeService(ctx *synccontext.SyncContext, pPod *corev1.Pod) (*corev1.Service, error) {
551-
serviceName := translate.SafeConcatName(translate.VClusterName, "node", strings.ReplaceAll(pPod.Spec.NodeName, ".", "-"))
552-
553-
nodeService := &corev1.Service{}
554-
err := ctx.CurrentNamespaceClient.Get(ctx.Context, types.NamespacedName{Name: serviceName, Namespace: ctx.CurrentNamespace}, nodeService)
555-
if err != nil {
556-
return nil, fmt.Errorf("get node service: %w", err)
557-
}
558-
return nodeService, nil
559-
}
560-
561525
func (s *podSyncer) applyLimitByClasses(ctx *synccontext.SyncContext, virtual *corev1.Pod) bool {
562526
return s.applyLimitByPriorityClass(ctx, virtual) || s.applyLimitByRuntimeClass(ctx, virtual)
563527
}

pkg/controllers/resources/pods/syncer_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,10 @@ func TestSync(t *testing.T) {
602602
{IP: "3.3.3.3"},
603603
}
604604

605+
pPodFakeKubeletHostIPs := pPodFakeKubelet.DeepCopy()
606+
pPodFakeKubeletHostIPs.Annotations[podtranslate.HostIPAnnotation] = pVclusterService.Spec.ClusterIP
607+
pPodFakeKubeletHostIPs.Annotations[podtranslate.HostIPsAnnotation] = pVclusterService.Spec.ClusterIP
608+
605609
vPodWithNodeName := &corev1.Pod{
606610
ObjectMeta: vObjectMeta,
607611
Spec: corev1.PodSpec{
@@ -675,9 +679,14 @@ func TestSync(t *testing.T) {
675679
Name: "Fake Kubelet enabled with Node sync",
676680
InitialVirtualState: []runtime.Object{testNode.DeepCopy(), vPodWithNodeName, vNamespace.DeepCopy()},
677681
InitialPhysicalState: []runtime.Object{testNode.DeepCopy(), pVclusterNodeService.DeepCopy(), pPodFakeKubelet.DeepCopy()},
682+
// The virtual pod should have the host IPs of the node service in its status.
678683
ExpectedVirtualState: map[schema.GroupVersionKind][]runtime.Object{
679684
corev1.SchemeGroupVersion.WithKind("Pod"): {vPodWithHostIP},
680685
},
686+
// The physical pod should have the host IPs of the node service in its annotations.
687+
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
688+
corev1.SchemeGroupVersion.WithKind("Pod"): {pPodFakeKubeletHostIPs},
689+
},
681690
Sync: func(ctx *synccontext.RegisterContext) {
682691
ctx.Config.Sync.FromHost.Nodes.Selector.All = true
683692
ctx.Config.Networking.Advanced.ProxyKubelets.ByIP = true

pkg/controllers/resources/pods/translate/diff.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package translate
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"strings"
67

78
"github.com/loft-sh/vcluster/pkg/patcher"
89
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
910
"github.com/loft-sh/vcluster/pkg/util/translate"
1011
appsv1 "k8s.io/api/apps/v1"
1112
corev1 "k8s.io/api/core/v1"
13+
"k8s.io/apimachinery/pkg/types"
1214
"sigs.k8s.io/controller-runtime/pkg/client"
1315
)
1416

@@ -83,11 +85,26 @@ func (t *translator) Diff(ctx *synccontext.SyncContext, event *synccontext.SyncE
8385
delete(event.Host.Annotations, OwnerSetKind)
8486
}
8587

88+
if t.fakeKubeletIPs && event.Host.Status.HostIP != "" {
89+
nodeService, err := ensureNodeService(ctx, event.Host)
90+
if err != nil {
91+
return err
92+
}
93+
94+
event.Virtual.Status.HostIP = nodeService.Spec.ClusterIP
95+
event.Virtual.Status.HostIPs = []corev1.HostIP{
96+
{IP: nodeService.Spec.ClusterIP},
97+
}
98+
99+
event.Host.Annotations[HostIPAnnotation] = nodeService.Spec.ClusterIP
100+
event.Host.Annotations[HostIPsAnnotation] = nodeService.Spec.ClusterIP
101+
}
102+
86103
return nil
87104
}
88105

89106
func GetExcludedAnnotations(pPod *corev1.Pod) []string {
90-
annotations := []string{ClusterAutoScalerAnnotation, OwnerReferences, OwnerSetKind, NamespaceAnnotation, NameAnnotation, UIDAnnotation, ServiceAccountNameAnnotation, HostsRewrittenAnnotation, VClusterLabelsAnnotation}
107+
annotations := []string{ClusterAutoScalerAnnotation, OwnerReferences, OwnerSetKind, NamespaceAnnotation, NameAnnotation, UIDAnnotation, ServiceAccountNameAnnotation, HostsRewrittenAnnotation, VClusterLabelsAnnotation, HostIPAnnotation, HostIPsAnnotation}
91108
if pPod != nil {
92109
for _, v := range pPod.Spec.Volumes {
93110
if v.Projected != nil {
@@ -113,6 +130,17 @@ func GetExcludedAnnotations(pPod *corev1.Pod) []string {
113130
return annotations
114131
}
115132

133+
func ensureNodeService(ctx *synccontext.SyncContext, pPod *corev1.Pod) (*corev1.Service, error) {
134+
serviceName := translate.SafeConcatName(translate.VClusterName, "node", strings.ReplaceAll(pPod.Spec.NodeName, ".", "-"))
135+
136+
nodeService := &corev1.Service{}
137+
err := ctx.CurrentNamespaceClient.Get(ctx.Context, types.NamespacedName{Name: serviceName, Namespace: ctx.CurrentNamespace}, nodeService)
138+
if err != nil {
139+
return nil, fmt.Errorf("get node service: %w", err)
140+
}
141+
return nodeService, nil
142+
}
143+
116144
// Changeable fields within the pod:
117145
// - spec.containers[*].image
118146
// - spec.initContainers[*].image

pkg/controllers/resources/pods/translate/translator.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ func (t *translator) translateVolumes(ctx *synccontext.SyncContext, pPod *corev1
442442
}
443443
if pPod.Spec.Volumes[i].DownwardAPI != nil {
444444
for j := range pPod.Spec.Volumes[i].DownwardAPI.Items {
445-
translateFieldRef(pPod.Spec.Volumes[i].DownwardAPI.Items[j].FieldRef, t.fakeKubeletIPs, t.schedulingConfig.IsSchedulerFromVirtualCluster(pPod.Spec.SchedulerName))
445+
translateFieldRef(pPod.Spec.Volumes[i].DownwardAPI.Items[j].FieldRef, t.fakeKubeletIPs)
446446
}
447447
}
448448
if pPod.Spec.Volumes[i].ISCSI != nil && pPod.Spec.Volumes[i].ISCSI.SecretRef != nil {
@@ -508,7 +508,7 @@ func (t *translator) translateProjectedVolume(
508508
}
509509
if projectedVolume.Sources[i].DownwardAPI != nil {
510510
for j := range projectedVolume.Sources[i].DownwardAPI.Items {
511-
translateFieldRef(projectedVolume.Sources[i].DownwardAPI.Items[j].FieldRef, t.fakeKubeletIPs, t.schedulingConfig.IsSchedulerFromVirtualCluster(pPod.Spec.SchedulerName))
511+
translateFieldRef(projectedVolume.Sources[i].DownwardAPI.Items[j].FieldRef, t.fakeKubeletIPs)
512512
}
513513
}
514514
if projectedVolume.Sources[i].ServiceAccountToken != nil {
@@ -607,7 +607,7 @@ func (t *translator) translateProjectedVolume(
607607
return nil
608608
}
609609

610-
func translateFieldRef(fieldSelector *corev1.ObjectFieldSelector, fakeKubeletIPs, enableScheduler bool) {
610+
func translateFieldRef(fieldSelector *corev1.ObjectFieldSelector, fakeKubeletIPs bool) {
611611
if fieldSelector == nil {
612612
return
613613
}
@@ -632,11 +632,11 @@ func translateFieldRef(fieldSelector *corev1.ObjectFieldSelector, fakeKubeletIPs
632632
fieldSelector.FieldPath = "metadata.annotations['" + ServiceAccountNameAnnotation + "']"
633633
// translate downward API references for status.hostIP(s) only when both virtual scheduler & fakeKubeletIPs are enabled
634634
case "status.hostIP":
635-
if fakeKubeletIPs && enableScheduler {
635+
if fakeKubeletIPs {
636636
fieldSelector.FieldPath = "metadata.annotations['" + HostIPAnnotation + "']"
637637
}
638638
case "status.hostIPs":
639-
if fakeKubeletIPs && enableScheduler {
639+
if fakeKubeletIPs {
640640
fieldSelector.FieldPath = "metadata.annotations['" + HostIPsAnnotation + "']"
641641
}
642642
}
@@ -645,7 +645,7 @@ func translateFieldRef(fieldSelector *corev1.ObjectFieldSelector, fakeKubeletIPs
645645
func (t *translator) TranslateContainerEnv(ctx *synccontext.SyncContext, envVar []corev1.EnvVar, envFrom []corev1.EnvFromSource, vPod *corev1.Pod, serviceEnvMap map[string]string) ([]corev1.EnvVar, []corev1.EnvFromSource, error) {
646646
envNameMap := make(map[string]struct{})
647647
for j, env := range envVar {
648-
translateDownwardAPI(&envVar[j], t.fakeKubeletIPs, t.schedulingConfig.IsSchedulerFromVirtualCluster(vPod.Spec.SchedulerName))
648+
translateDownwardAPI(&envVar[j], t.fakeKubeletIPs)
649649
if env.ValueFrom != nil && env.ValueFrom.ConfigMapKeyRef != nil && env.ValueFrom.ConfigMapKeyRef.Name != "" {
650650
envVar[j].ValueFrom.ConfigMapKeyRef.Name = mappings.VirtualToHostName(ctx, envVar[j].ValueFrom.ConfigMapKeyRef.Name, vPod.Namespace, mappings.ConfigMaps())
651651
}
@@ -686,14 +686,14 @@ func (t *translator) TranslateContainerEnv(ctx *synccontext.SyncContext, envVar
686686
return envVar, envFrom, nil
687687
}
688688

689-
func translateDownwardAPI(env *corev1.EnvVar, fakeKubeletIPs, enableScheduler bool) {
689+
func translateDownwardAPI(env *corev1.EnvVar, fakeKubeletIPs bool) {
690690
if env.ValueFrom == nil {
691691
return
692692
}
693693
if env.ValueFrom.FieldRef == nil {
694694
return
695695
}
696-
translateFieldRef(env.ValueFrom.FieldRef, fakeKubeletIPs, enableScheduler)
696+
translateFieldRef(env.ValueFrom.FieldRef, fakeKubeletIPs)
697697
}
698698

699699
func (t *translator) translateDNSConfig(pPod *corev1.Pod, vPod *corev1.Pod, nameServer string) {

0 commit comments

Comments
 (0)