Skip to content

Commit

Permalink
Merge pull request #186 from stfc/Fallback_to_sane_metadata
Browse files Browse the repository at this point in the history
BUG: Fall back to image metadata for invalid entries
  • Loading branch information
khalford authored Dec 5, 2024
2 parents 350a633 + 8849346 commit 57a2d72
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
37 changes: 33 additions & 4 deletions OpenStack-Rabbit-Consumer/rabbit_consumer/aq_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
"""
import logging
from dataclasses import dataclass
from typing import Dict, Optional
from typing import Dict, Optional, Union

from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig

logger = logging.getLogger(__name__)


# Case in-sensitive values that are considered invalid
_INVALID_VALUES = ["none", "null", ""]


@dataclass
class AqMetadata(DataClassDictMixin):
"""
Expand Down Expand Up @@ -48,8 +52,33 @@ class Config(BaseConfig):
def override_from_vm_meta(self, vm_meta: Dict[str, str]):
"""
Overrides the values in the metadata with the values from the VM's
metadata
metadata if they are present and sane
"""
for attr, alias in self.Config.aliases.items():
if alias in vm_meta:
setattr(self, attr, vm_meta[alias])
if alias not in vm_meta:
continue

if not self._is_metadata_val_valid(vm_meta[alias]):
logger.warning(
"Invalid metadata value '%s' found for metadata property '%s', skipping",
vm_meta[alias],
alias,
)
continue

setattr(self, attr, vm_meta[alias])

@staticmethod
def _is_metadata_val_valid(val: Union[str, None]) -> bool:
"""
Tests if an individual metadata value is sane, i.e.
a str which is not null, or a blocked value.
If this is valid, it returns true
"""
if not val:
return False

user_val = val.lower().strip()
if user_val in _INVALID_VALUES:
return False
return True
27 changes: 27 additions & 0 deletions OpenStack-Rabbit-Consumer/tests/test_aq_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ def test_aq_metadata_override_all(image_metadata):
assert returned.aq_os_version == "osversion_mock"


def test_aq_metadata_override_with_none_values(image_metadata):
"""
Tests that any invalid values, such as none, null or
whitespace are all ignored when overriding from the image
layer
"""
returned = AqMetadata.from_dict(image_metadata)
returned.override_from_vm_meta(
{
"AQ_ARCHETYPE": "archetype_mock_override",
"AQ_DOMAIN": "None",
"AQ_PERSONALITY": "null",
"AQ_OS": "none",
"AQ_OSVERSION": " ", # Space intentionally left
"AQ_SANDBOX": None,
}
)

assert returned.aq_archetype == "archetype_mock_override"

reference_metadata = AqMetadata.from_dict(image_metadata)
assert returned.aq_domain == reference_metadata.aq_domain
assert returned.aq_os == reference_metadata.aq_os
assert returned.aq_os_version == reference_metadata.aq_os_version
assert returned.aq_sandbox == reference_metadata.aq_sandbox


def test_aq_metadata_sandbox(image_metadata):
"""
Tests the sandbox value in an AQ metadata object
Expand Down
2 changes: 1 addition & 1 deletion OpenStack-Rabbit-Consumer/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.1
3.1.0
4 changes: 2 additions & 2 deletions charts/rabbit-consumer/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.7.1
version: 1.8.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "v3.0.1"
appVersion: "v3.1.0"

0 comments on commit 57a2d72

Please sign in to comment.