diff --git a/kuryr_kubernetes/controller/drivers/utils.py b/kuryr_kubernetes/controller/drivers/utils.py index 88f5fd7fe..80f657bc9 100644 --- a/kuryr_kubernetes/controller/drivers/utils.py +++ b/kuryr_kubernetes/controller/drivers/utils.py @@ -351,14 +351,17 @@ def match_labels(crd_labels, labels): def match_selector(selector, labels): + if selector is None: + return True + if labels is None: + labels = {} crd_labels = selector.get('matchLabels', None) crd_expressions = selector.get('matchExpressions', None) match_exp = match_lb = True if crd_expressions: - match_exp = match_expressions(crd_expressions, - labels) - if crd_labels and labels: + match_exp = match_expressions(crd_expressions, labels) + if crd_labels: match_lb = match_labels(crd_labels, labels) return match_exp and match_lb diff --git a/kuryr_kubernetes/tests/unit/controller/drivers/test_utils.py b/kuryr_kubernetes/tests/unit/controller/drivers/test_utils.py index 8a22b982c..52147e4cd 100644 --- a/kuryr_kubernetes/tests/unit/controller/drivers/test_utils.py +++ b/kuryr_kubernetes/tests/unit/controller/drivers/test_utils.py @@ -63,3 +63,26 @@ class TestUtils(test_base.TestCase): def test_get_network_id_empty(self): self.assertRaises(exceptions.IntegrityError, utils.get_network_id, {}) + + def test_match_selector(self): + self.assertFalse( + utils.match_selector({'matchLabels': {'app': 'demo'}}, None)) + self.assertFalse( + utils.match_selector({'matchLabels': {'app': 'demo'}}, {})) + self.assertFalse( + utils.match_selector({'matchLabels': {'app': 'demo'}}, + {'app': 'foobar'})) + self.assertTrue( + utils.match_selector({'matchLabels': {'app': 'demo'}}, + {'app': 'demo'})) + self.assertTrue( + utils.match_selector({'matchLabels': {'app': 'demo'}}, + {'app': 'demo', 'foo': 'bar'})) + self.assertTrue( + utils.match_selector({'matchLabels': {'app': 'demo', + 'foo': 'bar'}}, + {'app': 'demo', 'foo': 'bar'})) + self.assertFalse( + utils.match_selector({'matchLabels': {'app': 'demo', + 'foo': 'bar'}}, + {'app': 'demo'}))