diff --git a/src/cmd/linuxkit/run_qemu.go b/src/cmd/linuxkit/run_qemu.go index 8dec12943..e1a6cd4ab 100644 --- a/src/cmd/linuxkit/run_qemu.go +++ b/src/cmd/linuxkit/run_qemu.go @@ -620,12 +620,6 @@ func discoverBackend(config QemuConfig) QemuConfig { type multipleFlag []string -type publishedPorts struct { - guest int - host int - protocol string -} - func (f *multipleFlag) String() string { return "A multiple flag is a type of flag that can be repeated any number of times" } @@ -635,57 +629,13 @@ func (f *multipleFlag) Set(value string) error { return nil } -func splitPublish(publish string) (publishedPorts, error) { - p := publishedPorts{} - slice := strings.Split(publish, ":") - - if len(slice) < 2 { - return p, fmt.Errorf("Unable to parse the ports to be published, should be in format : or :/") - } - - hostPort, err := strconv.Atoi(slice[0]) - - if err != nil { - return p, fmt.Errorf("The provided hostPort can't be converted to int") - } - - right := strings.Split(slice[1], "/") - - protocol := "tcp" - if len(right) == 2 { - protocol = strings.TrimSpace(strings.ToLower(right[1])) - } - - if protocol != "tcp" && protocol != "udp" { - return p, fmt.Errorf("Provided protocol is not valid, valid options are: udp and tcp") - } - guestPort, err := strconv.Atoi(right[0]) - - if err != nil { - return p, fmt.Errorf("The provided guestPort can't be converted to int") - } - - if hostPort < 1 || hostPort > 65535 { - return p, fmt.Errorf("Invalid hostPort: %d", hostPort) - } - - if guestPort < 1 || guestPort > 65535 { - return p, fmt.Errorf("Invalid guestPort: %d", guestPort) - } - - p.guest = guestPort - p.host = hostPort - p.protocol = protocol - return p, nil -} - func buildQemuForwardings(publishFlags multipleFlag, containerized bool) (string, error) { if len(publishFlags) == 0 { return "", nil } var forwardings string for _, publish := range publishFlags { - p, err := splitPublish(publish) + p, err := NewPublishedPort(publish) if err != nil { return "", err } @@ -705,7 +655,7 @@ func buildQemuForwardings(publishFlags multipleFlag, containerized bool) (string func buildDockerForwardings(publishedPorts []string) ([]string, error) { pmap := []string{} for _, port := range publishedPorts { - s, err := splitPublish(port) + s, err := NewPublishedPort(port) if err != nil { return nil, err } diff --git a/src/cmd/linuxkit/util.go b/src/cmd/linuxkit/util.go index 39d794cb1..1f02fa622 100644 --- a/src/cmd/linuxkit/util.go +++ b/src/cmd/linuxkit/util.go @@ -191,3 +191,55 @@ func (l *Disks) Set(value string) error { *l = append(*l, d) return nil } + +// PublishedPort is used by some backends to expose a VMs port on the host +type PublishedPort struct { + guest int + host int + protocol string +} + +// NewPublishedPort parses a string of the form :[/] and returns a PublishedPort structure +func NewPublishedPort(publish string) (PublishedPort, error) { + p := PublishedPort{} + slice := strings.Split(publish, ":") + + if len(slice) < 2 { + return p, fmt.Errorf("Unable to parse the ports to be published, should be in format : or :/") + } + + hostPort, err := strconv.Atoi(slice[0]) + + if err != nil { + return p, fmt.Errorf("The provided hostPort can't be converted to int") + } + + right := strings.Split(slice[1], "/") + + protocol := "tcp" + if len(right) == 2 { + protocol = strings.TrimSpace(strings.ToLower(right[1])) + } + + if protocol != "tcp" && protocol != "udp" { + return p, fmt.Errorf("Provided protocol is not valid, valid options are: udp and tcp") + } + guestPort, err := strconv.Atoi(right[0]) + + if err != nil { + return p, fmt.Errorf("The provided guestPort can't be converted to int") + } + + if hostPort < 1 || hostPort > 65535 { + return p, fmt.Errorf("Invalid hostPort: %d", hostPort) + } + + if guestPort < 1 || guestPort > 65535 { + return p, fmt.Errorf("Invalid guestPort: %d", guestPort) + } + + p.guest = guestPort + p.host = hostPort + p.protocol = protocol + return p, nil +}