Add support for host:port:port

This adds support for supplying for example:
"127.0.0.1:80:80/tcp" to docker-compose.yaml and converting it to it's
corresponding Kubernetes / OpenShift hostIP.

This commit also refactors the loadPorts function of compose.go

Closes https://github.com/kubernetes-incubator/kompose/issues/335
This commit is contained in:
Charlie Drage
2017-02-09 12:21:17 -05:00
parent be042c7e1f
commit 438088f37d
7 changed files with 287 additions and 20 deletions
+66 -20
View File
@@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
@@ -18,6 +18,7 @@ package compose
import (
"fmt"
"net"
"os"
"path/filepath"
"reflect"
@@ -180,41 +181,86 @@ func loadEnvVars(envars []string) []kobject.EnvVar {
func loadPorts(composePorts []string) ([]kobject.Ports, error) {
ports := []kobject.Ports{}
character := ":"
// For each port listed
for _, port := range composePorts {
// Get the TCP / UDP protocol. Checks to see if it splits in 2 with '/' character.
// ex. 15000:15000/tcp
// else, set a default protocol of using TCP
proto := api.ProtocolTCP
// get protocol
p := strings.Split(port, "/")
if len(p) == 2 {
if strings.EqualFold("tcp", p[1]) {
protocolCheck := strings.Split(port, "/")
if len(protocolCheck) == 2 {
if strings.EqualFold("tcp", protocolCheck[1]) {
proto = api.ProtocolTCP
} else if strings.EqualFold("udp", p[1]) {
} else if strings.EqualFold("udp", protocolCheck[1]) {
proto = api.ProtocolUDP
} else {
return nil, fmt.Errorf("invalid protocol %q", protocolCheck[1])
}
}
// port mappings without protocol part
portNoProto := p[0]
if strings.Contains(portNoProto, character) {
hostPort := portNoProto[0:strings.Index(portNoProto, character)]
hostPort = strings.TrimSpace(hostPort)
hostPortInt, err := strconv.Atoi(hostPort)
if err != nil {
return nil, fmt.Errorf("invalid host port %q", port)
// Split up the ports / IP without the "/tcp" or "/udp" appended to it
justPorts := strings.Split(protocolCheck[0], character)
if len(justPorts) == 3 {
// ex. 127.0.0.1:80:80
// Get the IP address
hostIP := justPorts[0]
ip := net.ParseIP(hostIP)
if ip.To4() == nil && ip.To16() == nil {
return nil, fmt.Errorf("%q contains an invalid IPv4 or IPv6 IP address", port)
}
containerPort := portNoProto[strings.Index(portNoProto, character)+1:]
containerPort = strings.TrimSpace(containerPort)
containerPortInt, err := strconv.Atoi(containerPort)
// Get the host port
hostPortInt, err := strconv.Atoi(justPorts[1])
if err != nil {
return nil, fmt.Errorf("invalid container port %q", port)
return nil, fmt.Errorf("invalid host port %q valid example: 127.0.0.1:80:80", port)
}
// Get the container port
containerPortInt, err := strconv.Atoi(justPorts[2])
if err != nil {
return nil, fmt.Errorf("invalid container port %q valid example: 127.0.0.1:80:80", port)
}
// Convert to a kobject struct with ports as well as IP
ports = append(ports, kobject.Ports{
HostPort: int32(hostPortInt),
ContainerPort: int32(containerPortInt),
HostIP: hostIP,
Protocol: proto,
})
} else if len(justPorts) == 2 {
// ex. 80:80
// Get the host port
hostPortInt, err := strconv.Atoi(justPorts[0])
if err != nil {
return nil, fmt.Errorf("invalid host port %q valid example: 80:80", port)
}
// Get the container port
containerPortInt, err := strconv.Atoi(justPorts[1])
if err != nil {
return nil, fmt.Errorf("invalid container port %q valid example: 80:80", port)
}
// Convert to a kobject struct and add to the list of ports
ports = append(ports, kobject.Ports{
HostPort: int32(hostPortInt),
ContainerPort: int32(containerPortInt),
Protocol: proto,
})
} else {
containerPortInt, err := strconv.Atoi(portNoProto)
// ex. 80
containerPortInt, err := strconv.Atoi(justPorts[0])
if err != nil {
return nil, fmt.Errorf("invalid container port %q", port)
return nil, fmt.Errorf("invalid container port %q valid example: 80", port)
}
ports = append(ports, kobject.Ports{
ContainerPort: int32(containerPortInt),
+50
View File
@@ -23,6 +23,7 @@ import (
"testing"
"github.com/kubernetes-incubator/kompose/pkg/kobject"
"k8s.io/kubernetes/pkg/api"
"github.com/docker/libcompose/config"
"github.com/docker/libcompose/project"
@@ -53,6 +54,55 @@ func TestHandleServiceType(t *testing.T) {
}
}
// Test loading of ports
func TestLoadPorts(t *testing.T) {
port1 := []string{"127.0.0.1:80:80/tcp"}
result1 := kobject.Ports{
HostIP: "127.0.0.1",
HostPort: 80,
ContainerPort: 80,
Protocol: api.ProtocolTCP,
}
port2 := []string{"80:80/tcp"}
result2 := kobject.Ports{
HostPort: 80,
ContainerPort: 80,
Protocol: api.ProtocolTCP,
}
port3 := []string{"80:80"}
result3 := kobject.Ports{
HostPort: 80,
ContainerPort: 80,
Protocol: api.ProtocolTCP,
}
port4 := []string{"80"}
result4 := kobject.Ports{
HostPort: 0,
ContainerPort: 80,
Protocol: api.ProtocolTCP,
}
tests := []struct {
ports []string
result kobject.Ports
}{
{port1, result1},
{port2, result2},
{port3, result3},
{port4, result4},
}
for _, tt := range tests {
result, err := loadPorts(tt.ports)
if err != nil {
t.Errorf("Unexpected error with loading ports %v", err)
}
if result[0] != tt.result {
t.Errorf("Expected %q, got %q", tt.result, result[0])
}
}
}
func TestLoadEnvVar(t *testing.T) {
ev1 := []string{"foo=bar"}
rs1 := kobject.EnvVar{