Skip to content

Commit 9037b71

Browse files
Merge pull request #15 from gocrane/feature/hostnetwork-for-hostport-fake-pod
feat[pod hostport]: support hostNetwork for hostport fake pod
2 parents 7705a31 + dba1929 commit 9037b71

3 files changed

Lines changed: 33 additions & 11 deletions

File tree

pkg/syncer/bottomup/hostport/hostport_node_controller.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,9 @@ func (r *HostPortNodeReconciler) collectHostPorts(pods []corev1.Pod) []corev1.Co
352352
for _, port := range container.Ports {
353353
if port.HostPort != 0 {
354354
hostPorts = append(hostPorts, corev1.ContainerPort{
355-
Name: pod.Namespace + "/" + pod.Name + "/" + container.Name + "/" + port.Name,
356-
ContainerPort: port.ContainerPort,
355+
Name: pod.Namespace + "/" + pod.Name + "/" + container.Name + "/" + port.Name,
356+
// For fake pod with hostNetwork=true, containerPort must equal hostPort
357+
ContainerPort: port.HostPort,
357358
HostPort: port.HostPort,
358359
Protocol: port.Protocol,
359360
HostIP: port.HostIP,
@@ -367,8 +368,9 @@ func (r *HostPortNodeReconciler) collectHostPorts(pods []corev1.Pod) []corev1.Co
367368
for _, port := range container.Ports {
368369
if port.HostPort != 0 {
369370
hostPorts = append(hostPorts, corev1.ContainerPort{
370-
Name: pod.Namespace + "/" + pod.Name + "/" + container.Name + "/" + port.Name,
371-
ContainerPort: port.ContainerPort,
371+
Name: pod.Namespace + "/" + pod.Name + "/" + container.Name + "/" + port.Name,
372+
// For fake pod with hostNetwork=true, containerPort must equal hostPort
373+
ContainerPort: port.HostPort,
372374
HostPort: port.HostPort,
373375
Protocol: port.Protocol,
374376
HostIP: port.HostIP,
@@ -535,6 +537,7 @@ func (r *HostPortNodeReconciler) buildFakePod(virtualNodeName string, hostPorts
535537
PriorityClassName: SystemNodeCriticalPriorityClass,
536538
Tolerations: tolerations,
537539
RestartPolicy: corev1.RestartPolicyNever,
540+
HostNetwork: true,
538541
Containers: []corev1.Container{
539542
{
540543
Name: FakeContainerName,

pkg/syncer/bottomup/hostport/hostport_node_controller_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ func TestHostPortNodeReconciler_Reconcile(t *testing.T) {
272272
})
273273
assert.NoError(t, err, "Should be able to list fake pods")
274274
assert.Greater(t, len(fakePods.Items), 0, "Should have created fake pod")
275+
276+
// Verify fake pod has hostNetwork enabled
277+
for _, fakePod := range fakePods.Items {
278+
assert.True(t, fakePod.Spec.HostNetwork, "Fake pod should have hostNetwork=true")
279+
}
275280
}
276281
})
277282
}
@@ -832,6 +837,9 @@ func TestHostPortNodeReconciler_collectHostPorts(t *testing.T) {
832837
if port.Protocol == "" {
833838
assert.Equal(t, corev1.ProtocolTCP, port.Protocol, "Default protocol should be TCP")
834839
}
840+
// Verify ContainerPort equals HostPort (required for hostNetwork=true)
841+
assert.Equal(t, port.HostPort, port.ContainerPort,
842+
"ContainerPort should equal HostPort for fake pod with hostNetwork=true")
835843
}
836844

837845
// Verify ordering (should be sorted)
@@ -914,6 +922,9 @@ func TestHostPortNodeReconciler_buildFakePod(t *testing.T) {
914922
assert.Equal(t, SystemNodeCriticalPriorityClass, result.Spec.PriorityClassName, "Should have system priority")
915923
assert.Equal(t, corev1.RestartPolicyNever, result.Spec.RestartPolicy, "Should have Never restart policy")
916924

925+
// Verify hostNetwork is enabled (required for hostPort synchronization)
926+
assert.True(t, result.Spec.HostNetwork, "Fake pod should have hostNetwork=true for hostPort reservation")
927+
917928
// Verify labels
918929
assert.Equal(t, cloudv1beta1.LabelValueTrue, result.Labels[cloudv1beta1.LabelHostPortFakePod])
919930
assert.Equal(t, cloudv1beta1.LabelManagedByValue, result.Labels[cloudv1beta1.LabelManagedBy])

test/integration/hostport_integration_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,10 @@ func verifyFakePodCreation(ctx context.Context, virtualNodeName string) {
440440
gomega.Expect(fakePod.Labels[cloudv1beta1.LabelHostPortFakePod]).To(gomega.Equal(cloudv1beta1.LabelValueTrue))
441441
gomega.Expect(fakePod.Labels[cloudv1beta1.LabelManagedBy]).To(gomega.Equal(cloudv1beta1.LabelManagedByValue))
442442

443+
// Verify hostNetwork is enabled (required for hostPort reservation)
444+
gomega.Expect(fakePod.Spec.HostNetwork).To(gomega.BeTrue(),
445+
"Fake pod should have hostNetwork=true for proper hostPort reservation")
446+
443447
// Verify tolerations (should tolerate all taints)
444448
gomega.Expect(fakePod.Spec.Tolerations).To(gomega.ContainElement(corev1.Toleration{
445449
Operator: corev1.TolerationOpExists,
@@ -450,23 +454,24 @@ func verifyFakePodCreation(ctx context.Context, virtualNodeName string) {
450454
container := fakePod.Spec.Containers[0]
451455

452456
// Expected hostPorts from all 7 pods (sorted by HostIP, Protocol, HostPort)
457+
// Note: ContainerPort now equals HostPort because fake pod uses hostNetwork=true
453458
expectedPorts := []corev1.ContainerPort{
454459
// HostIP: hostIPAny, Protocol: TCP (sorted by HostPort)
455460
{ContainerPort: 5000, HostPort: 5000, Protocol: corev1.ProtocolTCP, HostIP: hostIPAny}, // Pod6-init
456461
{ContainerPort: 5002, HostPort: 5002, Protocol: corev1.ProtocolTCP, HostIP: hostIPAny}, // Pod6-main
457462
{ContainerPort: 7000, HostPort: 7000, Protocol: corev1.ProtocolTCP, HostIP: hostIPAny}, // Pod3
458-
{ContainerPort: 80, HostPort: 8000, Protocol: corev1.ProtocolTCP, HostIP: hostIPAny}, // Pod3
459-
{ContainerPort: 81, HostPort: 8001, Protocol: corev1.ProtocolTCP, HostIP: hostIPAny}, // Pod4
460-
{ContainerPort: 80, HostPort: 8080, Protocol: corev1.ProtocolTCP, HostIP: hostIPAny}, // Pod1
461-
{ContainerPort: 81, HostPort: 8081, Protocol: corev1.ProtocolTCP, HostIP: hostIPAny}, // Pod1
463+
{ContainerPort: 8000, HostPort: 8000, Protocol: corev1.ProtocolTCP, HostIP: hostIPAny}, // Pod3
464+
{ContainerPort: 8001, HostPort: 8001, Protocol: corev1.ProtocolTCP, HostIP: hostIPAny}, // Pod4
465+
{ContainerPort: 8080, HostPort: 8080, Protocol: corev1.ProtocolTCP, HostIP: hostIPAny}, // Pod1
466+
{ContainerPort: 8081, HostPort: 8081, Protocol: corev1.ProtocolTCP, HostIP: hostIPAny}, // Pod1
462467
// HostIP: hostIPAny, Protocol: UDP (sorted by HostPort)
463468
{ContainerPort: 5001, HostPort: 5001, Protocol: corev1.ProtocolUDP, HostIP: hostIPAny}, // Pod6-init
464469
// HostIP: hostIPLocalhost, Protocol: TCP (sorted by HostPort)
465-
{ContainerPort: 7001, HostPort: 7000, Protocol: corev1.ProtocolTCP, HostIP: hostIPLocalhost}, // Pod4
470+
{ContainerPort: 7000, HostPort: 7000, Protocol: corev1.ProtocolTCP, HostIP: hostIPLocalhost}, // Pod4
466471
{ContainerPort: 9000, HostPort: 9000, Protocol: corev1.ProtocolTCP, HostIP: hostIPLocalhost}, // Pod2
467-
{ContainerPort: 9000, HostPort: 9001, Protocol: corev1.ProtocolTCP, HostIP: hostIPLocalhost}, // Pod4
472+
{ContainerPort: 9001, HostPort: 9001, Protocol: corev1.ProtocolTCP, HostIP: hostIPLocalhost}, // Pod4
468473
// HostIP: hostIPLocalhost, Protocol: UDP (sorted by HostPort)
469-
{ContainerPort: 9001, HostPort: 9000, Protocol: corev1.ProtocolUDP, HostIP: hostIPLocalhost}, // Pod2
474+
{ContainerPort: 9000, HostPort: 9000, Protocol: corev1.ProtocolUDP, HostIP: hostIPLocalhost}, // Pod2
470475
{ContainerPort: 9001, HostPort: 9001, Protocol: corev1.ProtocolUDP, HostIP: hostIPLocalhost}, // Pod3
471476
// HostIP: "" (hostNetwork), Protocol: TCP (sorted by HostPort)
472477
{ContainerPort: 4000, HostPort: 4000, Protocol: corev1.ProtocolTCP, HostIP: ""}, // Pod7-init
@@ -523,6 +528,9 @@ func verifyFakePodCreation(ctx context.Context, virtualNodeName string) {
523528
fmt.Sprintf("Port %d: Protocol mismatch", i))
524529
gomega.Expect(actualPort.HostIP).To(gomega.Equal(expectedPort.HostIP),
525530
fmt.Sprintf("Port %d: HostIP mismatch", i))
531+
// Verify ContainerPort equals HostPort (required for hostNetwork=true)
532+
gomega.Expect(actualPort.ContainerPort).To(gomega.Equal(actualPort.HostPort),
533+
fmt.Sprintf("Port %d: ContainerPort should equal HostPort for hostNetwork pod", i))
526534
}
527535

528536
ginkgo.GinkgoWriter.Printf("Verified fake pod creation with %d hostPorts\n", len(container.Ports))

0 commit comments

Comments
 (0)