Optimize the case of versioned image endpoint in catalog

We have version discovery for glance. However, if someone asks for a
version, and it's the version that they get from the catalog, we can
skip doing a full discovery run. Do that.

Change-Id: I2c0694ffc70bf2801de1fc187ba7b38e6a1b9d09
This commit is contained in:
Monty Taylor 2017-05-15 11:14:27 -05:00
parent 40f66c70c3
commit 9ecf600397
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
4 changed files with 105 additions and 2 deletions

View File

@ -418,8 +418,22 @@ class OpenStackCloud(
self._raw_clients['raw-image'] = image_client
return self._raw_clients['raw-image']
def _match_given_image_endpoint(self, given, version):
if given.endswith('/'):
given = given[:-1]
if given.split('/')[-1].startswith('v' + version[0]):
return True
return False
def _discover_image_endpoint(self, config_version, image_client):
try:
# First - quick check to see if the endpoint in the catalog
# is a versioned endpoint that matches the version we requested.
# If it is, don't do any additoinal work.
catalog_endpoint = image_client.get_endpoint()
if self._match_given_image_endpoint(
catalog_endpoint, config_version):
return catalog_endpoint
# Version discovery
versions = image_client.get('/')
api_version = None

View File

@ -401,7 +401,7 @@ class RequestsMockTestCase(BaseTestCase):
return _RoleData(role_id, role_name, {'role': response},
{'role': request})
def use_keystone_v3(self):
def use_keystone_v3(self, catalog='catalog-v3.json'):
self.adapter = self.useFixture(rm_fixture.Fixture())
self.calls = []
self._uri_registry.clear()
@ -413,7 +413,7 @@ class RequestsMockTestCase(BaseTestCase):
headers={
'X-Subject-Token': self.getUniqueString('KeystoneToken')},
text=open(os.path.join(
self.fixtures_directory, 'catalog-v3.json'), 'r').read()
self.fixtures_directory, catalog), 'r').read()
)
])
self._make_test_cloud(identity_api_version='3')

View File

@ -0,0 +1,71 @@
{
"token": {
"audit_ids": [
"Rvn7eHkiSeOwucBIPaKdYA"
],
"catalog": [
{
"endpoints": [
{
"id": "5a64de3c4a614d8d8f8d1ba3dee5f45f",
"interface": "public",
"region": "RegionOne",
"url": "https://image.example.com/v2"
}
],
"name": "glance",
"type": "image"
},
{
"endpoints": [
{
"id": "4deb4d0504a044a395d4480741ba628c",
"interface": "public",
"region": "RegionOne",
"url": "https://identity.example.com"
},
{
"id": "012322eeedcd459edabb4933021112bc",
"interface": "admin",
"region": "RegionOne",
"url": "https://identity.example.com"
}
],
"endpoints_links": [],
"name": "keystone",
"type": "identity"
}
],
"expires_at": "9999-12-31T23:59:59Z",
"issued_at": "2016-12-17T14:25:05.000000Z",
"methods": [
"password"
],
"project": {
"domain": {
"id": "default",
"name": "default"
},
"id": "1c36b64c840a42cd9e9b931a369337f0",
"name": "Default Project"
},
"roles": [
{
"id": "9fe2ff9ee4384b1894a90878d3e92bab",
"name": "_member_"
},
{
"id": "37071fc082e14c2284c32a2761f71c63",
"name": "swiftoperator"
}
],
"user": {
"domain": {
"id": "default",
"name": "default"
},
"id": "c17534835f8f42bf98fc367e0bf35e09",
"name": "mordred"
}
}
}

View File

@ -761,3 +761,21 @@ class TestImageBrokenDiscovery(base.RequestsMockTestCase):
self.cloud._image_client.endpoint_override,
'https://image.example.com/v2/')
self.assert_calls()
class TestImageDiscoveryOptimization(base.RequestsMockTestCase):
def setUp(self):
super(TestImageDiscoveryOptimization, self).setUp()
self.use_keystone_v3(catalog='catalog-versioned-image.json')
def test_version_discovery_skip(self):
self.cloud.cloud_config.config['image_api_version'] = '2'
self.register_uris([
dict(method='GET',
uri='https://image.example.com/v2/images',
json={'images': []})
])
self.assertEqual([], self.cloud.list_images())
self.assert_calls()