-
Notifications
You must be signed in to change notification settings - Fork 18
Static Networking Config #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-bot
merged 1 commit into
openshift-assisted:master
from
keitwb:static-hosts-nmstate
Sep 19, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| """Static networking related functionality.""" | ||
|
|
||
| from .config import ( | ||
| add_or_replace_static_host_config_yaml, | ||
| remove_static_host_config_by_index, | ||
| validate_and_parse_nmstate, | ||
| ) | ||
| from .template import NMStateTemplateParams, generate_nmstate_from_template | ||
|
|
||
| __all__ = [ | ||
| "NMStateTemplateParams", | ||
| "add_or_replace_static_host_config_yaml", | ||
| "generate_nmstate_from_template", | ||
| "remove_static_host_config_by_index", | ||
| "validate_and_parse_nmstate", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| """infra env static_network_config field handling""" | ||
|
|
||
| import json | ||
| from typing import Any, TypedDict, cast | ||
|
|
||
| import yaml | ||
|
|
||
|
|
||
| class MacInterfaceMap(TypedDict): | ||
| """Maps a NIC to a MAC Address.""" | ||
|
|
||
| logical_nic_name: str | ||
| mac_address: str | ||
|
|
||
|
|
||
| class HostStaticNetworkConfig(TypedDict): | ||
| """Matches the structure in the Assisted Installer API.""" | ||
|
|
||
| mac_interface_map: list[MacInterfaceMap] | ||
| network_yaml: str | ||
|
carbonin marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def remove_static_host_config_by_index( | ||
| existing_static_network_config: str, index: int | ||
| ) -> list[HostStaticNetworkConfig]: | ||
| """Remove a single host's config by index position.""" | ||
| config = [ | ||
| cast(HostStaticNetworkConfig, d) | ||
| for d in json.loads(existing_static_network_config) | ||
| ] | ||
| if index < 0: | ||
| raise IndexError("negative indexes are not allowed") | ||
| if index >= len(config): | ||
| raise ValueError( | ||
| f"static network config only has {len(config)} elements, cannot delete index {index}" | ||
| ) | ||
| del config[index] | ||
| return config | ||
|
|
||
|
|
||
| def add_or_replace_static_host_config_yaml( | ||
| existing_static_network_config: str | None, | ||
| index: int | None, | ||
| new_nmstate_yaml: str, | ||
| ) -> list[HostStaticNetworkConfig]: | ||
| """Add/update a single host's config by index. | ||
|
|
||
| Raises: | ||
| - IndexError: if the index is out of range for the existing config | ||
| """ | ||
| config: list[HostStaticNetworkConfig] = [] | ||
| if existing_static_network_config: | ||
| config = [ | ||
| cast(HostStaticNetworkConfig, d) | ||
| for d in json.loads(existing_static_network_config) | ||
| ] | ||
|
|
||
| host_config = _generate_host_static_config(new_nmstate_yaml) | ||
| if index is None: | ||
| config.append(host_config) | ||
| else: | ||
| if index < 0: | ||
| raise IndexError("negative indexes are not allowed") | ||
| if index >= len(config): | ||
| raise IndexError( | ||
| f"static network config only has {len(config)} elements, cannot replace index {index}" | ||
| ) | ||
| config[index] = host_config | ||
|
|
||
| return config | ||
|
|
||
|
|
||
| def _generate_host_static_config(nmstate_yaml: str) -> HostStaticNetworkConfig: | ||
| nmstate = validate_and_parse_nmstate(nmstate_yaml) | ||
| interfaces = nmstate.get("interfaces") | ||
| name_and_mac_list: list[MacInterfaceMap] = [ | ||
| { | ||
| "mac_address": i.get("mac-address"), | ||
| "logical_nic_name": i.get("name"), | ||
| } | ||
| for i in interfaces | ||
| if i.get("mac-address") | ||
| ] | ||
| if not name_and_mac_list: | ||
| raise ValueError("At least one interface must be associated to a MAC Address") | ||
|
|
||
| new_host: HostStaticNetworkConfig = { | ||
| "mac_interface_map": name_and_mac_list, | ||
| "network_yaml": nmstate_yaml, | ||
| } | ||
| return new_host | ||
|
|
||
|
|
||
| def validate_and_parse_nmstate(nmstate_yaml: str) -> Any: | ||
| """Validate nmstate yaml and return it parsed. | ||
|
|
||
| Raises: | ||
| - ValueError: If the yaml is invalid is some way | ||
| """ | ||
| # Eventually when nmstate 2.2.51 is released we should be able to validate the nmstate by doing: | ||
| # libnmstate.validate(nmstate_yaml) | ||
| # For now just make sure it is valid yaml | ||
| try: | ||
| return yaml.safe_load(nmstate_yaml) | ||
| except yaml.YAMLError as e: | ||
| raise ValueError("Invalid YAML") from e | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.