Browse Source

Merge "Fix CRD podSelector update"

tags/1.0.0
Zuul 3 months ago
parent
commit
b76c379ed1

+ 5
- 5
kuryr_kubernetes/controller/drivers/utils.py View File

@@ -225,11 +225,11 @@ def patch_kuryr_crd(crd, i_rules, e_rules, pod_selector, np_spec=None):
225 225
         np_spec = crd['spec']['networkpolicy_spec']
226 226
     LOG.debug('Patching KuryrNetPolicy CRD %s' % crd_name)
227 227
     try:
228
-        kubernetes.patch('spec', crd['metadata']['selfLink'],
229
-                         {'ingressSgRules': i_rules,
230
-                          'egressSgRules': e_rules,
231
-                          'podSelector': pod_selector,
232
-                          'networkpolicy_spec': np_spec})
228
+        kubernetes.patch_crd('spec', crd['metadata']['selfLink'],
229
+                             {'ingressSgRules': i_rules,
230
+                              'egressSgRules': e_rules,
231
+                              'podSelector': pod_selector,
232
+                              'networkpolicy_spec': np_spec})
233 233
     except k_exc.K8sClientException:
234 234
         LOG.exception('Error updating kuryrnetpolicy CRD %s', crd_name)
235 235
         raise

+ 25
- 4
kuryr_kubernetes/k8s_client.py View File

@@ -83,9 +83,9 @@ class K8sClient(object):
83 83
         result = response.json() if json else response.text
84 84
         return result
85 85
 
86
-    def _get_url_and_header(self, path):
86
+    def _get_url_and_header(self, path, content_type):
87 87
         url = self._base_url + path
88
-        header = {'Content-Type': 'application/merge-patch+json',
88
+        header = {'Content-Type': content_type,
89 89
                   'Accept': 'application/json'}
90 90
         if self.token:
91 91
             header.update({'Authorization': 'Bearer %s' % self.token})
@@ -97,7 +97,8 @@ class K8sClient(object):
97 97
             'path': path, 'data': data})
98 98
         if field == 'status':
99 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 102
         response = requests.patch(url, json={field: data},
102 103
                                   headers=header, cert=self.cert,
103 104
                                   verify=self.verify_server)
@@ -105,6 +106,25 @@ class K8sClient(object):
105 106
             return response.json().get('status')
106 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 128
     def post(self, path, body):
109 129
         LOG.debug("Post %(path)s: %(body)s", {'path': path, 'body': body})
110 130
         url = self._base_url + path
@@ -142,7 +162,8 @@ class K8sClient(object):
142 162
         LOG.debug("Annotate %(path)s: %(names)s", {
143 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 168
         while itertools.count(1):
148 169
             metadata = {"annotations": annotations}

+ 1
- 1
kuryr_kubernetes/tests/unit/controller/drivers/test_network_policy.py View File

@@ -294,7 +294,7 @@ class TestNetworkPolicyDriver(test_base.TestCase):
294 294
                        'parse_network_policy_rules')
295 295
     def test_update_security_group_rules_with_k8s_exc(self, m_parse, m_get_crd,
296 296
                                                       m_create_sgr):
297
-        self._driver.kubernetes.patch.side_effect = (
297
+        self._driver.kubernetes.patch_crd.side_effect = (
298 298
             exceptions.K8sClientException())
299 299
         m_get_crd.return_value = self._crd
300 300
         m_parse.return_value = (self._i_rules, self._e_rules)

Loading…
Cancel
Save