Skip to content

Commit

Permalink
Revert "Don't assign IPv6 address when using calico-ipam"
Browse files Browse the repository at this point in the history
This reverts commit c5ec1e7.
  • Loading branch information
Casey D committed Feb 12, 2016
1 parent 4386319 commit 74a640a
Showing 3 changed files with 56 additions and 17 deletions.
15 changes: 11 additions & 4 deletions ipam.py
Original file line number Diff line number Diff line change
@@ -62,10 +62,11 @@ 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, _ = self._assign_address(handle_id=self.container_id)
ipv4, ipv6 = self._assign_address(handle_id=self.container_id)

# Output the response and exit successfully.
return json.dumps({"ip4": {"ip": str(ipv4.cidr)}})
return json.dumps({"ip4": {"ip": str(ipv4.cidr)},
"ip6": {"ip": str(ipv6.cidr)}})
else:
# Release IPs using the container_id as the handle.
_log.info("Releasing addresses on container %s",
@@ -86,7 +87,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=0, handle_id=handle_id, attributes=None,
num_v4=1, num_v6=1, handle_id=handle_id, attributes=None,
)
_log.debug("Allocated ip4s: %s, ip6s: %s", ipv4_addrs, ipv6_addrs)
except RuntimeError as e:
@@ -101,9 +102,15 @@ 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), None
return IPNetwork(ipv4), IPNetwork(ipv6)

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

# Create plugin.
@@ -148,7 +150,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)])
"cni", self.container_id, [IPNetwork(ip4), IPNetwork(ip6)])

# Assert a profile was applied.
self.client.append_profiles_to_endpoint.assert_called_once_with(
@@ -164,7 +166,9 @@ 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"
ipam_stdout = json.dumps({"ip4": {"ip": ip4}})
ip6 = "0:0:0:0:0:ffff:a00:1"
ipam_stdout = json.dumps({"ip4": {"ip": ip4},
"ip6": {"ip": ip6}})
self.set_ipam_result(0, ipam_stdout, "")

# Set up docker client response.
@@ -189,7 +193,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)])
"cni", self.container_id, [IPNetwork(ip4),IPNetwork(ip6)])

# Assert a profile was applied.
self.client.append_profiles_to_endpoint.assert_called_once_with(
@@ -206,7 +210,9 @@ 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"
ipam_stdout = json.dumps({"ip4": {"ip": ip4}})
ip6 = "0:0:0:0:0:ffff:a00:1"
ipam_stdout = json.dumps({"ip4": {"ip": ip4},
"ip6": {"ip": ip6}})
self.set_ipam_result(0, ipam_stdout, "")
self.policy = {"type": "k8s-annotations"}

@@ -242,7 +248,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)])
"cni", self.container_id, [IPNetwork(ip4), IPNetwork(ip6)])

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

# Create plugin.
@@ -455,7 +463,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)])
"cni", self.container_id, [IPNetwork(ip4), IPNetwork(ip6)])

# Assert set_profile called by policy driver.
self.client.append_profiles_to_endpoint.assert_called_once_with(
@@ -525,8 +533,10 @@ 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 []
self.m_ipam_plugin_client().auto_assign_ips.return_value = ip4s, None
ip6s = [ip6] if ip6 else []
self.m_ipam_plugin_client().auto_assign_ips.return_value = ip4s, ip6s

def test_add_ipam_error(self):
# Mock out auto_assign_ips to throw an error.
30 changes: 26 additions & 4 deletions tests/unit/test_ipam.py
Original file line number Diff line number Diff line change
@@ -67,14 +67,16 @@ 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, None
self.plugin._assign_address.return_value = ip4, ip6

# Call
ret = self.plugin.execute()

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

@patch('sys.stdout', new_callable=StringIO)
@@ -106,17 +108,19 @@ 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], []
self.plugin.datastore_client.auto_assign_ips.return_value = [ip4], [ip6]

# Args
handle_id = "abcdef12345"

# Call
ret_ip4, _ = self.plugin._assign_address(handle_id)
ret_ip4, ret_ip6 = 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
@@ -150,6 +154,24 @@ 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]

0 comments on commit 74a640a

Please sign in to comment.