Skip to content

Commit

Permalink
Merge pull request #541 from rackerlabs/remove-env-name-flavor
Browse files Browse the repository at this point in the history
fix: we don't want the env name in the flavor
  • Loading branch information
cardoe authored Dec 12, 2024
2 parents b27fc13 + 7dc998b commit 464fcce
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 42 deletions.
8 changes: 3 additions & 5 deletions operators/nova-flavors/nova_flavors/flavor_synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ def reconcile(self, desired_flavors: list[FlavorSpec]):
existing_flavors = self._nova.flavors.list()
for flavor in desired_flavors:
nova_flavor = next(
(flv for flv in existing_flavors if flv.name == flavor.stripped_name),
(flv for flv in existing_flavors if flv.name == flavor.name),
None,
)

update_needed = False
if nova_flavor:
logger.info(
f"Flavor: {flavor.stripped_name} already exists. Syncing values"
)
logger.info(f"Flavor: {flavor.name} already exists. Syncing values")
if nova_flavor.ram != flavor.memory_mib:
logger.info(
f"{flavor.name} RAM mismatch - {nova_flavor.ram=} {flavor.memory_mib=}"
Expand Down Expand Up @@ -86,7 +84,7 @@ def reconcile(self, desired_flavors: list[FlavorSpec]):

def _create(self, flavor: FlavorSpec):
nova_flavor = self._nova.flavors.create(
flavor.stripped_name,
flavor.name,
flavor.memory_mib,
flavor.cpu_cores,
min(flavor.drives),
Expand Down
10 changes: 5 additions & 5 deletions operators/nova-flavors/tests/test_flavor_synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ def test_flavor_synchronizer_reconcile_new_flavor(
mock_nova_client.return_value.flavors.list.return_value = []
flavor_synchronizer.reconcile([flavor])
mock_nova_client.return_value.flavors.create.assert_called_once_with(
flavor.stripped_name, flavor.memory_mib, flavor.cpu_cores, min(flavor.drives)
flavor.name, flavor.memory_mib, flavor.cpu_cores, min(flavor.drives)
)


def test_flavor_synchronizer_reconcile_existing_flavor(
flavor_synchronizer, mock_nova_client, flavor
):
existing_flavor = MagicMock()
existing_flavor.name = flavor.stripped_name
existing_flavor.name = flavor.name
existing_flavor.ram = flavor.memory_mib
existing_flavor.disk = max(flavor.drives)
existing_flavor.vcpus = flavor.cpu_cores
Expand All @@ -75,7 +75,7 @@ def test_flavor_synchronizer_reconcile_existing_flavor_update_needed(
flavor_synchronizer, mock_nova_client, flavor
):
existing_flavor = MagicMock()
existing_flavor.name = flavor.stripped_name
existing_flavor.name = flavor.name
existing_flavor.ram = flavor.memory_mib + 1
existing_flavor.disk = max(flavor.drives)
existing_flavor.vcpus = flavor.cpu_cores
Expand All @@ -84,7 +84,7 @@ def test_flavor_synchronizer_reconcile_existing_flavor_update_needed(
flavor_synchronizer.reconcile([flavor])
existing_flavor.delete.assert_called_once()
mock_nova_client.return_value.flavors.create.assert_called_once_with(
flavor.stripped_name, flavor.memory_mib, flavor.cpu_cores, min(flavor.drives)
flavor.name, flavor.memory_mib, flavor.cpu_cores, min(flavor.drives)
)


Expand All @@ -94,7 +94,7 @@ def test_flavor_synchronizer_create_flavor(
mock_create_flavor = mock_nova_client.return_value.flavors.create.return_value
flavor_synchronizer._create(flavor)
mock_nova_client.return_value.flavors.create.assert_called_once_with(
flavor.stripped_name, flavor.memory_mib, flavor.cpu_cores, min(flavor.drives)
flavor.name, flavor.memory_mib, flavor.cpu_cores, min(flavor.drives)
)
mock_create_flavor.set_keys.assert_called_once_with(
{
Expand Down
20 changes: 1 addition & 19 deletions python/understack-flavor-matcher/flavor_matcher/flavor_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,15 @@ def from_yaml(yaml_str: str) -> "FlavorSpec":
pci=data.get("pci", []),
)

@staticmethod
def configured_envtype():
return os.getenv("FLAVORS_ENV", "unconfigured")

@property
def stripped_name(self):
"""Returns actual flavor name with the prod/nonprod prefix removed."""
_, name = self.name.split(".", 1)
if not name:
raise Exception(f"Unable to strip envtype from flavor: {self.name}")
return name

@property
def baremetal_nova_resource_class(self):
"""Returns flavor name converted to be used with Nova flavor resources.
https://docs.openstack.org/ironic/latest/install/configure-nova-flavors.html
"""
converted_name = re.sub(r"[^\w]", "_", self.stripped_name).upper()
converted_name = re.sub(r"[^\w]", "_", self.name).upper()
return f"resources:CUSTOM_BAREMETAL_{converted_name}"

@property
def env_type(self):
return self.name.split(".")[0]

@property
def memory_mib(self):
"""Returns memory size in MiB"""
Expand All @@ -80,8 +64,6 @@ def from_directory(directory: str = "/etc/flavors/") -> list["FlavorSpec"]:
with open(filepath, "r") as file:
yaml_content = file.read()
flavor_spec = FlavorSpec.from_yaml(yaml_content)
if flavor_spec.env_type != FlavorSpec.configured_envtype():
continue
flavor_specs.append(flavor_spec)
except yaml.YAMLError as e:
print(f"Error parsing YAML file {filename}: {e}")
Expand Down
14 changes: 4 additions & 10 deletions python/understack-flavor-matcher/tests/test_flavor_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def valid_yaml():
return """
---
name: nonprod.gp2.ultramedium
name: gp2.ultramedium
manufacturer: Dell
model: PowerEdge R7615
memory_gb: 7777
Expand Down Expand Up @@ -46,8 +46,7 @@ def yaml_directory(tmp_path, valid_yaml, invalid_yaml):

def test_from_yaml(valid_yaml):
spec = FlavorSpec.from_yaml(valid_yaml)
assert spec.name == "nonprod.gp2.ultramedium"
assert spec.stripped_name == "gp2.ultramedium"
assert spec.name == "gp2.ultramedium"
assert spec.manufacturer == "Dell"
assert spec.model == "PowerEdge R7615"
assert spec.memory_gb == 7777
Expand Down Expand Up @@ -76,7 +75,7 @@ def test_from_directory(mocked_open, mock_walk, valid_yaml, invalid_yaml):
specs = FlavorSpec.from_directory("/etc/flavors/")

assert len(specs) == 1
assert specs[0].name == "nonprod.gp2.ultramedium"
assert specs[0].name == "gp2.ultramedium"
assert specs[0].memory_gb == 7777
assert specs[0].cpu_cores == 245

Expand All @@ -86,7 +85,7 @@ def test_from_directory_with_real_files(yaml_directory):
specs = FlavorSpec.from_directory(str(yaml_directory))

assert len(specs) == 1
assert specs[0].name == "nonprod.gp2.ultramedium"
assert specs[0].name == "gp2.ultramedium"
assert specs[0].memory_gb == 7777
assert specs[0].cpu_cores == 245

Expand Down Expand Up @@ -332,8 +331,3 @@ def test_baremetal_nova_resource_class(valid_yaml):
flv.baremetal_nova_resource_class
== "resources:CUSTOM_BAREMETAL_GP2_ULTRAMEDIUM"
)


def test_envtype(valid_yaml):
flv = FlavorSpec.from_yaml(valid_yaml)
assert flv.env_type == "nonprod"
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ def guess_machine_flavor(device_info: ChassisInfo, bmc: Bmc) -> str:
raise Exception(
f"Machine: {machine} could not be classified into any flavor {FLAVORS=}"
)
logger.info(f"Device has been classified as flavor: {flavor_name.stripped_name}")
logger.info(f"Device has been classified as flavor: {flavor_name.name}")

return flavor_name.stripped_name
return flavor_name.name
7 changes: 6 additions & 1 deletion workflows/argo-events/workflowtemplates/enroll-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ spec:
readOnly: true
- mountPath: /etc/understack_flavors/
name: understack-flavors
subPath: current/flavors/
readOnly: true
env:
- name: WF_NS
Expand All @@ -82,6 +81,12 @@ spec:
value: "{{workflow.name}}"
- name: WF_UID
value: "{{workflow.uid}}"
- name: FLAVORS_DIR
valueFrom:
configMapKeyRef:
name: understack-flavors
key: FLAVORS_DIR
optional: true
volumes:
- name: bmc-master
secret:
Expand Down

0 comments on commit 464fcce

Please sign in to comment.