summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2019-02-20 16:58:39 +0000
committerGerrit Code Review <review@openstack.org>2019-02-20 16:58:39 +0000
commitb76c379ed1ba2f749ba65e429fe7269b4101f8a1 (patch)
tree034b91b330e3692de24d30f68b858da4edb82041
parentc4d7f5e7ee196d10729592b98be16398a071a510 (diff)
parent5cf4b4177254272cd15885411194d033a188c0d7 (diff)
Merge "Fix CRD podSelector update"
-rw-r--r--kuryr_kubernetes/controller/drivers/utils.py10
-rw-r--r--kuryr_kubernetes/k8s_client.py29
-rw-r--r--kuryr_kubernetes/tests/unit/controller/drivers/test_network_policy.py2
3 files changed, 31 insertions, 10 deletions
diff --git a/kuryr_kubernetes/controller/drivers/utils.py b/kuryr_kubernetes/controller/drivers/utils.py
index dc758cd..6b5141d 100644
--- a/kuryr_kubernetes/controller/drivers/utils.py
+++ b/kuryr_kubernetes/controller/drivers/utils.py
@@ -225,11 +225,11 @@ def patch_kuryr_crd(crd, i_rules, e_rules, pod_selector, np_spec=None):
225 np_spec = crd['spec']['networkpolicy_spec'] 225 np_spec = crd['spec']['networkpolicy_spec']
226 LOG.debug('Patching KuryrNetPolicy CRD %s' % crd_name) 226 LOG.debug('Patching KuryrNetPolicy CRD %s' % crd_name)
227 try: 227 try:
228 kubernetes.patch('spec', crd['metadata']['selfLink'], 228 kubernetes.patch_crd('spec', crd['metadata']['selfLink'],
229 {'ingressSgRules': i_rules, 229 {'ingressSgRules': i_rules,
230 'egressSgRules': e_rules, 230 'egressSgRules': e_rules,
231 'podSelector': pod_selector, 231 'podSelector': pod_selector,
232 'networkpolicy_spec': np_spec}) 232 'networkpolicy_spec': np_spec})
233 except k_exc.K8sClientException: 233 except k_exc.K8sClientException:
234 LOG.exception('Error updating kuryrnetpolicy CRD %s', crd_name) 234 LOG.exception('Error updating kuryrnetpolicy CRD %s', crd_name)
235 raise 235 raise
diff --git a/kuryr_kubernetes/k8s_client.py b/kuryr_kubernetes/k8s_client.py
index 11dfedc..8cdc702 100644
--- a/kuryr_kubernetes/k8s_client.py
+++ b/kuryr_kubernetes/k8s_client.py
@@ -83,9 +83,9 @@ class K8sClient(object):
83 result = response.json() if json else response.text 83 result = response.json() if json else response.text
84 return result 84 return result
85 85
86 def _get_url_and_header(self, path): 86 def _get_url_and_header(self, path, content_type):
87 url = self._base_url + path 87 url = self._base_url + path
88 header = {'Content-Type': 'application/merge-patch+json', 88 header = {'Content-Type': content_type,
89 'Accept': 'application/json'} 89 'Accept': 'application/json'}
90 if self.token: 90 if self.token:
91 header.update({'Authorization': 'Bearer %s' % self.token}) 91 header.update({'Authorization': 'Bearer %s' % self.token})
@@ -97,7 +97,8 @@ class K8sClient(object):
97 'path': path, 'data': data}) 97 'path': path, 'data': data})
98 if field == 'status': 98 if field == 'status':
99 path = path + '/' + str(field) 99 path = path + '/' + str(field)
100 url, header = self._get_url_and_header(path) 100 content_type = 'application/merge-patch+json'
101 url, header = self._get_url_and_header(path, content_type)
101 response = requests.patch(url, json={field: data}, 102 response = requests.patch(url, json={field: data},
102 headers=header, cert=self.cert, 103 headers=header, cert=self.cert,
103 verify=self.verify_server) 104 verify=self.verify_server)
@@ -105,6 +106,25 @@ class K8sClient(object):
105 return response.json().get('status') 106 return response.json().get('status')
106 raise exc.K8sClientException(response.text) 107 raise exc.K8sClientException(response.text)
107 108
109 def patch_crd(self, field, path, data):
110 content_type = 'application/json-patch+json'
111 url, header = self._get_url_and_header(path, content_type)
112
113 data = [{'op': 'replace',
114 'path': '/{}/{}'.format(field, np_field),
115 'value': value}
116 for np_field, value in data.items()]
117
118 LOG.debug("Patch %(path)s: %(data)s", {
119 'path': path, 'data': data})
120
121 response = requests.patch(url, data=jsonutils.dumps(data),
122 headers=header, cert=self.cert,
123 verify=self.verify_server)
124 if response.ok:
125 return response.json().get('status')
126 raise exc.K8sClientException(response.text)
127
108 def post(self, path, body): 128 def post(self, path, body):
109 LOG.debug("Post %(path)s: %(body)s", {'path': path, 'body': body}) 129 LOG.debug("Post %(path)s: %(body)s", {'path': path, 'body': body})
110 url = self._base_url + path 130 url = self._base_url + path
@@ -142,7 +162,8 @@ class K8sClient(object):
142 LOG.debug("Annotate %(path)s: %(names)s", { 162 LOG.debug("Annotate %(path)s: %(names)s", {
143 'path': path, 'names': list(annotations)}) 163 'path': path, 'names': list(annotations)})
144 164
145 url, header = self._get_url_and_header(path) 165 content_type = 'application/merge-patch+json'
166 url, header = self._get_url_and_header(path, content_type)
146 167
147 while itertools.count(1): 168 while itertools.count(1):
148 metadata = {"annotations": annotations} 169 metadata = {"annotations": annotations}
diff --git a/kuryr_kubernetes/tests/unit/controller/drivers/test_network_policy.py b/kuryr_kubernetes/tests/unit/controller/drivers/test_network_policy.py
index 4002987..1381a8e 100644
--- a/kuryr_kubernetes/tests/unit/controller/drivers/test_network_policy.py
+++ b/kuryr_kubernetes/tests/unit/controller/drivers/test_network_policy.py
@@ -294,7 +294,7 @@ class TestNetworkPolicyDriver(test_base.TestCase):
294 'parse_network_policy_rules') 294 'parse_network_policy_rules')
295 def test_update_security_group_rules_with_k8s_exc(self, m_parse, m_get_crd, 295 def test_update_security_group_rules_with_k8s_exc(self, m_parse, m_get_crd,
296 m_create_sgr): 296 m_create_sgr):
297 self._driver.kubernetes.patch.side_effect = ( 297 self._driver.kubernetes.patch_crd.side_effect = (
298 exceptions.K8sClientException()) 298 exceptions.K8sClientException())
299 m_get_crd.return_value = self._crd 299 m_get_crd.return_value = self._crd
300 m_parse.return_value = (self._i_rules, self._e_rules) 300 m_parse.return_value = (self._i_rules, self._e_rules)