github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/common/util.go (about) 1 package common 2 3 import ( 4 "strconv" 5 6 "github.com/cri-o/ocicni/pkg/ocicni" 7 "github.com/docker/go-connections/nat" 8 "github.com/pkg/errors" 9 ) 10 11 // createPortBindings iterates ports mappings and exposed ports into a format CNI understands 12 func createPortBindings(ports []string) ([]ocicni.PortMapping, error) { 13 // TODO wants someone to rewrite this code in the future 14 var portBindings []ocicni.PortMapping 15 // The conversion from []string to natBindings is temporary while mheon reworks the port 16 // deduplication code. Eventually that step will not be required. 17 _, natBindings, err := nat.ParsePortSpecs(ports) 18 if err != nil { 19 return nil, err 20 } 21 for containerPb, hostPb := range natBindings { 22 var pm ocicni.PortMapping 23 pm.ContainerPort = int32(containerPb.Int()) 24 for _, i := range hostPb { 25 var hostPort int 26 var err error 27 pm.HostIP = i.HostIP 28 if i.HostPort == "" { 29 hostPort = containerPb.Int() 30 } else { 31 hostPort, err = strconv.Atoi(i.HostPort) 32 if err != nil { 33 return nil, errors.Wrapf(err, "unable to convert host port to integer") 34 } 35 } 36 37 pm.HostPort = int32(hostPort) 38 pm.Protocol = containerPb.Proto() 39 portBindings = append(portBindings, pm) 40 } 41 } 42 return portBindings, nil 43 }