Merge pull request #159 from kadel/fix-port-parsing

Add port protocol handing for docker-compose.
This commit is contained in:
Tomas Kral 2016-09-22 09:34:21 +02:00 committed by GitHub
commit fa46376dff
5 changed files with 356 additions and 7 deletions

View File

@ -52,15 +52,26 @@ func loadPorts(composePorts []string) ([]kobject.Ports, error) {
ports := []kobject.Ports{} ports := []kobject.Ports{}
character := ":" character := ":"
for _, port := range composePorts { for _, port := range composePorts {
p := api.ProtocolTCP proto := api.ProtocolTCP
if strings.Contains(port, character) { // get protocol
hostPort := port[0:strings.Index(port, character)] p := strings.Split(port, "/")
if len(p) == 2 {
if strings.EqualFold("tcp", p[1]) {
proto = api.ProtocolTCP
} else if strings.EqualFold("udp", p[1]) {
proto = api.ProtocolUDP
}
}
// port mappings without protocol part
portNoProto := p[0]
if strings.Contains(portNoProto, character) {
hostPort := portNoProto[0:strings.Index(portNoProto, character)]
hostPort = strings.TrimSpace(hostPort) hostPort = strings.TrimSpace(hostPort)
hostPortInt, err := strconv.Atoi(hostPort) hostPortInt, err := strconv.Atoi(hostPort)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid host port %q", port) return nil, fmt.Errorf("invalid host port %q", port)
} }
containerPort := port[strings.Index(port, character)+1:] containerPort := portNoProto[strings.Index(portNoProto, character)+1:]
containerPort = strings.TrimSpace(containerPort) containerPort = strings.TrimSpace(containerPort)
containerPortInt, err := strconv.Atoi(containerPort) containerPortInt, err := strconv.Atoi(containerPort)
if err != nil { if err != nil {
@ -69,16 +80,16 @@ func loadPorts(composePorts []string) ([]kobject.Ports, error) {
ports = append(ports, kobject.Ports{ ports = append(ports, kobject.Ports{
HostPort: int32(hostPortInt), HostPort: int32(hostPortInt),
ContainerPort: int32(containerPortInt), ContainerPort: int32(containerPortInt),
Protocol: p, Protocol: proto,
}) })
} else { } else {
containerPortInt, err := strconv.Atoi(port) containerPortInt, err := strconv.Atoi(portNoProto)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid container port %q", port) return nil, fmt.Errorf("invalid container port %q", port)
} }
ports = append(ports, kobject.Ports{ ports = append(ports, kobject.Ports{
ContainerPort: int32(containerPortInt), ContainerPort: int32(containerPortInt),
Protocol: p, Protocol: proto,
}) })
} }

View File

@ -43,5 +43,13 @@ convert::expect_success_and_warning "kompose -f $KOMPOSE_ROOT/script/test/fixtur
convert::expect_success_and_warning "kompose -f $KOMPOSE_ROOT/script/test/fixtures/entrypoint-command/docker-compose.yml convert --stdout --dc" "$KOMPOSE_ROOT/script/test/fixtures/entrypoint-command/output-os.json" "Service cannot be created because of missing port." convert::expect_success_and_warning "kompose -f $KOMPOSE_ROOT/script/test/fixtures/entrypoint-command/docker-compose.yml convert --stdout --dc" "$KOMPOSE_ROOT/script/test/fixtures/entrypoint-command/output-os.json" "Service cannot be created because of missing port."
######
# Tests related to docker-compose file in /script/test/fixtures/ports-with-proto
# kubernetes test
convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/ports-with-proto/docker-compose.yml convert --stdout" "$KOMPOSE_ROOT/script/test/fixtures/ports-with-proto/output-k8s.json"
# openshift test
convert::expect_success "kompose -f $KOMPOSE_ROOT/script/test/fixtures/ports-with-proto/docker-compose.yml convert --stdout --dc" "$KOMPOSE_ROOT/script/test/fixtures/ports-with-proto/output-os.json"
exit $EXIT_STATUS exit $EXIT_STATUS

View File

@ -0,0 +1,20 @@
version: "2"
services:
web:
image: tuna/docker-counter23
ports:
- "5000:5000/tcp"
links:
- redis
networks:
- default
redis:
image: redis:3.0
networks:
- default
ports:
- "6379/tcp"
- "1234:1235/udp"

View File

@ -0,0 +1,145 @@
{
"kind": "List",
"apiVersion": "v1",
"metadata": {},
"items": [
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "web",
"creationTimestamp": null,
"labels": {
"service": "web"
}
},
"spec": {
"ports": [
{
"name": "5000",
"protocol": "TCP",
"port": 5000,
"targetPort": 5000
}
],
"selector": {
"service": "web"
}
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "redis",
"creationTimestamp": null,
"labels": {
"service": "redis"
}
},
"spec": {
"ports": [
{
"name": "6379",
"protocol": "TCP",
"port": 6379,
"targetPort": 6379
},
{
"name": "1234",
"protocol": "UDP",
"port": 1234,
"targetPort": 1235
}
],
"selector": {
"service": "redis"
}
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "web",
"creationTimestamp": null
},
"spec": {
"replicas": 1,
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"service": "web"
}
},
"spec": {
"containers": [
{
"name": "web",
"image": "tuna/docker-counter23",
"ports": [
{
"containerPort": 5000,
"protocol": "TCP"
}
],
"resources": {}
}
],
"restartPolicy": "Always"
}
},
"strategy": {}
},
"status": {}
},
{
"kind": "Deployment",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "redis",
"creationTimestamp": null
},
"spec": {
"replicas": 1,
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"service": "redis"
}
},
"spec": {
"containers": [
{
"name": "redis",
"image": "redis:3.0",
"ports": [
{
"containerPort": 6379,
"protocol": "TCP"
},
{
"containerPort": 1235,
"protocol": "UDP"
}
],
"resources": {}
}
],
"restartPolicy": "Always"
}
},
"strategy": {}
},
"status": {}
}
]
}

