From 42e210861c1d56449feabca41d6562bec46e42a6 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Thu, 15 Aug 2024 10:42:54 +0800 Subject: [PATCH] fix(host): ignore nil host_port of portmapping error (#21025) --- pkg/hostman/guestman/portmapping.go | 5 ++++- pkg/util/netutils2/getport/getport.go | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/hostman/guestman/portmapping.go b/pkg/hostman/guestman/portmapping.go index 602af14df1..4a0c39d7ad 100644 --- a/pkg/hostman/guestman/portmapping.go +++ b/pkg/hostman/guestman/portmapping.go @@ -4,6 +4,7 @@ import ( "context" "yunion.io/x/jsonutils" + "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/sets" @@ -119,7 +120,9 @@ func (m *portMappingManager) getOtherGuestsUsedPorts(gst GuestRuntimeInstance) ( ps = sets.NewInt() } if pm.HostPort == nil { - return nil, errors.Errorf("portmap %s has nil host port", jsonutils.Marshal(pm)) + //return nil, errors.Errorf("guest (%s/%s) portmap %s has nil host port", ins.GetId(), ins.GetName(), jsonutils.Marshal(pm)) + log.Warningf("%s", errors.Errorf("guest (%s/%s) portmap %s has nil host port", ins.GetId(), ins.GetName(), jsonutils.Marshal(pm))) + continue } ps.Insert(*pm.HostPort) ret[pm.Protocol] = ps diff --git a/pkg/util/netutils2/getport/getport.go b/pkg/util/netutils2/getport/getport.go index 4237942b59..98d987830f 100644 --- a/pkg/util/netutils2/getport/getport.go +++ b/pkg/util/netutils2/getport/getport.go @@ -21,7 +21,6 @@ import ( "strconv" "strings" - "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/sets" ) @@ -119,6 +118,7 @@ func GetPortByRange(proto Protocol, start int, end int) (PortResult, error) { } func GetPortByRangeBySets(proto Protocol, start int, end int, usedPorts sets.Int) (PortResult, error) { + errs := []error{} for i := start; i <= end; i++ { rPort := rand.Intn(end-start) + start if usedPorts.Has(rPort) { @@ -127,7 +127,7 @@ func GetPortByRangeBySets(proto Protocol, start int, end int, usedPorts sets.Int result, err := getPort(proto, "", rPort) if err != nil { usedPorts.Insert(rPort) - log.Debugf("check random port %d: %v", rPort, err) + errs = append(errs, errors.Wrapf(err, "check random port: %d", rPort)) } else { return result, nil } @@ -135,7 +135,7 @@ func GetPortByRangeBySets(proto Protocol, start int, end int, usedPorts sets.Int return PortResult{ IP: "", Port: -1, - }, errors.Errorf("can't get free port in [%d, %d]", start, end) + }, errors.Wrapf(errors.NewAggregate(errs), "can't get free port in [%d, %d]", start, end) } // GetTcpPort gets a port for some random available address using either