Skip to content

Commit 3d62338

Browse files
fix: revert fake kubelet ip in pod syncer (#3191) (#3195)
(cherry picked from commit b3f6fc4) Co-authored-by: Johannes Frey <[email protected]>
1 parent eb1ba50 commit 3d62338

File tree

4 files changed

+7
-130
lines changed

4 files changed

+7
-130
lines changed

pkg/controllers/resources/pods/syncer_test.go

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"maps"
66
"testing"
7-
"time"
87

98
"gotest.tools/assert"
109
corev1 "k8s.io/api/core/v1"
@@ -591,39 +590,6 @@ func TestSync(t *testing.T) {
591590
},
592591
}
593592

594-
testNodeName := "test123"
595-
pVclusterNodeService := pVclusterService.DeepCopy()
596-
pVclusterNodeService.Name = translate.SafeConcatName(testingutil.DefaultTestVClusterName, "node", testNodeName)
597-
598-
pPodFakeKubelet := pPodBase.DeepCopy()
599-
pPodFakeKubelet.Spec.NodeName = testNodeName
600-
pPodFakeKubelet.Status.HostIP = "3.3.3.3"
601-
pPodFakeKubelet.Status.HostIPs = []corev1.HostIP{
602-
{IP: "3.3.3.3"},
603-
}
604-
605-
pPodFakeKubeletHostIPs := pPodFakeKubelet.DeepCopy()
606-
pPodFakeKubeletHostIPs.Annotations[podtranslate.HostIPAnnotation] = pVclusterService.Spec.ClusterIP
607-
pPodFakeKubeletHostIPs.Annotations[podtranslate.HostIPsAnnotation] = pVclusterService.Spec.ClusterIP
608-
609-
vPodWithNodeName := &corev1.Pod{
610-
ObjectMeta: vObjectMeta,
611-
Spec: corev1.PodSpec{
612-
NodeName: testNodeName,
613-
},
614-
}
615-
vPodWithHostIP := vPodWithNodeName.DeepCopy()
616-
vPodWithHostIP.Status.HostIP = pVclusterService.Spec.ClusterIP
617-
vPodWithHostIP.Status.HostIPs = []corev1.HostIP{
618-
{IP: pVclusterService.Spec.ClusterIP},
619-
}
620-
621-
testNode := &corev1.Node{
622-
ObjectMeta: metav1.ObjectMeta{
623-
Name: testNodeName,
624-
},
625-
}
626-
627593
priorityClassName := "high-priority"
628594
pPriorityClass := &schedulingv1.PriorityClass{
629595
ObjectMeta: metav1.ObjectMeta{
@@ -675,43 +641,6 @@ func TestSync(t *testing.T) {
675641
assert.NilError(t, err)
676642
},
677643
},
678-
{
679-
Name: "Fake Kubelet enabled with Node sync",
680-
InitialVirtualState: []runtime.Object{testNode.DeepCopy(), vPodWithNodeName, vNamespace.DeepCopy()},
681-
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.
683-
ExpectedVirtualState: map[schema.GroupVersionKind][]runtime.Object{
684-
corev1.SchemeGroupVersion.WithKind("Pod"): {vPodWithHostIP},
685-
},
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-
},
690-
Sync: func(ctx *synccontext.RegisterContext) {
691-
ctx.Config.Sync.FromHost.Nodes.Selector.All = true
692-
ctx.Config.Networking.Advanced.ProxyKubelets.ByIP = true
693-
syncContext, syncer := syncertesting.FakeStartSyncer(t, ctx, New)
694-
_, err := syncer.(*podSyncer).Sync(syncContext, synccontext.NewSyncEventWithOld(pPodFakeKubelet, pPodFakeKubelet, vPodWithNodeName, vPodWithNodeName))
695-
assert.NilError(t, err)
696-
},
697-
},
698-
{
699-
Name: "Fake Kubelet enabled with Node sync and node service not found",
700-
InitialVirtualState: []runtime.Object{testNode.DeepCopy(), vPodWithNodeName, vNamespace.DeepCopy()},
701-
InitialPhysicalState: []runtime.Object{testNode.DeepCopy(), pPodFakeKubelet.DeepCopy()},
702-
ExpectedVirtualState: map[schema.GroupVersionKind][]runtime.Object{
703-
corev1.SchemeGroupVersion.WithKind("Pod"): {vPodWithNodeName},
704-
},
705-
Sync: func(ctx *synccontext.RegisterContext) {
706-
ctx.Config.Sync.FromHost.Nodes.Selector.All = true
707-
ctx.Config.Networking.Advanced.ProxyKubelets.ByIP = true
708-
syncContext, syncer := syncertesting.FakeStartSyncer(t, ctx, New)
709-
710-
result, err := syncer.(*podSyncer).Sync(syncContext, synccontext.NewSyncEventWithOld(pPodFakeKubelet, pPodFakeKubelet, vPodWithNodeName, vPodWithNodeName))
711-
assert.NilError(t, err)
712-
assert.Equal(t, result.RequeueAfter, time.Second, "Should requeue if node service is not found")
713-
},
714-
},
715644
{
716645
Name: "From Host PriorityClasses sync enabled",
717646
InitialVirtualState: []runtime.Object{vPodWithPriorityClass, vNamespace.DeepCopy()},

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

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

33
import (
44
"encoding/json"
5-
"fmt"
65
"strings"
76

87
"github.com/loft-sh/vcluster/pkg/patcher"
98
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
109
"github.com/loft-sh/vcluster/pkg/util/translate"
1110
appsv1 "k8s.io/api/apps/v1"
1211
corev1 "k8s.io/api/core/v1"
13-
"k8s.io/apimachinery/pkg/types"
1412
"sigs.k8s.io/controller-runtime/pkg/client"
1513
)
1614

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

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-
10386
return nil
10487
}
10588

