Skip to content

Commit e66ca1a

Browse files
evallespdanpawlik
authored andcommitted
Add multus_attach to ci_multus bridge multus type
We're adding a new variable attach at the networking_mapper to map its value to the output variable multus_attach. This is used then to generate NAD with information about where to attach the multus type bridge, if in a interface or a linux-bridge.
1 parent 23c1cb6 commit e66ca1a

16 files changed

Lines changed: 99 additions & 9 deletions

plugins/module_utils/net_map/networking_definition.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ class SubnetBasedNetworkToolDefinition:
10451045
__FIELD_ROUTES_IPV4 = "routes-v4"
10461046
__FIELD_ROUTES_IPV6 = "routes-v6"
10471047
__FIELD_TYPE = "type"
1048+
__FIELD_ATTACH = "attach"
10481049

10491050
def __init__(
10501051
self,
@@ -1069,6 +1070,7 @@ def __init__(
10691070
self.__ipv4_routes: typing.List[HostNetworkRoute] = []
10701071
self.__ipv6_routes: typing.List[HostNetworkRoute] = []
10711072
self.__type: typing.Optional[str] = None
1073+
self.__attach: typing.Optional[str] = None
10721074

10731075
self.__parse_raw(raw_config)
10741076

@@ -1093,14 +1095,20 @@ def __parse_raw(self, raw_definition: typing.Dict[str, typing.Any]):
10931095
parent_name=self.__object_name,
10941096
alone_field=self.__FIELD_ROUTES,
10951097
)
1096-
10971098
_validate_fields_one_of(
10981099
[
10991100
self.__FIELD_TYPE,
11001101
],
11011102
raw_definition,
11021103
parent_name=self.__object_name,
11031104
)
1105+
_validate_fields_one_of(
1106+
[
1107+
self.__FIELD_ATTACH,
1108+
],
1109+
raw_definition,
1110+
parent_name=self.__object_name,
1111+
)
11041112
self.__parse_raw_range_field(raw_definition, self.__FIELD_RANGES)
11051113
self.__parse_raw_range_field(
11061114
raw_definition, self.__FIELD_RANGES_IPV4, ip_version=4
@@ -1117,6 +1125,7 @@ def __parse_raw(self, raw_definition: typing.Dict[str, typing.Any]):
11171125
raw_definition, self.__FIELD_ROUTES_IPV6, ip_version=6
11181126
)
11191127
self.__parse_raw_type_field(raw_definition, self.__FIELD_TYPE)
1128+
self.__parse_raw_type_attach(raw_definition, self.__FIELD_ATTACH)
11201129

11211130
def __parse_raw_range_field(
11221131
self,
@@ -1215,6 +1224,21 @@ def __parse_raw_type_field(self, raw_definition, field_name: str):
12151224
)
12161225
self.__type = type
12171226

1227+
@property
1228+
def attach(self) -> str:
1229+
"""Where to attach the multus bridge"""
1230+
return self.__attach
1231+
1232+
def __parse_raw_type_attach(self, raw_definition, field_name: str):
1233+
if field_name in raw_definition:
1234+
attach = _validate_parse_field_type(
1235+
field_name,
1236+
raw_definition,
1237+
str,
1238+
parent_name=self.__object_name,
1239+
)
1240+
self.__attach = attach
1241+
12181242

12191243
class MultusNetworkDefinition(SubnetBasedNetworkToolDefinition):
12201244
"""Parses and holds Multus configuration for a given network."""

plugins/module_utils/net_map/networking_env_definitions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class MappedMultusNetworkConfig:
137137
ipv4_routes: IPv4 routes assigned to Multus.
138138
ipv6_routes: IPv6 routes assigned to Multus.
139139
multus_type: The type of the multus network.
140+
multus_attach: The type of the multus network.
140141
141142
"""
142143

@@ -145,6 +146,7 @@ class MappedMultusNetworkConfig:
145146
ipv4_routes: typing.List[MappedIpv4NetworkRoute]
146147
ipv6_routes: typing.List[MappedIpv6NetworkRoute]
147148
multus_type: typing.Optional[str] = None
149+
multus_attach: typing.Optional[str] = None
148150

149151

150152
@dataclasses.dataclass(frozen=True)

plugins/module_utils/net_map/networking_mapper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,14 +679,16 @@ def __build_network_tool_common(
679679
],
680680
]
681681
multus_type = []
682+
multus_attach = []
682683
if tool_type.__name__ == "MappedMultusNetworkConfig":
683684
multus_type.append(tool_net_def.type)
685+
multus_attach.append(tool_net_def.attach)
684686

685687
if any(
686688
route_field in tool_type.__dataclass_fields__
687689
for route_field in ["ipv4_routes", "ipv6_routes"]
688690
):
689-
args_list = args_list + route_args_list + multus_type
691+
args_list = args_list + route_args_list + multus_type + multus_attach
690692
return tool_type(*args_list)
691693

692694

roles/ci_multus/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Creates additional networks in a OCP cluster using NetworkAttachmentDefinition
1111
* `cifmw_ci_multus_ocp_hostname`: (String) The OCP inventory hostname. Used to gather network information specific to those nodes, mostly the interfaces. Defaults to `crc`.
1212
* `cifmw_ci_multus_cniversion`: (String) The CNI specification version used when creating the resource. Defaults to `0.3.1`.
1313
* `cifmw_ci_multus_default_nad_type`: (String) Default NAD type used when not specified by the network configuration. Defaults to `macvlan`. You can select the type of each NAD by "multus_type"
14+
* `cifmw_ci_multus_default_bridge_attach`: (String) Set place to attach the bridge when NAD type is bridge. Defaults to `interface`. You can select the place to attach it by "multus_attach".
1415
* `cifmw_ci_multus_default_nad_ipam_type`: (String) Default NAD IPAM type to be used when not specified by the network configuration. Defaults to `whereabouts`.
1516
* `cifmw_ci_multus_default_nad_ipam_type_ip_version``: (String) Default IP version to use in IPAM config. Defaults to `v4`.
1617
* `cifmw_ci_multus_dryrun`: (Bool) When enabled, tasks that require an OCP environment are skipped. Defaults to `false`.
@@ -37,6 +38,7 @@ cifmw_ci_multus_net_info_patch_1:
3738
- start: 192.168.122.30
3839
end: 192.168.122.70
3940
type: bridge
41+
attach: linux-bridge
4042
```
4143
4244
## Limitations

roles/ci_multus/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cifmw_ci_multus_namespace: "openstack"
2424
cifmw_ci_multus_ocp_hostname: "crc"
2525
cifmw_ci_multus_cniversion: "0.3.1"
2626
cifmw_ci_multus_default_nad_type: "macvlan"
27+
cifmw_ci_multus_default_bridge_attach: "interface"
2728
cifmw_ci_multus_default_nad_ipam_type: "whereabouts"
2829
cifmw_ci_multus_default_nad_ipam_type_ip_version: "v4"
2930
# Input configuration for ci_multus role

roles/ci_multus/molecule/default/molecule.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ provisioner:
1515
_expected_multus_networks:
1616
- default
1717
- patchnetwork
18+
- bridge-to-linux-bridge
1819
cifmw_ci_multus_net_info_patch_1:
1920
patchnetwork:
2021
gw_v4: 192.168.122.1
@@ -27,6 +28,19 @@ provisioner:
2728
- start: 192.168.122.30
2829
end: 192.168.122.70
2930
multus_type: macvlan
31+
cifmw_ci_multus_net_info_patch_2:
32+
bridge-to-linux-bridge:
33+
gw_v4: 192.168.122.1
34+
network_name: bridge-to-linux-bridge
35+
network_v4: 192.168.122.0/24
36+
interface_name: eth1
37+
tools:
38+
multus:
39+
ipv4_ranges:
40+
- start: 192.168.122.30
41+
end: 192.168.122.70
42+
multus_type: bridge
43+
multus_attach: linux-bridge
3044

3145
cifmw_path: "{{ ansible_user_dir }}/.crc/bin:{{ ansible_user_dir }}/.crc/bin/oc:{{ ansible_user_dir }}/bin:{{ ansible_env.PATH }}"
3246
cifmw_openshift_kubeconfig: "{{ ansible_user_dir }}/.crc/machines/crc/kubeconfig"

roles/ci_multus/molecule/default/nads_output.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
---
22
apiVersion: k8s.cni.cncf.io/v1
33
kind: NetworkAttachmentDefinition
4+
metadata:
5+
labels:
6+
osp/net: bridge-to-linux-bridge
7+
name: bridge-to-linux-bridge
8+
namespace: openstack
9+
spec:
10+
config: |
11+
{
12+
"cniVersion": "0.3.1",
13+
"name": "bridge-to-linux-bridge",
14+
"type": "bridge",
15+
"bridge": "bridge-to-linux-bridge",
16+
"ipam": {
17+
"type": "whereabouts",
18+
"range": "192.168.122.0/24",
19+
"range_start": "192.168.122.30",
20+
"range_end": "192.168.122.70"
21+
}
22+
}
23+
---
24+
apiVersion: k8s.cni.cncf.io/v1
25+
kind: NetworkAttachmentDefinition
426
metadata:
527
labels:
628
osp/net: default

roles/ci_multus/molecule/default/verify_crc.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- name: Verify CRC
1818
hosts: all
1919
gather_facts: true
20+
vars:
21+
_ci_multus_expected_spec: "{{ lookup('file', 'nads_output.yml', rstrip=True) | from_yaml_all | map(attribute='spec.config') }}"
2022
tasks:
2123
- name: Include default vars
2224
ansible.builtin.include_vars:
@@ -43,11 +45,16 @@
4345
map(attribute='spec.config')
4446
}}
4547
46-
- name: Assert expected Network Attachment Definitions content spec with the expected
47-
vars:
48-
_ci_multus_expected_spec: "{{ lookup('file', 'nads_output.yml', rstrip=True) | from_yaml_all | map(attribute='spec.config') }}"
48+
- name: Ensure both lists have the same length
4949
ansible.builtin.assert:
50-
that: _ci_multus_out_spec == _ci_multus_expected_spec
50+
that:
51+
- _ci_multus_out_spec | length == _ci_multus_expected_spec | length
52+
53+
- name: Compare each corresponding element in the lists
54+
ansible.builtin.assert:
55+
that:
56+
- (item.0 | replace('\n', '')) == (item.1 | replace('\n', ''))
57+
loop: "{{ _ci_multus_out_spec | zip(_ci_multus_expected_spec) | list }}"
5158

5259
- name: Create a test pod to attach a network
5360
kubernetes.core.k8s:

roles/ci_multus/molecule/resources/vars/shared_vars.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,4 @@ cifmw_ci_multus_deny_list:
6363
cifmw_ci_multus_allow_list:
6464
- default
6565
- patchnetwork
66+
- bridge-to-linux-bridge

roles/ci_multus/templates/nad.yml.j2

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
{% else %}
55
{% set multus_type = cifmw_ci_multus_default_nad_type %}
66
{% endif %}
7+
{% if network_details.tools.get('multus', {}).get('multus_attach', None) %}
8+
{% set multus_attach = network_details.tools.multus.multus_attach %}
9+
{% else %}
10+
{% set multus_attach = cifmw_ci_multus_default_bridge_attach %}
11+
{% endif %}
712
---
813
apiVersion: k8s.cni.cncf.io/v1
914
kind: NetworkAttachmentDefinition
@@ -22,7 +27,11 @@ spec:
2227
"master": "{{ network_details.interface_name }}",
2328
{% endif %}
2429
{% if multus_type == "bridge" %}
30+
{% if multus_attach == "interface" %}
2531
"bridge": "{{ network_details.interface_name }}",
32+
{% elif multus_attach == "linux-bridge" %}
33+
"bridge": "{{ network_name }}",
34+
{% endif %}
2635
{% endif %}
2736
"ipam": {
2837
"type": "{{ cifmw_ci_multus_default_nad_ipam_type }}",

0 commit comments

Comments
 (0)