Skip to content

Commit d5fffca

Browse files
authored
Merge pull request #1697 from venukarnati92/async-fix
Async fix for Dynamic Client
2 parents 3fabf9e + 70ae7a8 commit d5fffca

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

kubernetes/base/dynamic/client.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def request(self, method, path, body=None, **params):
267267
# Authentication setting
268268
auth_settings = ['BearerToken']
269269

270-
return self.client.call_api(
270+
api_response = self.client.call_api(
271271
path,
272272
method.upper(),
273273
path_params,
@@ -281,6 +281,10 @@ def request(self, method, path, body=None, **params):
281281
_preload_content=False,
282282
_return_http_data_only=params.get('_return_http_data_only', True)
283283
)
284+
if params.get('async_req'):
285+
return api_response.get()
286+
else:
287+
return api_response
284288

285289
def validate(self, definition, version=None, strict=False):
286290
"""validate checks a kubernetes resource definition

kubernetes/base/dynamic/test_client.py

+115
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,121 @@ def test_cluster_custom_resources(self):
135135
changeme_api = client.resources.get(
136136
api_version='apps.example.com/v1', kind='ClusterChangeMe')
137137

138+
def test_async_namespaced_custom_resources(self):
139+
client = DynamicClient(api_client.ApiClient(configuration=self.config))
140+
141+
with self.assertRaises(ResourceNotFoundError):
142+
changeme_api = client.resources.get(
143+
api_version='apps.example.com/v1', kind='ChangeMe')
144+
145+
crd_api = client.resources.get(
146+
api_version='apiextensions.k8s.io/v1beta1',
147+
kind='CustomResourceDefinition')
148+
149+
name = 'changemes.apps.example.com'
150+
151+
crd_manifest = {
152+
'apiVersion': 'apiextensions.k8s.io/v1beta1',
153+
'kind': 'CustomResourceDefinition',
154+
'metadata': {
155+
'name': name,
156+
},
157+
'spec': {
158+
'group': 'apps.example.com',
159+
'names': {
160+
'kind': 'ChangeMe',
161+
'listKind': 'ChangeMeList',
162+
'plural': 'changemes',
163+
'singular': 'changeme',
164+
},
165+
'scope': 'Namespaced',
166+
'version': 'v1',
167+
'subresources': {
168+
'status': {}
169+
}
170+
}
171+
}
172+
async_resp = crd_api.create(crd_manifest, async_req=True)
173+
174+
self.assertEqual(name, async_resp.metadata.name)
175+
self.assertTrue(async_resp.status)
176+
177+
async_resp = crd_api.get(
178+
name=name,
179+
async_req=True
180+
)
181+
self.assertEqual(name, async_resp.metadata.name)
182+
self.assertTrue(async_resp.status)
183+
184+
try:
185+
changeme_api = client.resources.get(
186+
api_version='apps.example.com/v1', kind='ChangeMe')
187+
except ResourceNotFoundError:
188+
# Need to wait a sec for the discovery layer to get updated
189+
time.sleep(2)
190+
changeme_api = client.resources.get(
191+
api_version='apps.example.com/v1', kind='ChangeMe')
192+
193+
async_resp = changeme_api.get(async_req=True)
194+
self.assertEqual(async_resp.items, [])
195+
196+
changeme_name = 'custom-resource' + short_uuid()
197+
changeme_manifest = {
198+
'apiVersion': 'apps.example.com/v1',
199+
'kind': 'ChangeMe',
200+
'metadata': {
201+
'name': changeme_name,
202+
},
203+
'spec': {}
204+
}
205+
206+
async_resp = changeme_api.create(body=changeme_manifest, namespace='default', async_req=True)
207+
self.assertEqual(async_resp.metadata.name, changeme_name)
208+
209+
async_resp = changeme_api.get(name=changeme_name, namespace='default', async_req=True)
210+
self.assertEqual(async_resp.metadata.name, changeme_name)
211+
212+
changeme_manifest['spec']['size'] = 3
213+
async_resp = changeme_api.patch(
214+
body=changeme_manifest,
215+
namespace='default',
216+
content_type='application/merge-patch+json',
217+
async_req=True
218+
)
219+
self.assertEqual(async_resp.spec.size, 3)
220+
221+
async_resp = changeme_api.get(name=changeme_name, namespace='default', async_req=True)
222+
self.assertEqual(async_resp.spec.size, 3)
223+
224+
async_resp = changeme_api.get(namespace='default', async_req=True)
225+
self.assertEqual(len(async_resp.items), 1)
226+
227+
async_resp = changeme_api.get(async_req=True)
228+
self.assertEqual(len(async_resp.items), 1)
229+
230+
async_resp = changeme_api.delete(
231+
name=changeme_name,
232+
namespace='default',
233+
async_req=True
234+
)
235+
236+
async_resp = changeme_api.get(namespace='default', async_req=True)
237+
self.assertEqual(len(async_resp.items), 0)
238+
239+
async_resp = changeme_api.get(async_req=True)
240+
self.assertEqual(len(async_resp.items), 0)
241+
242+
async_resp = crd_api.delete(
243+
name=name,
244+
async_req=True
245+
)
246+
247+
time.sleep(2)
248+
client.resources.invalidate_cache()
249+
with self.assertRaises(ResourceNotFoundError):
250+
changeme_api = client.resources.get(
251+
api_version='apps.example.com/v1', kind='ChangeMe')
252+
138253
def test_namespaced_custom_resources(self):
139254
client = DynamicClient(api_client.ApiClient(configuration=self.config))
140255

0 commit comments

Comments
 (0)