Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #229 from cjellick/ip-binding
Browse files Browse the repository at this point in the history
Support binding to specific IP
  • Loading branch information
Craig Jellick committed Mar 18, 2016
2 parents 82e739a + 11e4380 commit 193e660
Show file tree
Hide file tree
Showing 26 changed files with 145 additions and 108 deletions.
45 changes: 30 additions & 15 deletions cattle/plugins/docker/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,19 @@ def _setup_ports(create_config, instance, start_config):
ports.append((port.privatePort, port.protocol))
if port.publicPort is not None:
bind = '{0}/{1}'.format(port.privatePort, port.protocol)
bindings[bind] = ('', port.publicPort)
bind_addr = ''
try:
if port.data.fields['bindAddr'] is not None:
bind_addr = port.data.fields['bindAddr']
except (AttributeError, KeyError):
pass

host_bind = (bind_addr, port.publicPort)
if bind not in bindings:
bindings[bind] = [host_bind]
else:
bindings[bind].append(host_bind)

except (AttributeError, KeyError):
pass

Expand Down Expand Up @@ -727,7 +739,7 @@ def _get_instance_host_map_data(self, obj):
inspect = None
docker_mounts = None
existing = self.get_container(client, obj.instance)
docker_ports = {}
docker_ports = []
docker_ip = None

if existing is not None:
Expand All @@ -736,19 +748,22 @@ def _get_instance_host_map_data(self, obj):
docker_ip = inspect['NetworkSettings']['IPAddress']
if existing.get('Ports') is not None:
for port in existing['Ports']:
if 'PublicPort' in port and 'PrivatePort' not in port:
# Remove after docker 0.12/1.0 is released
private_port = '{0}/{1}'.format(port['PublicPort'],
port['Type'])
docker_ports[private_port] = None
elif 'PublicPort' in port:
private_port = '{0}/{1}'.format(port['PrivatePort'],
port['Type'])
docker_ports[private_port] = str(port['PublicPort'])
else:
private_port = '{0}/{1}'.format(port['PrivatePort'],
port['Type'])
docker_ports[private_port] = None
private_port = '{0}/{1}'.format(port['PrivatePort'],
port['Type'])
port_spec = private_port

bind_addr = ''
if 'IP' in port:
bind_addr = '%s:' % port['IP']

public_port = ''
if 'PublicPort' in port:
public_port = '%s:' % port['PublicPort']
elif 'IP' in port:
public_port = ':'

port_spec = bind_addr + public_port + port_spec
docker_ports.append(port_spec)

update = {
'instance': {
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_agent_instance_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_bridge_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_command_args_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "sleep 1 2 3",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_command_null_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_command_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "sleep 5",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_double_links_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
6 changes: 3 additions & 3 deletions tests/docker/instance_activate_fields_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": null
}
"dockerPorts": [
"8080/tcp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_ipsec_lb_agent_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
12 changes: 6 additions & 6 deletions tests/docker/instance_activate_ipsec_network_agent_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"500/udp": "1500",
"4500/udp": "5500",
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1500:500/udp",
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp",
"0.0.0.0:5500:4500/udp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
12 changes: 6 additions & 6 deletions tests/docker/instance_activate_ipsec_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"500/udp": "1500",
"4500/udp": "5500",
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1500:500/udp",
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp",
"0.0.0.0:5500:4500/udp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_labels_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "sleep 5",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_links_no_service_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_links_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
15 changes: 15 additions & 0 deletions tests/docker/instance_activate_ports
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@
"protocol": "udp",
"privatePort": 12201,
"publicPort": null
},
{
"protocol": "tcp",
"privatePort": 6666,
"publicPort": 7777,
"data": {
"fields": {
"bindAddr": "127.0.0.1"
}
}
},
{
"protocol": "tcp",
"privatePort": 6666,
"publicPort": 8888
}
],
"description": null,
Expand Down
16 changes: 6 additions & 10 deletions tests/docker/instance_activate_ports_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": null,
"12201/udp": null
}
"dockerPorts": [
"0.0.0.0:8888:6666/tcp",
"12201/udp",
"127.0.0.1:7777:6666/tcp",
"8080/tcp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand All @@ -17,12 +19,6 @@
"Names": [
"/r-test"
],
"Ports": [
{
"Type": "tcp",
"PrivatePort": 8080
}
],
"Labels": {
"io.rancher.container.uuid": "c861f990-4472-4fa1-960f-65171b544c28",
"io.rancher.container.name": "test",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_pull_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/instance_activate_volumes_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {
"8080/tcp": "1234",
"12201/udp": "5678"
}
"dockerPorts": [
"0.0.0.0:1234:12201/udp",
"0.0.0.0:1234:8080/tcp"
]
},
"dockerContainer": {
"Command": "sleep 5",
Expand Down
2 changes: 1 addition & 1 deletion tests/docker/instance_deactivate_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {}
"dockerPorts": []
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
2 changes: 1 addition & 1 deletion tests/docker/native_container_activate_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {"8080/tcp": null}
"dockerPorts": ["8080/tcp"]
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
2 changes: 1 addition & 1 deletion tests/docker/native_container_deactivate_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {}
"dockerPorts": []
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
2 changes: 1 addition & 1 deletion tests/docker/native_container_not_running_resp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"+data": {
"+fields": {
"dockerHostIp": "1.2.3.4",
"dockerPorts": {}
"dockerPorts": []
},
"dockerContainer": {
"Command": "/sleep.sh",
Expand Down
Loading

0 comments on commit 193e660

Please sign in to comment.