From ed711804fe07413a09cd0dd9294cc949d0be081c Mon Sep 17 00:00:00 2001 From: Tomas Kral Date: Mon, 19 Sep 2016 14:19:42 +0200 Subject: [PATCH 1/2] Add port protocol handing for docker-compose. --- pkg/loader/compose/compose.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/loader/compose/compose.go b/pkg/loader/compose/compose.go index eb8191b77..3fedb679a 100644 --- a/pkg/loader/compose/compose.go +++ b/pkg/loader/compose/compose.go @@ -52,15 +52,26 @@ func loadPorts(composePorts []string) ([]kobject.Ports, error) { ports := []kobject.Ports{} character := ":" for _, port := range composePorts { - p := api.ProtocolTCP - if strings.Contains(port, character) { - hostPort := port[0:strings.Index(port, character)] + proto := api.ProtocolTCP + // get protocol + 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) hostPortInt, err := strconv.Atoi(hostPort) if err != nil { 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) containerPortInt, err := strconv.Atoi(containerPort) if err != nil { @@ -69,16 +80,16 @@ func loadPorts(composePorts []string) ([]kobject.Ports, error) { ports = append(ports, kobject.Ports{ HostPort: int32(hostPortInt), ContainerPort: int32(containerPortInt), - Protocol: p, + Protocol: proto, }) } else { - containerPortInt, err := strconv.Atoi(port) + containerPortInt, err := strconv.Atoi(portNoProto) if err != nil { return nil, fmt.Errorf("invalid container port %q", port) } ports = append(ports, kobject.Ports{ ContainerPort: int32(containerPortInt), - Protocol: p, + Protocol: proto, }) } From 371af623afe1567e0611e3bfe9589203dc3d3454 Mon Sep 17 00:00:00 2001 From: Tomas Kral Date: Mon, 19 Sep 2016 14:33:19 +0200 Subject: [PATCH 2/2] Add test for ports with protocols. --- script/test/cmd/tests.sh | 8 + .../ports-with-proto/docker-compose.yml | 20 +++ .../fixtures/ports-with-proto/output-k8s.json | 145 +++++++++++++++ .../fixtures/ports-with-proto/output-os.json | 165 ++++++++++++++++++ 4 files changed, 338 insertions(+) create mode 100644 script/test/fixtures/ports-with-proto/docker-compose.yml create mode 100644 script/test/fixtures/ports-with-proto/output-k8s.json create mode 100644 script/test/fixtures/ports-with-proto/output-os.json diff --git a/script/test/cmd/tests.sh b/script/test/cmd/tests.sh index e8fdb273a..9723a83c9 100755 --- a/script/test/cmd/tests.sh +++ b/script/test/cmd/tests.sh @@ -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." +###### +# 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 diff --git a/script/test/fixtures/ports-with-proto/docker-compose.yml b/script/test/fixtures/ports-with-proto/docker-compose.yml new file mode 100644 index 000000000..c508f86d1 --- /dev/null +++ b/script/test/fixtures/ports-with-proto/docker-compose.yml @@ -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" + diff --git a/script/test/fixtures/ports-with-proto/output-k8s.json b/script/test/fixtures/ports-with-proto/output-k8s.json new file mode 100644 index 000000000..a766bc1b1 --- /dev/null +++ b/script/test/fixtures/ports-with-proto/output-k8s.json @@ -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": {} + } + ] +} diff --git a/script/test/fixtures/ports-with-proto/output-os.json b/script/test/fixtures/ports-with-proto/output-os.json new file mode 100644 index 000000000..a5d78d3b4 --- /dev/null +++ b/script/test/fixtures/ports-with-proto/output-os.json @@ -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": {} + } + ] +}