From d996fe85fef47ff61652000c2021956f8ff9f46c Mon Sep 17 00:00:00 2001 From: Jeffrey Morgan Date: Tue, 8 Sep 2015 18:34:53 -0700 Subject: [PATCH] Fixed small bug where port collision would not be detected between containers. Signed-off-by: Jeffrey Morgan --- .../ContainerSettingsPorts.react.js | 38 ++++++++++--------- src/utils/ContainerUtil.js | 19 ---------- 2 files changed, 20 insertions(+), 37 deletions(-) diff --git a/src/components/ContainerSettingsPorts.react.js b/src/components/ContainerSettingsPorts.react.js index 5b3e2576f1..54a5409cfd 100644 --- a/src/components/ContainerSettingsPorts.react.js +++ b/src/components/ContainerSettingsPorts.react.js @@ -23,8 +23,8 @@ var ContainerSettingsPorts = React.createClass({ shell.openExternal(url); }, handleChangePort: function(key, e) { - var ports = this.state.ports; - var port = e.target.value; + let ports = this.state.ports; + let port = e.target.value; // save updated port ports[key] = _.extend(ports[key], { @@ -35,29 +35,31 @@ var ContainerSettingsPorts = React.createClass({ // basic validation, if number is integer, if its in range, if there // is no collision with ports of other containers and also if there is no - // collision with ports for current containser - const name = this.props.container.Name; - const containers = containerStore.getState().containers; - const container = ContainerUtil.isPortCollision(name, containers, port); + // collision with ports for current container + const otherContainers = _.filter(_.values(containerStore.getState().containers), c => c.Name !== this.props.container.Name); + const otherPorts = _.flatten(otherContainers.map(container => { + return _.values(container.NetworkSettings.Ports).map(hosts => hosts.map(host => {return {port: host.HostPort, name: container.Name}})); + })).reduce((prev, pair) => { + prev[pair.port] = pair.name; + return prev; + }, {}); + const duplicates = _.filter(ports, (v, i) => { return (i != key && _.isEqual(v.port, port)); }); + if (!port.match(/^[0-9]+$/g)) { ports[key].error = 'Needs to be an integer.'; - } - else if (port <= 0 || port > 65535) { + } else if (port <= 0 || port > 65535) { ports[key].error = 'Needs to be in range <1,65535>.'; + } else if (otherPorts[port]) { + ports[key].error = 'Collision with port at container "'+ otherPorts[port] +'"'; + } else if (duplicates.length > 0) { + ports[key].error = 'Collision with another port in this container.'; + } else if (port == 22 || port == 2376) { + ports[key].error = 'Ports 22 and 2376 are reserved ports for Kitematic/Docker.'; } - else if (container) { - ports[key].error = 'Collision with port at container "'+ container.Name +'"'; - } - else if (duplicates.length > 0) { - ports[key].error = "Collision with other port at container."; - } - else if (port == 22 || port == 2376) { - ports[key].error = "Ports 22 and 2376 are reserved ports for Kitematic/Docker."; - } - this.setState({ ports: ports }); + this.setState({ports: ports}); }, handleSave: function() { containerActions.update(this.props.container.Name, { diff --git a/src/utils/ContainerUtil.js b/src/utils/ContainerUtil.js index c63b60b8c4..b30e5203df 100644 --- a/src/utils/ContainerUtil.js +++ b/src/utils/ContainerUtil.js @@ -45,25 +45,6 @@ var ContainerUtil = { }; }); return res; - }, - - /** - * Check if there is port colision with other containers - * @param {String} name name of the current container - * @param {Array} containers array of all containers - * @param {String} port - * @return {Object|null} return nothing or container with colision - */ - isPortCollision: function (name, containers, port) { - var interfaces = {}; - _.forEach(containers, container => { - if (container.Name != name) { - _.forEach(this.ports(container), (ip) => { - interfaces[ip + ':' + port] = container; - }); - } - }); - return interfaces[docker.host + ':' + port]; } };