Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add port protocol handing for docker-compose. #159

Merged
merged 2 commits into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions pkg/loader/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
})
}

Expand Down
8 changes: 8 additions & 0 deletions script/test/cmd/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

20 changes: 20 additions & 0 deletions script/test/fixtures/ports-with-proto/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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"

145 changes: 145 additions & 0 deletions script/test/fixtures/ports-with-proto/output-k8s.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
]
}
165 changes: 165 additions & 0 deletions script/test/fixtures/ports-with-proto/output-os.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
]
}