Fix the problem that the hostPort plugin repeatedly allocates ports when pods with the same name are created (#66)

* update docs for network

Signed-off-by: ChrisLiu <chrisliu1995@163.com>

* update doc of gs scaling

Signed-off-by: ChrisLiu <chrisliu1995@163.com>

* Fix: the hostPort plugin repeatedly deallocates ports

Signed-off-by: ChrisLiu <chrisliu1995@163.com>

---------

Signed-off-by: ChrisLiu <chrisliu1995@163.com>
This commit is contained in:
ChrisLiu 2023-06-19 10:51:12 +08:00 committed by GitHub
parent 1192d4dc62
commit b9af47ba84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 1 deletions

View File

@ -24,6 +24,7 @@ import (
provideroptions "github.com/openkruise/kruise-game/cloudprovider/options" provideroptions "github.com/openkruise/kruise-game/cloudprovider/options"
"github.com/openkruise/kruise-game/cloudprovider/utils" "github.com/openkruise/kruise-game/cloudprovider/utils"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
log "k8s.io/klog/v2" log "k8s.io/klog/v2"
@ -68,6 +69,19 @@ func (hpp *HostPortPlugin) Alias() string {
} }
func (hpp *HostPortPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, errors.PluginError) { func (hpp *HostPortPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx context.Context) (*corev1.Pod, errors.PluginError) {
podNow := &corev1.Pod{}
err := c.Get(ctx, types.NamespacedName{
Namespace: pod.GetNamespace(),
Name: pod.GetName(),
}, podNow)
// There is a pod with same ns/name exists in cluster, do not allocate
if err == nil {
return pod, nil
}
if !k8serrors.IsNotFound(err) {
return pod, errors.NewPluginError(errors.ApiCallError, err.Error())
}
if _, ok := hpp.isAllocated[pod.GetNamespace()+"/"+pod.GetName()]; ok { if _, ok := hpp.isAllocated[pod.GetNamespace()+"/"+pod.GetName()]; ok {
return pod, nil return pod, nil
} }
@ -78,7 +92,7 @@ func (hpp *HostPortPlugin) OnPodAdded(c client.Client, pod *corev1.Pod, ctx cont
containerPortsMap, containerProtocolsMap, numToAlloc := parseConfig(conf, pod) containerPortsMap, containerProtocolsMap, numToAlloc := parseConfig(conf, pod)
hostPorts := hpp.allocate(numToAlloc, pod.GetNamespace()+"/"+pod.GetName()) hostPorts := hpp.allocate(numToAlloc, pod.GetNamespace()+"/"+pod.GetName())
log.V(5).Infof("pod %s/%s allocated hostPorts %v", pod.GetNamespace(), pod.GetName(), hostPorts) log.Infof("pod %s/%s allocated hostPorts %v", pod.GetNamespace(), pod.GetName(), hostPorts)
// patch pod container ports // patch pod container ports
containers := pod.Spec.Containers containers := pod.Spec.Containers
@ -173,6 +187,7 @@ func (hpp *HostPortPlugin) OnPodDeleted(c client.Client, pod *corev1.Pod, ctx co
} }
hpp.deAllocate(hostPorts, pod.GetNamespace()+"/"+pod.GetName()) hpp.deAllocate(hostPorts, pod.GetNamespace()+"/"+pod.GetName())
log.Infof("pod %s/%s deallocated hostPorts %v", pod.GetNamespace(), pod.GetName(), hostPorts)
return nil return nil
} }