From ca4b6b051d530a8444578b470ac900fc5b2c4126 Mon Sep 17 00:00:00 2001 From: shixuanqing <1356292400@qq.com> Date: Tue, 12 Sep 2023 04:29:51 +0000 Subject: [PATCH] runtime: Naming conflict of network devices When creating a new endpoint, we check existing endpoint names and automatically adjust the naming of the new endpoint to ensure uniqueness. Fixes: #7876 Signed-off-by: shixuanqing <1356292400@qq.com> --- src/runtime/virtcontainers/network_linux.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/runtime/virtcontainers/network_linux.go b/src/runtime/virtcontainers/network_linux.go index 30819bb30b..9ef4373015 100644 --- a/src/runtime/virtcontainers/network_linux.go +++ b/src/runtime/virtcontainers/network_linux.go @@ -12,8 +12,10 @@ import ( "net" "os" "os/exec" + "regexp" "runtime" "sort" + "strconv" "time" "github.com/containernetworking/plugins/pkg/ns" @@ -128,6 +130,22 @@ func (n *LinuxNetwork) addSingleEndpoint(ctx context.Context, s *Sandbox, netInf var socketPath string idx := len(n.eps) + // Avoid endpoint naming conflicts + // When creating a new endpoint, we check existing endpoint names and automatically adjust the naming of the new endpoint to ensure uniqueness. + lastIdx := -1 + if len(n.eps) > 0 { + lastEndpoint := n.eps[len(n.eps)-1] + re := regexp.MustCompile("[0-9]+") + matchStr := re.FindString(lastEndpoint.Name()) + n, err := strconv.ParseInt(matchStr, 10, 64) + if err != nil { + return nil, err + } + lastIdx = int(n) + } + if idx <= lastIdx { + idx = lastIdx + 1 + } // Check if this is a dummy interface which has a vhost-user socket associated with it socketPath, err = vhostUserSocketPath(netInfo) if err != nil {