Skip to content

Commit 193c9f2

Browse files
edwinpjacquesejacques
and
ejacques
authored
allow resource names >2 parts (tomplus#343)
Dynamic client discovery for resources with three part names, the processing fails. Allow for processing names with >2 parts. This was failing on an OpenShift environment with the resource 'virtualmachineinstances/sev/fetchcertchain' Co-authored-by: ejacques <[email protected]>
1 parent 5e5de93 commit 193c9f2

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

kubernetes_asyncio/dynamic/discovery.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ async def get_resources_for_api_version(self, prefix, group, version, preferred)
189189
resources_raw = list(filter(lambda r: '/' not in r['name'], resources_response))
190190
subresources_raw = list(filter(lambda r: '/' in r['name'], resources_response))
191191
for subresource in subresources_raw:
192-
resource, name = subresource['name'].split('/')
192+
# Handle resources with >2 parts in their name
193+
resource, name = subresource['name'].split('/', 1)
193194
if not subresources.get(resource):
194195
subresources[resource] = {}
195196
subresources[resource][name] = subresource

kubernetes_asyncio/dynamic/discovery_test.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
import os
1616
import unittest
17+
from unittest.mock import AsyncMock, MagicMock, patch
1718

1819
from kubernetes_asyncio.client import api_client
1920
from kubernetes_asyncio.dynamic import DynamicClient
21+
from kubernetes_asyncio.dynamic.discovery import Discoverer
2022
from kubernetes_asyncio.e2e_test import base
2123

2224

@@ -56,7 +58,7 @@ async def test_cache_decoder_resource_and_subresource(self):
5658
deploy1 = await client.resources.get(kind='Deployment', api_version="apps/v1")
5759

5860
# do Discoverer.__init__
59-
# async with api_client.ApiClient(configuration=self.config) as apic:
61+
async with api_client.ApiClient(configuration=self.config) as apic:
6062
client2 = await DynamicClient(apic)
6163
# the resources of client will use _cache['resources'] decode from cache file
6264
deploy2 = await client2.resources.get(kind='Deployment', api_version="apps/v1")
@@ -65,5 +67,29 @@ async def test_cache_decoder_resource_and_subresource(self):
6567
# test Resource is the same
6668
self.assertDictEqual(deploy1.to_dict(), deploy2.to_dict())
6769

68-
# test Subresource is the same
69-
self.assertDictEqual(deploy1.status.to_dict(), deploy2.status.to_dict())
70+
@patch('kubernetes_asyncio.dynamic.discovery.Discoverer.get_resources_for_api_version', new_callable=AsyncMock)
71+
async def test_get_resources_for_api_version(self, mock_get_resources):
72+
"""Test case for get_resources_for_api_version"""
73+
mock_get_resources.return_value = {
74+
'resources': [{'name': 'pods', 'kind': 'Pod'}],
75+
'subresources': {
76+
'virtualmachineinstances': {
77+
'sev/fetchcertchain': {'name': 'virtualmachineinstances/sev/fetchcertchain'}
78+
}
79+
}
80+
}
81+
82+
# Create a mock client with the necessary attributes
83+
mock_client = MagicMock()
84+
mock_client.configuration.host = "https://mock-host"
85+
86+
discoverer = Discoverer(client=mock_client)
87+
response = await discoverer.get_resources_for_api_version('api', 'v1', 'pods', True)
88+
self.assertEqual(response['resources'][0]['name'], 'pods')
89+
self.assertEqual(response['resources'][0]['kind'], 'Pod')
90+
self.assertIn('virtualmachineinstances', response['subresources'])
91+
self.assertIn('sev/fetchcertchain', response['subresources']['virtualmachineinstances'])
92+
93+
94+
if __name__ == '__main__':
95+
unittest.main()

0 commit comments

Comments
 (0)