Add openstack-network-agents charm
The charm deploys the openstack-network-agents snap, configures external bridge, physnet mapping, supports enabling chassis as gateway (ovn-cms-options). It runs as a subordinate to MicroOVN to provide external north/south connectivity for the network role. Change-Id: I0922348b2d1f6e474470da0a4632e4121caa046a Signed-off-by: Fabian Fulga <ffulga@cloudbasesolutions.com>
This commit is contained in:
4
charms/openstack-network-agents/.sunbeam-build.yaml
Normal file
4
charms/openstack-network-agents/.sunbeam-build.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
external-libraries:
|
||||
- charms.operator_libs_linux.v2.snap
|
||||
internal-libraries: []
|
||||
templates: []
|
202
charms/openstack-network-agents/LICENSE
Normal file
202
charms/openstack-network-agents/LICENSE
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2025 Canonical Ltd.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
23
charms/openstack-network-agents/README.md
Normal file
23
charms/openstack-network-agents/README.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# OpenStack Network Agents (charm)
|
||||
|
||||
This subordinate charm drives the `openstack-network-agents` snap on
|
||||
Sunbeam network-role nodes. It configures the provider bridge (`br-ex`),
|
||||
OVN physnet mapping (`physnet1`) and configures the node to act as
|
||||
a gateway via `enable-chassis-as-gw` option.
|
||||
|
||||
## Usage
|
||||
|
||||
Attach to MicroOVN units:
|
||||
|
||||
```bash
|
||||
juju relate openstack-network-agents:juju-info microovn:juju-info
|
||||
```
|
||||
|
||||
## Configure
|
||||
|
||||
```bash
|
||||
juju config openstack-network-agents \
|
||||
external-interface=enp86s0 \
|
||||
bridge-name=br-ex \
|
||||
physnet-name=physnet1
|
||||
```
|
70
charms/openstack-network-agents/charmcraft.yaml
Normal file
70
charms/openstack-network-agents/charmcraft.yaml
Normal file
@@ -0,0 +1,70 @@
|
||||
type: charm
|
||||
name: openstack-network-agents
|
||||
title: OpenStack Network Agents (subordinate)
|
||||
summary: Subordinate charm to drive the openstack-network-agents snap
|
||||
description: |
|
||||
Installs and configures the 'openstack-network-agents' snap, sets up the
|
||||
provider bridge/physnet mapping, and runs neutron-ovn-metadata-agent.
|
||||
Attach this charm to MicroOVN units to provide north/south traffic.
|
||||
|
||||
base: ubuntu@24.04
|
||||
subordinate: true
|
||||
|
||||
platforms:
|
||||
amd64:
|
||||
|
||||
requires:
|
||||
juju-info:
|
||||
interface: juju-info
|
||||
scope: container
|
||||
|
||||
config:
|
||||
options:
|
||||
snap-name:
|
||||
type: string
|
||||
default: openstack-network-agents
|
||||
description: Name of the snap to install.
|
||||
snap-channel:
|
||||
type: string
|
||||
default: latest/edge
|
||||
description: Snap channel to track.
|
||||
|
||||
debug:
|
||||
type: boolean
|
||||
default: false
|
||||
|
||||
actions:
|
||||
set-network-agents-local-settings:
|
||||
description: |
|
||||
Apply settings specific to this unit for provider networking.
|
||||
params:
|
||||
external-interface:
|
||||
type: string
|
||||
description: Physical NIC to add as a bridge port (e.g. enp6s0).
|
||||
bridge-name:
|
||||
type: string
|
||||
description: Provider bridge name.
|
||||
physnet-name:
|
||||
type: string
|
||||
description: OVN physnet label.
|
||||
enable-chassis-as-gw:
|
||||
type: boolean
|
||||
description: |
|
||||
If true, set ovn-cms-options=enable-chassis-as-gw so L3 gateway roles
|
||||
prefer scheduling here.
|
||||
additionalProperties: false
|
||||
|
||||
parts:
|
||||
charm:
|
||||
plugin: charm
|
||||
source: .
|
||||
charm-entrypoint: src/charm.py
|
||||
charm-requirements: [requirements.txt]
|
||||
build-packages:
|
||||
- build-essential
|
||||
- python3-dev
|
||||
- libssl-dev
|
||||
- pkg-config
|
||||
- libffi-dev
|
||||
- rustc
|
||||
- cargo
|
18
charms/openstack-network-agents/pyproject.toml
Normal file
18
charms/openstack-network-agents/pyproject.toml
Normal file
@@ -0,0 +1,18 @@
|
||||
# Project configuration
|
||||
[project]
|
||||
name = "openstack-network-agents"
|
||||
version = "2025.1"
|
||||
requires-python = "~=3.12.0"
|
||||
|
||||
dependencies = [
|
||||
"cryptography",
|
||||
"jinja2",
|
||||
"pydantic",
|
||||
"lightkube",
|
||||
"lightkube-models",
|
||||
"requests",
|
||||
"ops",
|
||||
"interface_tls_certificates@git+https://opendev.org/openstack/charm-ops-interface-tls-certificates",
|
||||
"tenacity", # From ops_sunbeam
|
||||
"opentelemetry-api~=1.21.0", # charm_tracing library -> opentelemetry-sdk requires 1.21.0
|
||||
]
|
3
charms/openstack-network-agents/rebuild
Normal file
3
charms/openstack-network-agents/rebuild
Normal file
@@ -0,0 +1,3 @@
|
||||
# This file is used to trigger a build.
|
||||
# Change uuid to trigger a new build.
|
||||
f439952d-0ff5-4695-ad6d-a1f93cfbcb1c
|
2
charms/openstack-network-agents/requirements.txt
Normal file
2
charms/openstack-network-agents/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
ops ~= 2.17
|
||||
tenacity
|
226
charms/openstack-network-agents/src/charm.py
Executable file
226
charms/openstack-network-agents/src/charm.py
Executable file
@@ -0,0 +1,226 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Copyright 2025 Canonical Ltd.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Openstack Network Agents subordinate charm.
|
||||
|
||||
This charm deploys the `openstack-network-agents` snap and configures
|
||||
OVS bridge mapping + optional chassis-as-gw on the network role node.
|
||||
"""
|
||||
|
||||
|
||||
from __future__ import (
|
||||
annotations,
|
||||
)
|
||||
|
||||
import logging
|
||||
|
||||
import charms.operator_libs_linux.v2.snap as snap
|
||||
import ops
|
||||
import ops_sunbeam.charm as sunbeam_charm
|
||||
import ops_sunbeam.guard as sunbeam_guard
|
||||
import ops_sunbeam.relation_handlers as sunbeam_rhandlers
|
||||
import ops_sunbeam.tracing as sunbeam_tracing
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
OVN_CHASSIS_PLUG = "ovn-chassis"
|
||||
OVN_CHASSIS_SLOT = "microovn:ovn-chassis"
|
||||
|
||||
|
||||
@sunbeam_tracing.trace_type
|
||||
class JujuInfoRequiresHandler(sunbeam_rhandlers.RelationHandler):
|
||||
"""Relation handler for juju-info interface."""
|
||||
|
||||
def setup_event_handler(self):
|
||||
"""Set up event handler."""
|
||||
rel = self.charm.on[self.relation_name]
|
||||
self.framework.observe(rel.relation_joined, self._on_event)
|
||||
self.framework.observe(rel.relation_changed, self._on_event)
|
||||
self.framework.observe(rel.relation_broken, self._on_event)
|
||||
return None
|
||||
|
||||
def _on_event(self, event: ops.RelationEvent) -> None:
|
||||
"""Handle juju-info relation events."""
|
||||
self.callback_f(event)
|
||||
|
||||
@property
|
||||
def ready(self) -> bool:
|
||||
"""True if at least one juju-info relation is present."""
|
||||
return bool(self.charm.model.relations.get(self.relation_name))
|
||||
|
||||
|
||||
@sunbeam_tracing.trace_sunbeam_charm
|
||||
class OpenstackNetworkAgentsOperatorCharm(
|
||||
sunbeam_charm.OSBaseOperatorCharmSnap
|
||||
):
|
||||
"""Snap-based subordinate for OVN provider bridge configuration (no daemons)."""
|
||||
|
||||
service_name = "openstack-network-agents"
|
||||
_state = ops.framework.StoredState()
|
||||
|
||||
def __init__(self, framework: ops.framework.Framework) -> None:
|
||||
super().__init__(framework)
|
||||
self._state.set_default(
|
||||
external_interface=None,
|
||||
bridge_name=None,
|
||||
physnet_name=None,
|
||||
enable_chassis_as_gw=None,
|
||||
)
|
||||
self.framework.observe(
|
||||
self.on.set_network_agents_local_settings_action,
|
||||
self._set_network_agents_local_settings_action,
|
||||
)
|
||||
|
||||
@property
|
||||
def snap_name(self) -> str:
|
||||
"""Snap to install (configurable for dev/testing)."""
|
||||
return str(self.model.config.get("snap-name"))
|
||||
|
||||
@property
|
||||
def snap_channel(self) -> str:
|
||||
"""Channel to track in the Snap Store."""
|
||||
return str(self.model.config.get("snap-channel"))
|
||||
|
||||
def ensure_services_running(self, enable: bool = True) -> None:
|
||||
"""No-op; there are no services to start."""
|
||||
pass
|
||||
|
||||
def stop_services(self, relation: set[str] | None = None) -> None:
|
||||
"""No-op; there are no services to stop."""
|
||||
pass
|
||||
|
||||
def get_relation_handlers(
|
||||
self, handlers: list[sunbeam_rhandlers.RelationHandler] | None = None
|
||||
) -> list[sunbeam_rhandlers.RelationHandler]:
|
||||
"""Return a list of relation handlers used by this charm."""
|
||||
handlers = handlers or []
|
||||
if self.can_add_handler("juju-info", handlers):
|
||||
juju_info = JujuInfoRequiresHandler(
|
||||
self,
|
||||
"juju-info",
|
||||
self.configure_charm,
|
||||
"juju-info" in self.mandatory_relations,
|
||||
)
|
||||
handlers.append(juju_info)
|
||||
return super().get_relation_handlers(handlers)
|
||||
|
||||
def _connect_ovn_chassis(self) -> None:
|
||||
"""Connect the snap ovn-chassis plug to microovn:ovn-chassis."""
|
||||
openstack_network_agents = self.get_snap()
|
||||
|
||||
try:
|
||||
openstack_network_agents.connect(
|
||||
OVN_CHASSIS_PLUG, slot=OVN_CHASSIS_SLOT
|
||||
)
|
||||
logger.info(
|
||||
"Connected microovn:ovn-chassis slot to openstack-network-agents:ovn-chassis plug"
|
||||
)
|
||||
except snap.SnapError as e:
|
||||
logger.error(
|
||||
f"Failed to connect to microovn:ovn-chassis: {e.message}"
|
||||
)
|
||||
raise
|
||||
|
||||
def _validated_network_config(self) -> tuple[str, str, str, bool]:
|
||||
"""Validate and normalize network-related charm config.
|
||||
|
||||
Returns: (iface, bridge, physnet, enable_gw)
|
||||
"""
|
||||
iface = self._state.external_interface
|
||||
bridge = self._state.bridge_name
|
||||
physnet = self._state.physnet_name
|
||||
enable_gw = self._state.enable_chassis_as_gw
|
||||
enable_gw_bool = True if enable_gw is None else bool(enable_gw)
|
||||
|
||||
missing = []
|
||||
if not iface:
|
||||
missing.append("external-interface")
|
||||
if not bridge:
|
||||
missing.append("bridge-name")
|
||||
if not physnet:
|
||||
missing.append("physnet-name")
|
||||
if enable_gw is None:
|
||||
missing.append("enable-chassis-as-gw")
|
||||
|
||||
if missing:
|
||||
raise sunbeam_guard.BlockedExceptionError(
|
||||
f"missing: {', '.join(missing)}"
|
||||
)
|
||||
return str(iface), str(bridge), str(physnet), enable_gw_bool
|
||||
|
||||
def _set_network_agents_local_settings_action(
|
||||
self, event: ops.ActionEvent
|
||||
) -> None:
|
||||
"""Action to set per-unit local settings for provider networking."""
|
||||
params = event.params or {}
|
||||
iface = params.get("external-interface")
|
||||
bridge = params.get("bridge-name")
|
||||
physnet = params.get("physnet-name")
|
||||
enable_gw = params.get("enable-chassis-as-gw")
|
||||
|
||||
missing = [
|
||||
name
|
||||
for name, val in (
|
||||
("external-interface", iface),
|
||||
("bridge-name", bridge),
|
||||
("physnet-name", physnet),
|
||||
("enable-chassis-as-gw", enable_gw),
|
||||
)
|
||||
if val is None or val == ""
|
||||
]
|
||||
if missing:
|
||||
event.fail(f"Missing required params: {', '.join(missing)}")
|
||||
return
|
||||
|
||||
self._state.external_interface = str(iface)
|
||||
self._state.bridge_name = str(bridge)
|
||||
self._state.physnet_name = str(physnet)
|
||||
self._state.enable_chassis_as_gw = bool(enable_gw)
|
||||
|
||||
try:
|
||||
self.configure_charm(event)
|
||||
except Exception as exc:
|
||||
event.fail(str(exc))
|
||||
return
|
||||
event.set_results(
|
||||
{
|
||||
"external-interface": self._state.external_interface,
|
||||
"bridge-name": self._state.bridge_name,
|
||||
"physnet-name": self._state.physnet_name,
|
||||
"enable-chassis-as-gw": self._state.enable_chassis_as_gw,
|
||||
}
|
||||
)
|
||||
|
||||
def configure_snap(self, event: ops.EventBase) -> None:
|
||||
"""Push configuration into the snap (no subprocess)."""
|
||||
self._connect_ovn_chassis()
|
||||
iface, bridge, physnet, enable_gw = self._validated_network_config()
|
||||
self.set_snap_data(
|
||||
{
|
||||
# consumed by the snap’s configure/bridge-setup helper
|
||||
"network.interface": iface,
|
||||
"network.bridge": bridge,
|
||||
"network.physnet": physnet,
|
||||
"network.enable-chassis-as-gw": enable_gw,
|
||||
"settings.debug": bool(
|
||||
self.model.config.get("debug") or False
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: nocover
|
||||
ops.main(OpenstackNetworkAgentsOperatorCharm)
|
15
charms/openstack-network-agents/tests/unit/__init__.py
Normal file
15
charms/openstack-network-agents/tests/unit/__init__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
# Copyright 2025 Canonical Ltd.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Unit tests for openstack-network-agents."""
|
@@ -0,0 +1,135 @@
|
||||
# Copyright 2025 Canonical Ltd.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Define openstack-network-agents tests."""
|
||||
|
||||
from unittest.mock import (
|
||||
MagicMock,
|
||||
patch,
|
||||
)
|
||||
|
||||
import charm
|
||||
import ops
|
||||
import ops_sunbeam.guard as sunbeam_guard
|
||||
import ops_sunbeam.test_utils as test_utils
|
||||
|
||||
|
||||
class _OpenstackNetworkAgentsOperatorCharm(
|
||||
charm.OpenstackNetworkAgentsOperatorCharm
|
||||
):
|
||||
def __init__(self, framework):
|
||||
self.seen = []
|
||||
super().__init__(framework)
|
||||
|
||||
|
||||
class TestOpenstackNetworkAgentsCharm(test_utils.CharmTestCase):
|
||||
"""Tests for Openstack Network Agents charm."""
|
||||
|
||||
PATCHES = []
|
||||
|
||||
def setUp(self):
|
||||
"""Set up the test harness."""
|
||||
super().setUp(charm, self.PATCHES)
|
||||
self.harness = test_utils.get_harness(
|
||||
_OpenstackNetworkAgentsOperatorCharm,
|
||||
container_calls=self.container_calls,
|
||||
)
|
||||
self.addCleanup(self.harness.cleanup)
|
||||
|
||||
def test_validated_network_config_ok(self):
|
||||
"""Test validated network config with all required fields."""
|
||||
self.harness.begin()
|
||||
evt = MagicMock(spec=ops.ActionEvent)
|
||||
evt.params = {
|
||||
"external-interface": "ens10",
|
||||
"bridge-name": "br-ex",
|
||||
"physnet-name": "physnet1",
|
||||
"enable-chassis-as-gw": True,
|
||||
}
|
||||
self.harness.charm._set_network_agents_local_settings_action(evt)
|
||||
iface, bridge, physnet, enable_gw = (
|
||||
self.harness.charm._validated_network_config()
|
||||
)
|
||||
assert iface == "ens10"
|
||||
assert bridge == "br-ex"
|
||||
assert physnet == "physnet1"
|
||||
assert enable_gw is True
|
||||
|
||||
def test_validated_network_config_missing(self):
|
||||
"""Test validated network config with missing fields."""
|
||||
self.harness.begin()
|
||||
with self.assertRaises(sunbeam_guard.BlockedExceptionError) as ctx:
|
||||
self.harness.charm._validated_network_config()
|
||||
msg = str(ctx.exception)
|
||||
assert "physnet-name" in msg
|
||||
assert "enable-chassis-as-gw" in msg
|
||||
|
||||
def test_connect_ovn_chassis_success(self):
|
||||
"""Test _connect_ovn_chassis when microovn is present."""
|
||||
self.harness.begin()
|
||||
mock_snap = MagicMock(name="openstack-network-agents")
|
||||
with patch.object(
|
||||
self.harness.charm, "get_snap", return_value=mock_snap
|
||||
):
|
||||
self.harness.charm._connect_ovn_chassis()
|
||||
mock_snap.connect.assert_called_once_with(
|
||||
charm.OVN_CHASSIS_PLUG, slot=charm.OVN_CHASSIS_SLOT
|
||||
)
|
||||
|
||||
def test_connect_ovn_chassis_errors_out(self):
|
||||
"""Test _connect_ovn_chassis when snap connect raises SnapError."""
|
||||
self.harness.begin()
|
||||
mock_snap = MagicMock(name="openstack-network-agents")
|
||||
mock_snap.connect.side_effect = Exception("boom")
|
||||
|
||||
with patch.object(
|
||||
self.harness.charm, "get_snap", return_value=mock_snap
|
||||
):
|
||||
with self.assertRaises(Exception):
|
||||
self.harness.charm._connect_ovn_chassis()
|
||||
|
||||
def test_configure_snap_sets_snap_data_and_connects(self):
|
||||
"""configure_snap connects ovn-chassis and pushes snap data."""
|
||||
self.harness.begin()
|
||||
evt = MagicMock(spec=ops.ActionEvent)
|
||||
evt.params = {
|
||||
"external-interface": "ens10",
|
||||
"bridge-name": "br-ex",
|
||||
"physnet-name": "physnet1",
|
||||
"enable-chassis-as-gw": True,
|
||||
}
|
||||
self.harness.charm._set_network_agents_local_settings_action(evt)
|
||||
self.harness.update_config({"debug": True})
|
||||
|
||||
mock_snap = MagicMock(name="openstack-network-agents")
|
||||
with patch.object(
|
||||
self.harness.charm, "get_snap", return_value=mock_snap
|
||||
), patch.object(
|
||||
self.harness.charm, "set_snap_data"
|
||||
) as set_snap_data_mock:
|
||||
evt = MagicMock(spec=ops.EventBase)
|
||||
self.harness.charm.configure_snap(evt)
|
||||
|
||||
mock_snap.connect.assert_called_once_with(
|
||||
charm.OVN_CHASSIS_PLUG, slot=charm.OVN_CHASSIS_SLOT
|
||||
)
|
||||
set_snap_data_mock.assert_called_once()
|
||||
(kwargs,), _ = set_snap_data_mock.call_args
|
||||
assert kwargs == {
|
||||
"network.interface": "ens10",
|
||||
"network.bridge": "br-ex",
|
||||
"network.physnet": "physnet1",
|
||||
"network.enable-chassis-as-gw": True,
|
||||
"settings.debug": True,
|
||||
}
|
Reference in New Issue
Block a user