Proceed CNI output in format of version 0.3.1

Kuryr-kubernetes declares, that supported CNI version is 0.3.0, but
it prints to output in format of version 0.2.0.
Kubernetes can't parse it.
This patch modifies CNI output according to 0.3.1, it has a little
difference with version 0.3.0, just in naming of ips field.

Change-Id: I7b6bb5c178035b7c85fc28973f9a0cf1bc1a139e
Closes-Bug: 1779718
Signed-off-by: Alexey Perevalov <a.perevalov@samsung.com>
This commit is contained in:
Alexey Perevalov 2018-07-04 04:27:11 -04:00
parent bf3ce1ad47
commit 607a249e10
5 changed files with 52 additions and 13 deletions

View File

@ -256,7 +256,7 @@
x="754.02399" x="754.02399"
y="640.83807" y="640.83807"
id="tspan10312-2-0" id="tspan10312-2-0"
style="font-weight:normal;font-size:30px;line-height:93.99999976%">CNI 0.3.0</tspan></text> style="font-weight:normal;font-size:30px;line-height:93.99999976%">CNI 0.3.1</tspan></text>
<g <g
id="g10768"> id="g10768">
<path <path

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

View File

@ -142,7 +142,7 @@ Kubelet would only use the lexicographically first file in that directory, so
make sure that it is kuryr's config file:: make sure that it is kuryr's config file::
{ {
"cniVersion": "0.3.0", "cniVersion": "0.3.1",
"name": "kuryr", "name": "kuryr",
"type": "kuryr-cni", "type": "kuryr-cni",
"kuryr_conf": "/etc/kuryr/kuryr.conf", "kuryr_conf": "/etc/kuryr/kuryr.conf",

View File

@ -1,5 +1,5 @@
{ {
"cniVersion": "0.3.0", "cniVersion": "0.3.1",
"name": "kuryr", "name": "kuryr",
"type": "kuryr-cni", "type": "kuryr-cni",
"kuryr_conf": "/etc/kuryr/kuryr.conf", "kuryr_conf": "/etc/kuryr/kuryr.conf",

View File

@ -38,8 +38,8 @@ LOG = logging.getLogger(__name__)
class CNIRunner(object): class CNIRunner(object):
# TODO(ivc): extend SUPPORTED_VERSIONS and format output based on # TODO(ivc): extend SUPPORTED_VERSIONS and format output based on
# requested params.CNI_VERSION and/or params.config.cniVersion # requested params.CNI_VERSION and/or params.config.cniVersion
VERSION = '0.3.0' VERSION = '0.3.1'
SUPPORTED_VERSIONS = ['0.3.0'] SUPPORTED_VERSIONS = ['0.3.1']
@abc.abstractmethod @abc.abstractmethod
def _add(self, params): def _add(self, params):
@ -69,6 +69,10 @@ class CNIRunner(object):
def prepare_env(self, env, stdin): def prepare_env(self, env, stdin):
raise NotImplementedError() raise NotImplementedError()
@abc.abstractmethod
def get_container_id(self):
raise NotImplementedError()
def run(self, env, fin, fout): def run(self, env, fin, fout):
try: try:
# Prepare params according to calling Object # Prepare params according to calling Object
@ -89,24 +93,36 @@ class CNIRunner(object):
self._write_exception(fout, str(ex)) self._write_exception(fout, str(ex))
return 1 return 1
def _vif_data(self, vif): def _vif_data(self, vif, params):
result = {} result = {}
nameservers = [] nameservers = []
cni_ip_list = result.setdefault("ips", [])
cni_routes_list = result.setdefault("routes", [])
result["interfaces"] = [
{
"name": vif.vif_name,
"mac": vif.address,
"sandbox": self.get_container_id(params)}]
for subnet in vif.network.subnets.objects: for subnet in vif.network.subnets.objects:
cni_ip = {}
nameservers.extend(subnet.dns) nameservers.extend(subnet.dns)
ip = subnet.ips.objects[0].address ip = subnet.ips.objects[0].address
cni_ip = result.setdefault("ip%s" % ip.version, {})
cni_ip['ip'] = "%s/%s" % (ip, subnet.cidr.prefixlen) cni_ip['version'] = str(ip.version)
cni_ip['address'] = "%s/%s" % (ip, subnet.cidr.prefixlen)
cni_ip['interface'] = len(result["interfaces"]) - 1
if hasattr(subnet, 'gateway'): if hasattr(subnet, 'gateway'):
cni_ip['gateway'] = str(subnet.gateway) cni_ip['gateway'] = str(subnet.gateway)
if subnet.routes.objects: if subnet.routes.objects:
cni_ip['routes'] = [ routes = [
{'dst': str(route.cidr), 'gw': str(route.gateway)} {'dst': str(route.cidr), 'gw': str(route.gateway)}
for route in subnet.routes.objects] for route in subnet.routes.objects]
cni_routes_list.extend(routes)
cni_ip_list.append(cni_ip)
if nameservers: if nameservers:
result['dns'] = {'nameservers': nameservers} result['dns'] = {'nameservers': nameservers}
@ -120,7 +136,7 @@ class CNIStandaloneRunner(CNIRunner):
def _add(self, params): def _add(self, params):
vif = self._plugin.add(params) vif = self._plugin.add(params)
return self._vif_data(vif) return self._vif_data(vif, params)
def _delete(self, params): def _delete(self, params):
self._plugin.delete(params) self._plugin.delete(params)
@ -128,13 +144,16 @@ class CNIStandaloneRunner(CNIRunner):
def prepare_env(self, env, stdin): def prepare_env(self, env, stdin):
return utils.CNIParameters(env, stdin) return utils.CNIParameters(env, stdin)
def get_container_id(self, params):
return params.CNI_CONTAINERID
class CNIDaemonizedRunner(CNIRunner): class CNIDaemonizedRunner(CNIRunner):
def _add(self, params): def _add(self, params):
resp = self._make_request('addNetwork', params, httplib.ACCEPTED) resp = self._make_request('addNetwork', params, httplib.ACCEPTED)
vif = base.VersionedObject.obj_from_primitive(resp.json()) vif = base.VersionedObject.obj_from_primitive(resp.json())
return self._vif_data(vif) return self._vif_data(vif, params)
def _delete(self, params): def _delete(self, params):
self._make_request('delNetwork', params, httplib.NO_CONTENT) self._make_request('delNetwork', params, httplib.NO_CONTENT)
@ -146,6 +165,9 @@ class CNIDaemonizedRunner(CNIRunner):
cni_envs['config_kuryr'] = dict(stdin) cni_envs['config_kuryr'] = dict(stdin)
return cni_envs return cni_envs
def get_container_id(self, params):
return params["CNI_CONTAINERID"]
def _make_request(self, path, cni_envs, expected_status=None): def _make_request(self, path, cni_envs, expected_status=None):
method = 'POST' method = 'POST'

View File

@ -62,8 +62,10 @@ class TestCNIStandaloneRunner(test_base.TestCase, TestCNIRunnerMixin):
m_k8s_add.return_value = vif m_k8s_add.return_value = vif
m_fin = StringIO() m_fin = StringIO()
m_fout = StringIO() m_fout = StringIO()
container_id = 'a4181c680a39'
env = { env = {
'CNI_COMMAND': 'ADD', 'CNI_COMMAND': 'ADD',
'CNI_CONTAINERID': container_id,
'CNI_ARGS': 'foo=bar', 'CNI_ARGS': 'foo=bar',
} }
self.runner.run(env, m_fin, m_fout) self.runner.run(env, m_fin, m_fout)
@ -71,9 +73,22 @@ class TestCNIStandaloneRunner(test_base.TestCase, TestCNIRunnerMixin):
self.assertEqual('foo=bar', m_k8s_add.call_args[0][0].CNI_ARGS) self.assertEqual('foo=bar', m_k8s_add.call_args[0][0].CNI_ARGS)
result = jsonutils.loads(m_fout.getvalue()) result = jsonutils.loads(m_fout.getvalue())
self.assertDictEqual( self.assertDictEqual(
{"cniVersion": "0.3.0", {"cniVersion": '0.3.1',
"dns": {"nameservers": ["192.168.0.1"]}, "dns": {"nameservers": ["192.168.0.1"]},
"ip4": {"gateway": "192.168.0.1", "ip": "192.168.0.2/24"}}, "ips": [
{
"version": "4",
"gateway": "192.168.0.1",
"address": "192.168.0.2/24",
"interface": 0,
}],
"interfaces": [
{
"name": vif.vif_name,
"mac": vif.address,
"sandbox": container_id,
}],
"routes": []},
result) result)
@mock.patch('kuryr_kubernetes.cni.plugins.k8s_cni.K8sCNIPlugin.delete') @mock.patch('kuryr_kubernetes.cni.plugins.k8s_cni.K8sCNIPlugin.delete')
@ -84,6 +99,7 @@ class TestCNIStandaloneRunner(test_base.TestCase, TestCNIRunnerMixin):
m_fout = StringIO() m_fout = StringIO()
env = { env = {
'CNI_COMMAND': 'DEL', 'CNI_COMMAND': 'DEL',
'CNI_CONTAINERID': 'a4181c680a39',
'CNI_ARGS': 'foo=bar', 'CNI_ARGS': 'foo=bar',
} }
self.runner.run(env, m_fin, m_fout) self.runner.run(env, m_fin, m_fout)
@ -103,6 +119,7 @@ class TestCNIDaemonizedRunner(test_base.TestCase, TestCNIRunnerMixin):
m_fout = StringIO() m_fout = StringIO()
env = { env = {
'CNI_COMMAND': cni_cmd, 'CNI_COMMAND': cni_cmd,
'CNI_CONTAINERID': 'a4181c680a39',
'CNI_ARGS': 'foo=bar', 'CNI_ARGS': 'foo=bar',
} }
result = self.runner.run(env, m_fin, m_fout) result = self.runner.run(env, m_fin, m_fout)