feat(Kubernetes-HostPort): support TCPUDP protocol (#244)

Signed-off-by: roc <roc@imroc.cc>
This commit is contained in:
roc 2025-06-16 23:47:42 +08:00 committed by GitHub
parent 9a04f87f5e
commit a6ed1d95c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 12 deletions

View File

@ -18,6 +18,11 @@ package kubernetes
import (
"context"
"net"
"strconv"
"strings"
"sync"
gamekruiseiov1alpha1 "github.com/openkruise/kruise-game/apis/v1alpha1"
"github.com/openkruise/kruise-game/cloudprovider"
"github.com/openkruise/kruise-game/cloudprovider/errors"
@ -29,20 +34,17 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
log "k8s.io/klog/v2"
"net"
"sigs.k8s.io/controller-runtime/pkg/client"
"strconv"
"strings"
"sync"
)
const (
HostPortNetwork = "Kubernetes-HostPort"
//ContainerPortsKey represents the configuration key when using hostPort.
//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 represents the configuration key when using hostPort.
// 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"
ProtocolTCPUDP = "TCPUDP"
)
type HostPortPlugin struct {
@ -108,12 +110,26 @@ func (hpp *HostPortPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx cont
if port == -1 {
port = hostPorts[numToAlloc-1]
}
containerPort := corev1.ContainerPort{
ContainerPort: port,
HostPort: hostPorts[numToAlloc-1],
Protocol: containerProtocolsMap[container.Name][i],
protocol := containerProtocolsMap[container.Name][i]
hostPort := hostPorts[numToAlloc-1]
if protocol == ProtocolTCPUDP {
containerPorts = append(containerPorts,
corev1.ContainerPort{
ContainerPort: port,
HostPort: hostPort,
Protocol: corev1.ProtocolTCP,
}, corev1.ContainerPort{
ContainerPort: port,
HostPort: hostPort,
Protocol: corev1.ProtocolUDP,
})
} else {
containerPorts = append(containerPorts, corev1.ContainerPort{
ContainerPort: port,
HostPort: hostPort,
Protocol: protocol,
})
}
containerPorts = append(containerPorts, containerPort)
numToAlloc--
}
containers[cIndex].Ports = containerPorts