From 51a82bd107333703ad0b4524ff1feb2e37b4e63c Mon Sep 17 00:00:00 2001 From: ChrisLiu <70144550+chrisliu1995@users.noreply.github.com> Date: Fri, 6 Jun 2025 11:49:42 +0800 Subject: [PATCH] enhance: Kubernetes-HostPort support container port same as host (#230) Signed-off-by: ChrisLiu --- cloudprovider/kubernetes/hostPort.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/cloudprovider/kubernetes/hostPort.go b/cloudprovider/kubernetes/hostPort.go index 3603118..6710eae 100644 --- a/cloudprovider/kubernetes/hostPort.go +++ b/cloudprovider/kubernetes/hostPort.go @@ -42,6 +42,7 @@ const ( //Its corresponding value format is as follows, containerName:port1/protocol1,port2/protocol2,... e.g. game-server:25565/TCP //When no protocol is specified, TCP is used by default ContainerPortsKey = "ContainerPorts" + PortSameAsHost = "SameAsHost" ) type HostPortPlugin struct { @@ -103,6 +104,10 @@ func (hpp *HostPortPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx cont if ports, ok := containerPortsMap[container.Name]; ok { containerPorts := container.Ports for i, port := range ports { + // -1 means same as host + if port == -1 { + port = hostPorts[numToAlloc-1] + } containerPort := corev1.ContainerPort{ ContainerPort: port, HostPort: hostPorts[numToAlloc-1], @@ -338,9 +343,15 @@ func parseConfig(conf []gamekruiseiov1alpha1.NetworkConfParams, pod *corev1.Pod) for _, portString := range strings.Split(cpSlice[1], ",") { ppSlice := strings.Split(portString, "/") // handle port - port, err := strconv.ParseInt(ppSlice[0], 10, 32) - if err != nil { - continue + var port int64 + var err error + if ppSlice[0] == PortSameAsHost { + port = -1 + } else { + port, err = strconv.ParseInt(ppSlice[0], 10, 32) + if err != nil { + continue + } } numToAlloc++ ports = append(ports, int32(port))