Skip to content

Commit

Permalink
Rework Mesos labels hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Brockbank authored and ozdanborne committed Jun 21, 2016
1 parent 739c944 commit 241ec96
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
25 changes: 20 additions & 5 deletions calico.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ def __init__(self, network_config, env):
Name of Kubernetes namespace if running under Kubernetes, else None.
"""

self.labels = network_config.get(LABELS_KEY, {})
"""
Label data to assign to this endpoint.
"""

self.network_config = network_config
"""
Network config as provided in the CNI network file passed in
Expand Down Expand Up @@ -115,6 +110,26 @@ def __init__(self, network_config, env):
Environment dictionary used when calling the IPAM plugin.
"""

self.labels = {}
"""
Label data to assign to this endpoint. The origin of the labels is orchestrator
dependent.
"""

self.args = network_config.get(ARGS_KEY, {})

# If there Mesos namespaced data, extract any labels that have been specified.
# Note that Mesos labels are a list of {"key": <key>, "value": <value>}, so pull
# the key and values and populate the labels dictionary.
if MESOS_NS_KEY in self.args:
_log.info("Extracting Mesos namespaced data")
labels_list = self.args[MESOS_NS_KEY]. \
get(MESOS_NETWORK_INFO_KEY, {}). \
get(MESOS_LABELS_OUTER_KEY, {}). \
get(MESOS_LABELS_KEY, [])
for label in labels_list:
self.labels[label["key"]] = label["value"]

self.command = env[CNI_COMMAND_ENV]
assert self.command in [CNI_CMD_DELETE, CNI_CMD_ADD], \
"Invalid CNI command %s" % self.command
Expand Down
21 changes: 20 additions & 1 deletion calico_cni/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
POLICY_KEY = "policy"
ASSIGN_IPV4_KEY = "assign_ipv4"
ASSIGN_IPV6_KEY = "assign_ipv6"
LABELS_KEY = "labels"
ARGS_KEY = "args"

# Constants for getting policy specific information
# from the policy dictionary in the network config file.
Expand All @@ -71,3 +71,22 @@
LOG_DIR = "/var/log/calico/cni"
LOG_FORMAT = '%(asctime)s %(process)d [%(identity)s] %(levelname)s %(message)s'

# Mesos namespaced data. Mesos CNI inserts the following additional data into
# the args field:
# "args" : {
# "org.apache.mesos" : {
# "network_info" : {
# "name" : "mynet",
# "labels" : {
# "labels" : [
# { "key" : "app", "value" : "myapp" },
# { "key" : "env", "value" : "prod" }
# ]
# }
# }
# }
# }
MESOS_NS_KEY = "org.apache.mesos"
MESOS_NETWORK_INFO_KEY = "network_info"
MESOS_LABELS_OUTER_KEY = "labels"
MESOS_LABELS_KEY = "labels"
15 changes: 12 additions & 3 deletions tests/unit/test_cni_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,17 @@ def setUp(self):
"range-start": "",
"range-end": ""
},
"labels": {
"group": "production"
"args" : {
"org.apache.mesos" : {
"network_info" : {
"name" : "mynet",
"labels" : {
"labels" : [
{ "key" : "group", "value" : "production" },
]
},
},
},
}
}
self.env = {
Expand Down Expand Up @@ -546,7 +555,7 @@ def test_create_endpoint_mainline(self):
self.plugin._client.create_endpoint.assert_called_once_with(ANY,
self.expected_orch_id, self.expected_workload_id, ip_list)
assert_equal(ep, endpoint)
self.assertEqual(ep.labels, self.network_config['labels'])
self.assertEqual(ep.labels, {"group": "production"})

def test_create_endpoint_error(self):
# Mock.
Expand Down

0 comments on commit 241ec96

Please sign in to comment.