View File

@ -0,0 +1,165 @@
{
"kind": "List",
"apiVersion": "v1",
"metadata": {},
"items": [
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "redis",
"creationTimestamp": null,
"labels": {
"service": "redis"
}
},
"spec": {
"ports": [
{
"name": "6379",
"protocol": "TCP",
"port": 6379,
"targetPort": 6379
},
{
"name": "1234",
"protocol": "UDP",
"port": 1234,
"targetPort": 1235
}
],
"selector": {
"service": "redis"
}
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "web",
"creationTimestamp": null,
"labels": {
"service": "web"
}
},
"spec": {
"ports": [
{
"name": "5000",
"protocol": "TCP",
"port": 5000,
"targetPort": 5000
}
],
"selector": {
"service": "web"
}
},
"status": {
"loadBalancer": {}
}
},
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "redis",
"creationTimestamp": null,
"labels": {
"service": "redis"
}
},
"spec": {
"strategy": {
"resources": {}
},
"triggers": null,
"replicas": 1,
"test": false,
"selector": {
"service": "redis"
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"service": "redis"
}
},
"spec": {
"containers": [
{
"name": "redis",
"image": "redis:3.0",
"ports": [
{
"containerPort": 6379,
"protocol": "TCP"
},
{
"containerPort": 1235,
"protocol": "UDP"
}
],
"resources": {}
}
],
"restartPolicy": "Always"
}
}
},
"status": {}
},
{
"kind": "DeploymentConfig",
"apiVersion": "v1",
"metadata": {
"name": "web",
"creationTimestamp": null,
"labels": {
"service": "web"
}
},
"spec": {
"strategy": {
"resources": {}
},
"triggers": null,
"replicas": 1,
"test": false,
"selector": {
"service": "web"
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"service": "web"
}
},
"spec": {
"containers": [
{
"name": "web",
"image": "tuna/docker-counter23",
"ports": [
{
"containerPort": 5000,
"protocol": "TCP"
}
],
"resources": {}
}
],
"restartPolicy": "Always"
}
}
},
"status": {}
}
]
}