10689
func GetExcludedAnnotations(pPod *corev1.Pod) []string {
107-
annotations := []string{ClusterAutoScalerAnnotation, OwnerReferences, OwnerSetKind, NamespaceAnnotation, NameAnnotation, UIDAnnotation, ServiceAccountNameAnnotation, HostsRewrittenAnnotation, VClusterLabelsAnnotation, HostIPAnnotation, HostIPsAnnotation}
90+
annotations := []string{ClusterAutoScalerAnnotation, OwnerReferences, OwnerSetKind, NamespaceAnnotation, NameAnnotation, UIDAnnotation, ServiceAccountNameAnnotation, HostsRewrittenAnnotation, VClusterLabelsAnnotation}
10891
if pPod != nil {
10992
for _, v := range pPod.Spec.Volumes {
11093
if v.Projected != nil {
@@ -130,17 +113,6 @@ func GetExcludedAnnotations(pPod *corev1.Pod) []string {
130113
return annotations
131114
}
132115

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-
144116
// Changeable fields within the pod:
145117
// - spec.containers[*].image
146118
// - spec.initContainers[*].image

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

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ const (
4242
ClusterAutoScalerDaemonSetAnnotation = "cluster-autoscaler.kubernetes.io/daemonset-pod"
4343
ServiceAccountNameAnnotation = "vcluster.loft.sh/service-account-name"
4444
ServiceAccountTokenAnnotation = "vcluster.loft.sh/token-"
45-
HostIPAnnotation = "vcluster.loft.sh/host-ip"
46-
HostIPsAnnotation = "vcluster.loft.sh/host-ips"
4745
)
4846

4947
var (
@@ -115,7 +113,6 @@ func NewTranslator(ctx *synccontext.RegisterContext, eventRecorder record.EventR
115113
hostPriorityClassesSyncEnabled: ctx.Config.Sync.FromHost.PriorityClasses.Enabled,
116114
priorityClassesSyncEnabled: ctx.Config.Sync.ToHost.PriorityClasses.Enabled,
117115
schedulingConfig: schedulingConfig,
118-
fakeKubeletIPs: ctx.Config.Networking.Advanced.ProxyKubelets.ByIP,
119116

120117
mountPhysicalHostPaths: ctx.Config.ControlPlane.HostPathMapper.Enabled && !ctx.Config.ControlPlane.HostPathMapper.Central,
121118

@@ -148,7 +145,6 @@ type translator struct {
148145
hostPriorityClassesSyncEnabled bool
149146
priorityClassesSyncEnabled bool
150147
schedulingConfig scheduling.Config
151-
fakeKubeletIPs bool
152148

153149
virtualLogsPath string
154150
virtualPodLogsPath string
@@ -438,7 +434,7 @@ func (t *translator) translateVolumes(ctx *synccontext.SyncContext, pPod *corev1
438434
}
439435
if pPod.Spec.Volumes[i].DownwardAPI != nil {
440436
for j := range pPod.Spec.Volumes[i].DownwardAPI.Items {
441-
translateFieldRef(pPod.Spec.Volumes[i].DownwardAPI.Items[j].FieldRef, t.fakeKubeletIPs)
437+
translateFieldRef(pPod.Spec.Volumes[i].DownwardAPI.Items[j].FieldRef)
442438
}
443439
}
444440
if pPod.Spec.Volumes[i].ISCSI != nil && pPod.Spec.Volumes[i].ISCSI.SecretRef != nil {
@@ -504,7 +500,7 @@ func (t *translator) translateProjectedVolume(
504500
}
505501
if projectedVolume.Sources[i].DownwardAPI != nil {
506502
for j := range projectedVolume.Sources[i].DownwardAPI.Items {
507-
translateFieldRef(projectedVolume.Sources[i].DownwardAPI.Items[j].FieldRef, t.fakeKubeletIPs)
503+
translateFieldRef(projectedVolume.Sources[i].DownwardAPI.Items[j].FieldRef)
508504
}
509505
}
510506
if projectedVolume.Sources[i].ServiceAccountToken != nil {
@@ -603,7 +599,7 @@ func (t *translator) translateProjectedVolume(
603599
return nil
604600
}
605601

606-
func translateFieldRef(fieldSelector *corev1.ObjectFieldSelector, fakeKubeletIPs bool) {
602+
func translateFieldRef(fieldSelector *corev1.ObjectFieldSelector) {
607603
if fieldSelector == nil {
608604
return
609605
}
@@ -626,22 +622,13 @@ func translateFieldRef(fieldSelector *corev1.ObjectFieldSelector, fakeKubeletIPs
626622
fieldSelector.FieldPath = "metadata.annotations['" + UIDAnnotation + "']"
627623
case "spec.serviceAccountName":
628624
fieldSelector.FieldPath = "metadata.annotations['" + ServiceAccountNameAnnotation + "']"
629-
// translate downward API references for status.hostIP(s) only when both virtual scheduler & fakeKubeletIPs are enabled
630-
case "status.hostIP":
631-
if fakeKubeletIPs {
632-
fieldSelector.FieldPath = "metadata.annotations['" + HostIPAnnotation + "']"
633-
}
634-
case "status.hostIPs":
635-
if fakeKubeletIPs {
636-
fieldSelector.FieldPath = "metadata.annotations['" + HostIPsAnnotation + "']"
637-
}
638625
}
639626
}
640627

641628
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) {
642629
envNameMap := make(map[string]struct{})
643630
for j, env := range envVar {
644-
translateDownwardAPI(&envVar[j], t.fakeKubeletIPs)
631+
translateDownwardAPI(&envVar[j])
645632
if env.ValueFrom != nil && env.ValueFrom.ConfigMapKeyRef != nil && env.ValueFrom.ConfigMapKeyRef.Name != "" {
646633
envVar[j].ValueFrom.ConfigMapKeyRef.Name = mappings.VirtualToHostName(ctx, envVar[j].ValueFrom.ConfigMapKeyRef.Name, vPod.Namespace, mappings.ConfigMaps())
647634
}
@@ -682,14 +669,14 @@ func (t *translator) TranslateContainerEnv(ctx *synccontext.SyncContext, envVar
682669
return envVar, envFrom, nil
683670
}
684671

685-
func translateDownwardAPI(env *corev1.EnvVar, fakeKubeletIPs bool) {
672+
func translateDownwardAPI(env *corev1.EnvVar) {
686673
if env.ValueFrom == nil {
687674
return
688675
}
689676
if env.ValueFrom.FieldRef == nil {
690677
return
691678
}
692-
translateFieldRef(env.ValueFrom.FieldRef, fakeKubeletIPs)
679+
translateFieldRef(env.ValueFrom.FieldRef)
693680
}
694681

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

test/e2e/syncer/pods/pods.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ var _ = ginkgo.Describe("Pods are running in the host cluster", func() {
8282
pod, err := f.HostClient.CoreV1().Pods(pPodName.Namespace).Get(f.Context, pPodName.Name, metav1.GetOptions{})
8383
framework.ExpectNoError(err)
8484

85-
// ignore HostIP differences
86-
resetHostIP(vpod, pod)
87-
8885
// Since k8s 1.32, status.QOSClass field has become immutable,
8986
// hence we have stopeed syncing it. So ignore
9087
// the differences in the status.QOSClass field
@@ -218,9 +215,6 @@ var _ = ginkgo.Describe("Pods are running in the host cluster", func() {
218215
pod, err := f.HostClient.CoreV1().Pods(pPodName.Namespace).Get(f.Context, pPodName.Name, metav1.GetOptions{})
219216
framework.ExpectNoError(err)
220217

221-
// ignore HostIP differences
222-
resetHostIP(vpod, pod)
223-
224218
// Since k8s 1.32, status.QOSClass field has become immutable,
225219
// hence we have stopeed syncing it. So ignore
226220
// the differences in the status.QOSClass field
@@ -772,11 +766,6 @@ var _ = ginkgo.Describe("Pods are running in the host cluster", func() {
772766
})
773767
})
774768

775-
func resetHostIP(vpod, pod *corev1.Pod) {
776-
vpod.Status.HostIP, pod.Status.HostIP = "", ""
777-
vpod.Status.HostIPs, pod.Status.HostIPs = nil, nil
778-
}
779-
780769
func ignoreQOSClassDiff(vpod, pod *corev1.Pod) {
781770
pod.Status.QOSClass = vpod.Status.QOSClass
782771
}

0 commit comments

Comments
 (0)