Skip to content

Commit

Permalink
allow resource names >2 parts
Browse files Browse the repository at this point in the history
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'
  • Loading branch information
ejacques committed Dec 7, 2024
1 parent 5e5de93 commit 0eca545
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
3 changes: 2 additions & 1 deletion kubernetes_asyncio/dynamic/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ async def get_resources_for_api_version(self, prefix, group, version, preferred)
resources_raw = list(filter(lambda r: '/' not in r['name'], resources_response))
subresources_raw = list(filter(lambda r: '/' in r['name'], resources_response))
for subresource in subresources_raw:
resource, name = subresource['name'].split('/')
# Handle resources with >2 parts in their name
resource, name = subresource['name'].split('/', 1)

Check warning on line 193 in kubernetes_asyncio/dynamic/discovery.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/dynamic/discovery.py#L193

Added line #L193 was not covered by tests
if not subresources.get(resource):
subresources[resource] = {}
subresources[resource][name] = subresource
Expand Down
32 changes: 29 additions & 3 deletions kubernetes_asyncio/dynamic/discovery_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

import os
import unittest
from unittest.mock import AsyncMock, MagicMock, patch

from kubernetes_asyncio.client import api_client
from kubernetes_asyncio.dynamic import DynamicClient
from kubernetes_asyncio.dynamic.discovery import Discoverer
from kubernetes_asyncio.e2e_test import base


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

# do Discoverer.__init__
# async with api_client.ApiClient(configuration=self.config) as apic:
async with api_client.ApiClient(configuration=self.config) as apic:

Check warning on line 61 in kubernetes_asyncio/dynamic/discovery_test.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/dynamic/discovery_test.py#L61

Added line #L61 was not covered by tests
client2 = await DynamicClient(apic)
# the resources of client will use _cache['resources'] decode from cache file
deploy2 = await client2.resources.get(kind='Deployment', api_version="apps/v1")
Expand All @@ -65,5 +67,29 @@ async def test_cache_decoder_resource_and_subresource(self):
# test Resource is the same
self.assertDictEqual(deploy1.to_dict(), deploy2.to_dict())

# test Subresource is the same
self.assertDictEqual(deploy1.status.to_dict(), deploy2.status.to_dict())
@patch('kubernetes_asyncio.dynamic.discovery.Discoverer.get_resources_for_api_version', new_callable=AsyncMock)
async def test_get_resources_for_api_version(self, mock_get_resources):
"""Test case for get_resources_for_api_version"""
mock_get_resources.return_value = {

Check warning on line 73 in kubernetes_asyncio/dynamic/discovery_test.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/dynamic/discovery_test.py#L73

Added line #L73 was not covered by tests
'resources': [{'name': 'pods', 'kind': 'Pod'}],
'subresources': {
'virtualmachineinstances': {
'sev/fetchcertchain': {'name': 'virtualmachineinstances/sev/fetchcertchain'}
}
}
}

# Create a mock client with the necessary attributes
mock_client = MagicMock()
mock_client.configuration.host = "https://mock-host"

Check warning on line 84 in kubernetes_asyncio/dynamic/discovery_test.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/dynamic/discovery_test.py#L83-L84

Added lines #L83 - L84 were not covered by tests

discoverer = Discoverer(client=mock_client)
response = await discoverer.get_resources_for_api_version('api', 'v1', 'pods', True)
self.assertEqual(response['resources'][0]['name'], 'pods')
self.assertEqual(response['resources'][0]['kind'], 'Pod')
self.assertIn('virtualmachineinstances', response['subresources'])
self.assertIn('sev/fetchcertchain', response['subresources']['virtualmachineinstances'])

Check warning on line 91 in kubernetes_asyncio/dynamic/discovery_test.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/dynamic/discovery_test.py#L86-L91

Added lines #L86 - L91 were not covered by tests


if __name__ == '__main__':
unittest.main()

Check warning on line 95 in kubernetes_asyncio/dynamic/discovery_test.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/dynamic/discovery_test.py#L95

Added line #L95 was not covered by tests

0 comments on commit 0eca545

Please sign in to comment.