|
| 1 | +# Copyright 2026 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Apply Proxmox NoCloud network data from a config-drive file.""" |
| 16 | + |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import string |
| 20 | +import sys |
| 21 | + |
| 22 | +try: |
| 23 | + from oslo_log import log as oslo_logging |
| 24 | + |
| 25 | + LOG = oslo_logging.getLogger(__name__) |
| 26 | +except Exception: # pragma: no cover - fallback when oslo logging is unavailable |
| 27 | + logging.basicConfig(level=logging.INFO) |
| 28 | + LOG = logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +DEFAULT_NETWORK_DATA_FILENAMES = ("NETWORK_CONFIG", "network-config") |
| 32 | + |
| 33 | + |
| 34 | +def _iter_search_roots(): |
| 35 | + search_roots = os.environ.get("PROXMOX_NETWORK_DATA_SEARCH_ROOTS") |
| 36 | + if search_roots: |
| 37 | + for root in search_roots.split(os.pathsep): |
| 38 | + root = root.strip() |
| 39 | + if root: |
| 40 | + yield root |
| 41 | + return |
| 42 | + |
| 43 | + for drive_letter in string.ascii_uppercase: |
| 44 | + yield "%s:\\" % drive_letter |
| 45 | + |
| 46 | + |
| 47 | +def _iter_candidate_paths(): |
| 48 | + override_path = os.environ.get("PROXMOX_NETWORK_DATA_PATH", "").strip() |
| 49 | + if override_path: |
| 50 | + yield override_path |
| 51 | + return |
| 52 | + |
| 53 | + for root in _iter_search_roots(): |
| 54 | + normalized_root = root.rstrip("\\/") |
| 55 | + for filename in DEFAULT_NETWORK_DATA_FILENAMES: |
| 56 | + yield "%s\\%s" % (normalized_root, filename) |
| 57 | + |
| 58 | + |
| 59 | +def find_network_data_path(path_exists=os.path.exists): |
| 60 | + for candidate_path in _iter_candidate_paths(): |
| 61 | + if path_exists(candidate_path): |
| 62 | + return candidate_path |
| 63 | + return None |
| 64 | + |
| 65 | + |
| 66 | +def load_network_data(network_data_path, open_file=open, parser=None): |
| 67 | + if parser is None: |
| 68 | + from cloudbaseinit.utils import serialization |
| 69 | + |
| 70 | + parser = serialization.parse_json_yaml |
| 71 | + |
| 72 | + with open_file(network_data_path, "r", encoding="utf-8") as network_data_file: |
| 73 | + raw_network_data = network_data_file.read() |
| 74 | + |
| 75 | + network_data = parser(raw_network_data) |
| 76 | + if not isinstance(network_data, dict): |
| 77 | + raise ValueError( |
| 78 | + "Proxmox network data parsed into %r, expected dict" % |
| 79 | + type(network_data) |
| 80 | + ) |
| 81 | + |
| 82 | + return network_data |
| 83 | + |
| 84 | + |
| 85 | +def apply_network_data(network_data, network_parser=None, plugin_factory=None): |
| 86 | + if network_parser is None: |
| 87 | + from cloudbaseinit.metadata.services.nocloudservice import ( |
| 88 | + NoCloudNetworkConfigParser, |
| 89 | + ) |
| 90 | + |
| 91 | + network_parser = NoCloudNetworkConfigParser.parse |
| 92 | + |
| 93 | + if plugin_factory is None: |
| 94 | + from cloudbaseinit.plugins.common import networkconfig |
| 95 | + |
| 96 | + plugin_factory = networkconfig.NetworkConfigPlugin |
| 97 | + |
| 98 | + network_details = network_parser(network_data) |
| 99 | + if not network_details: |
| 100 | + LOG.warning("NoCloud network parser returned no interfaces") |
| 101 | + return False |
| 102 | + |
| 103 | + plugin = plugin_factory() |
| 104 | + process_network_details = getattr(plugin, "_process_network_details_v2", None) |
| 105 | + if process_network_details is None: |
| 106 | + raise AttributeError( |
| 107 | + "Cloudbase-Init network plugin is missing _process_network_details_v2" |
| 108 | + ) |
| 109 | + |
| 110 | + process_network_details(network_details) |
| 111 | + return True |
| 112 | + |
| 113 | + |
| 114 | +def main(): |
| 115 | + network_data_path = find_network_data_path() |
| 116 | + if not network_data_path: |
| 117 | + LOG.info( |
| 118 | + "No Proxmox network data found in candidate paths: %s", |
| 119 | + ", ".join(_iter_candidate_paths()), |
| 120 | + ) |
| 121 | + return 0 |
| 122 | + |
| 123 | + try: |
| 124 | + network_data = load_network_data(network_data_path) |
| 125 | + except Exception: |
| 126 | + LOG.exception( |
| 127 | + "Failed to load Proxmox network data from %s", network_data_path |
| 128 | + ) |
| 129 | + return 0 |
| 130 | + |
| 131 | + try: |
| 132 | + LOG.info("Applying Proxmox network data from %s", network_data_path) |
| 133 | + applied = apply_network_data(network_data) |
| 134 | + except Exception: |
| 135 | + LOG.exception( |
| 136 | + "Failed to apply Proxmox network data from %s", network_data_path |
| 137 | + ) |
| 138 | + return 0 |
| 139 | + |
| 140 | + if not applied: |
| 141 | + LOG.warning( |
| 142 | + "No network interfaces were applied from %s", network_data_path |
| 143 | + ) |
| 144 | + |
| 145 | + return 0 |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + sys.exit(main()) |
0 commit comments