From 762153cdb35317deed19737df2af9d5b11adc051 Mon Sep 17 00:00:00 2001 From: Rogger Vasquez Date: Thu, 12 May 2022 16:41:42 -0500 Subject: [PATCH] rpk: avoid duplicates in container port pool --- src/go/rpk/pkg/net/interfaces.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/go/rpk/pkg/net/interfaces.go b/src/go/rpk/pkg/net/interfaces.go index c3f64041c2a1..41bc185918c5 100644 --- a/src/go/rpk/pkg/net/interfaces.go +++ b/src/go/rpk/pkg/net/interfaces.go @@ -59,13 +59,17 @@ func getFreePort() (uint, error) { } func GetFreePortPool(n int) ([]uint, error) { - var ports []uint - for i := 0; i < n; i++ { + m := make(map[uint]struct{}) + for len(m) != n { p, err := getFreePort() if err != nil { return nil, err } - ports = append(ports, p) + m[p] = struct{}{} + } + var ports []uint + for port := range m { + ports = append(ports, port) } return ports, nil }