Skip to content

Commit

Permalink
Merge pull request #53 from tomdee/no-assign-ipv6
Browse files Browse the repository at this point in the history
Don't assign IPv6 address when using calico-ipam
  • Loading branch information
tomdee committed Feb 12, 2016
2 parents 25a8713 + c5ec1e7 commit 4386319
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 56 deletions.
15 changes: 4 additions & 11 deletions ipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ def execute(self):
if self.command == "ADD":
# Assign an IP address for this container.
_log.info("Assigning address to container %s", self.container_id)
ipv4, ipv6 = self._assign_address(handle_id=self.container_id)
ipv4, _ = self._assign_address(handle_id=self.container_id)

# Output the response and exit successfully.
return json.dumps({"ip4": {"ip": str(ipv4.cidr)},
"ip6": {"ip": str(ipv6.cidr)}})
return json.dumps({"ip4": {"ip": str(ipv4.cidr)}})
else:
# Release IPs using the container_id as the handle.
_log.info("Releasing addresses on container %s",
Expand All @@ -87,7 +86,7 @@ def _assign_address(self, handle_id):
ipv6 = IPNetwork("::")
try:
ipv4_addrs, ipv6_addrs = self.datastore_client.auto_assign_ips(
num_v4=1, num_v6=1, handle_id=handle_id, attributes=None,
num_v4=1, num_v6=0, handle_id=handle_id, attributes=None,
)
_log.debug("Allocated ip4s: %s, ip6s: %s", ipv4_addrs, ipv6_addrs)
except RuntimeError as e:
Expand All @@ -102,15 +101,9 @@ def _assign_address(self, handle_id):
_log.error("No IPv4 address returned, exiting")
raise CniError(ERR_CODE_GENERIC,
msg="No IPv4 addresses available in pool")
try:
ipv6 = ipv6_addrs[0]
except IndexError:
_log.error("No IPv6 address returned, exiting")
raise CniError(ERR_CODE_GENERIC,
msg="No IPv6 addresses available in pool")

_log.info("Assigned IPv4: %s, IPv6: %s", ipv4, ipv6)
return IPNetwork(ipv4), IPNetwork(ipv6)
return IPNetwork(ipv4), None

def _parse_environment(self, env):
"""
Expand Down
28 changes: 9 additions & 19 deletions tests/fv/test_calico_cni.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ def test_add_mainline(self):
# Configure.
self.command = CNI_CMD_ADD
ip4 = "10.0.0.1/32"
ip6 = "0:0:0:0:0:ffff:a00:1"
ipam_stdout = json.dumps({"ip4": {"ip": ip4},
"ip6": {"ip": ip6}})
ipam_stdout = json.dumps({"ip4": {"ip": ip4}})
self.set_ipam_result(0, ipam_stdout, "")

# Create plugin.
Expand All @@ -150,7 +148,7 @@ def test_add_mainline(self):

# Assert an endpoint was created.
self.client.create_endpoint.assert_called_once_with(ANY,
"cni", self.container_id, [IPNetwork(ip4), IPNetwork(ip6)])
"cni", self.container_id, [IPNetwork(ip4)])

# Assert a profile was applied.
self.client.append_profiles_to_endpoint.assert_called_once_with(
Expand All @@ -166,9 +164,7 @@ def test_add_mainline_kubernetes_docker(self):
self.cni_args = "K8S_POD_NAME=podname;K8S_POD_NAMESPACE=default"
self.command = CNI_CMD_ADD
ip4 = "10.0.0.1/32"
ip6 = "0:0:0:0:0:ffff:a00:1"
ipam_stdout = json.dumps({"ip4": {"ip": ip4},
"ip6": {"ip": ip6}})
ipam_stdout = json.dumps({"ip4": {"ip": ip4}})
self.set_ipam_result(0, ipam_stdout, "")

# Set up docker client response.
Expand All @@ -193,7 +189,7 @@ def test_add_mainline_kubernetes_docker(self):

# Assert an endpoint was created.
self.client.create_endpoint.assert_called_once_with(ANY,
"cni", self.container_id, [IPNetwork(ip4),IPNetwork(ip6)])
"cni", self.container_id, [IPNetwork(ip4)])

# Assert a profile was applied.
self.client.append_profiles_to_endpoint.assert_called_once_with(
Expand All @@ -210,9 +206,7 @@ def test_add_mainline_kubernetes_annotations(self, m_requests):
self.cni_args = "K8S_POD_NAME=podname;K8S_POD_NAMESPACE=defaultns"
self.command = CNI_CMD_ADD
ip4 = "10.0.0.1/32"
ip6 = "0:0:0:0:0:ffff:a00:1"
ipam_stdout = json.dumps({"ip4": {"ip": ip4},
"ip6": {"ip": ip6}})
ipam_stdout = json.dumps({"ip4": {"ip": ip4}})
self.set_ipam_result(0, ipam_stdout, "")
self.policy = {"type": "k8s-annotations"}

Expand Down Expand Up @@ -248,7 +242,7 @@ def test_add_mainline_kubernetes_annotations(self, m_requests):

# Assert an endpoint was created.
self.client.create_endpoint.assert_called_once_with(ANY,
"cni", self.container_id, [IPNetwork(ip4), IPNetwork(ip6)])
"cni", self.container_id, [IPNetwork(ip4)])

# Assert profile was created.
self.client.create_profile.assert_called_once_with(
Expand Down Expand Up @@ -439,9 +433,7 @@ def test_add_error_profile_create(self):
# Configure.
self.command = CNI_CMD_ADD
ip4 = "10.0.0.1/32"
ip6 = "0:0:0:0:0:ffff:a00:1"
ipam_stdout = json.dumps({"ip4": {"ip": ip4},
"ip6": {"ip": ip6}})
ipam_stdout = json.dumps({"ip4": {"ip": ip4}})
self.set_ipam_result(0, ipam_stdout, "")

# Create plugin.
Expand All @@ -463,7 +455,7 @@ def test_add_error_profile_create(self):

# Assert an endpoint was created.
self.client.create_endpoint.assert_called_once_with(ANY,
"cni", self.container_id, [IPNetwork(ip4), IPNetwork(ip6)])
"cni", self.container_id, [IPNetwork(ip4)])

# Assert set_profile called by policy driver.
self.client.append_profiles_to_endpoint.assert_called_once_with(
Expand Down Expand Up @@ -533,10 +525,8 @@ def set_ipam_result(self, rc, stdout, stderr):
if stdout and not rc:
# A successful add response.
ip4 = json.loads(stdout)["ip4"]["ip"]
ip6 = json.loads(stdout)["ip6"]["ip"]
ip4s = [ip4] if ip4 else []
ip6s = [ip6] if ip6 else []
self.m_ipam_plugin_client().auto_assign_ips.return_value = ip4s, ip6s
self.m_ipam_plugin_client().auto_assign_ips.return_value = ip4s, None

def test_add_ipam_error(self):
# Mock out auto_assign_ips to throw an error.
Expand Down
30 changes: 4 additions & 26 deletions tests/unit/test_ipam.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ def test_execute_add_mainline(self, m_stdout):
# Mock
self.plugin.command = CNI_CMD_ADD
ip4 = IPNetwork("1.2.3.4/32")
ip6 = IPNetwork("ba:ad::be:ef/128")
self.plugin._assign_address = MagicMock(spec=self.plugin._assign_address)
self.plugin._assign_address.return_value = ip4, ip6
self.plugin._assign_address.return_value = ip4, None

# Call
ret = self.plugin.execute()

# Assert
expected = json.dumps({"ip4": {"ip": "1.2.3.4/32"},
"ip6": {"ip": "ba:ad::be:ef/128"}})
expected = json.dumps({"ip4": {"ip": "1.2.3.4/32"}})
assert_equal(ret, expected)

@patch('sys.stdout', new_callable=StringIO)
Expand Down Expand Up @@ -108,19 +106,17 @@ def test_execute_del_not_assigned(self, m_stdout):
def test_assign_address_mainline(self):
# Mock
ip4 = IPNetwork("1.2.3.4/32")
ip6 = IPNetwork("ba:ad::be:ef/128")
self.plugin.datastore_client.auto_assign_ips = MagicMock(spec=self.plugin._assign_address)
self.plugin.datastore_client.auto_assign_ips.return_value = [ip4], [ip6]
self.plugin.datastore_client.auto_assign_ips.return_value = [ip4], []

# Args
handle_id = "abcdef12345"

# Call
ret_ip4, ret_ip6 = self.plugin._assign_address(handle_id)
ret_ip4, _ = self.plugin._assign_address(handle_id)

# Assert
assert_equal(ip4, ret_ip4)
assert_equal(ip6, ret_ip6)

def test_assign_address_runtime_err(self):
# Mock
Expand Down Expand Up @@ -154,24 +150,6 @@ def test_assign_address_no_ipv4(self, m_exit):
# Assert
assert_equal(e.code, ERR_CODE_GENERIC)

@patch("ipam._exit_on_error", autospec=True)
def test_assign_address_no_ipv6(self, m_exit):
# Mock
ip4 = IPNetwork("1.2.3.4/32")
self.plugin.datastore_client.auto_assign_ips = MagicMock(spec=self.plugin._assign_address)
self.plugin.datastore_client.auto_assign_ips.return_value = [ip4], []

# Args
handle_id = "abcdef12345"

# Call
with assert_raises(CniError) as err:
self.plugin._assign_address(handle_id)
e = err.exception

# Assert
assert_equal(e.code, ERR_CODE_GENERIC)

def test_parse_environment_no_command(self):
# Delete command.
del self.env[CNI_COMMAND_ENV]
Expand Down

0 comments on commit 4386319

Please sign in to comment.