Skip to content

Commit c0863fe

Browse files
committed
DNM
1 parent 902e85d commit c0863fe

16 files changed

+194
-12
lines changed

plugins/module_utils/net_map/networking_definition.py

+25
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,7 @@ class SubnetBasedNetworkToolDefinition:
10441044
__FIELD_ROUTES = "routes"
10451045
__FIELD_ROUTES_IPV4 = "routes-v4"
10461046
__FIELD_ROUTES_IPV6 = "routes-v6"
1047+
__FIELD_TYPE = "type"
10471048

10481049
def __init__(
10491050
self,
@@ -1067,6 +1068,7 @@ def __init__(
10671068
self.__ipv6_ranges: typing.List[HostNetworkRange] = []
10681069
self.__ipv4_routes: typing.List[HostNetworkRoute] = []
10691070
self.__ipv6_routes: typing.List[HostNetworkRoute] = []
1071+
self.__type: typing.Optional[str] = None
10701072

10711073
self.__parse_raw(raw_config)
10721074

@@ -1092,6 +1094,13 @@ def __parse_raw(self, raw_definition: typing.Dict[str, typing.Any]):
10921094
alone_field=self.__FIELD_ROUTES,
10931095
)
10941096

1097+
_validate_fields_one_of(
1098+
[
1099+
self.__FIELD_TYPE,
1100+
],
1101+
raw_definition,
1102+
parent_name=self.__object_name,
1103+
)
10951104
self.__parse_raw_range_field(raw_definition, self.__FIELD_RANGES)
10961105
self.__parse_raw_range_field(
10971106
raw_definition, self.__FIELD_RANGES_IPV4, ip_version=4
@@ -1107,6 +1116,7 @@ def __parse_raw(self, raw_definition: typing.Dict[str, typing.Any]):
11071116
self.__parse_raw_route_field(
11081117
raw_definition, self.__FIELD_ROUTES_IPV6, ip_version=6
11091118
)
1119+
self.__parse_raw_type_field(raw_definition, self.__FIELD_TYPE)
11101120

11111121
def __parse_raw_range_field(
11121122
self,
@@ -1190,6 +1200,21 @@ def __parse_raw_route_field(
11901200
if ipv6_route:
11911201
self.__ipv6_routes.append(ipv6_route)
11921202

1203+
@property
1204+
def type(self) -> str:
1205+
"""The type of the tool for multus."""
1206+
return self.__type
1207+
1208+
def __parse_raw_type_field(self, raw_definition, field_name: str):
1209+
if field_name in raw_definition:
1210+
type = _validate_parse_field_type(
1211+
field_name,
1212+
raw_definition,
1213+
str,
1214+
parent_name=self.__object_name,
1215+
)
1216+
self.__type = type
1217+
11931218

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

plugins/module_utils/net_map/networking_env_definitions.py

+2
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,15 @@ class MappedMultusNetworkConfig:
136136
ipv6_ranges: IPv6 ranges assigned to Multus.
137137
ipv4_routes: IPv4 routes assigned to Multus.
138138
ipv6_routes: IPv6 routes assigned to Multus.
139+
multus_type: The type of the multus network.
139140
140141
"""
141142

142143
ipv4_ranges: typing.List[MappedIpv4NetworkRange]
143144
ipv6_ranges: typing.List[MappedIpv6NetworkRange]
144145
ipv4_routes: typing.List[MappedIpv4NetworkRoute]
145146
ipv6_routes: typing.List[MappedIpv6NetworkRoute]
147+
multus_type: typing.Optional[str] = None
146148

147149

148150
@dataclasses.dataclass(frozen=True)

plugins/module_utils/net_map/networking_mapper.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -678,12 +678,15 @@ def __build_network_tool_common(
678678
for ip_route in tool_net_def.routes_ipv6
679679
],
680680
]
681+
multus_type = []
682+
if tool_type.__name__ == "MappedMultusNetworkConfig":
683+
multus_type.append(tool_net_def.type)
681684

682685
if any(
683686
route_field in tool_type.__dataclass_fields__
684687
for route_field in ["ipv4_routes", "ipv6_routes"]
685688
):
686-
args_list = args_list + route_args_list
689+
args_list = args_list + route_args_list + multus_type
687690
return tool_type(*args_list)
688691

689692

roles/ci_multus/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Creates additional networks in a OCP cluster using NetworkAttachmentDefinition
1010
* `cifmw_ci_multus_namespace`: (String) The namespace where OCP resources will be installed. Defaults to `openstack`.
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`.
13-
* `cifmw_ci_multus_default_nad_type`: (String) Default NAD type used when not specified by the network configuration. Defaults to `macvlan`.
13+
* `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 "type"
1414
* `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`.
1515
* `cifmw_ci_multus_default_nad_ipam_type_ip_version``: (String) Default IP version to use in IPAM config. Defaults to `v4`.
1616
* `cifmw_ci_multus_dryrun`: (Bool) When enabled, tasks that require an OCP environment are skipped. Defaults to `false`.
@@ -36,6 +36,7 @@ cifmw_ci_multus_net_info_patch_1:
3636
ipv4_ranges:
3737
- start: 192.168.122.30
3838
end: 192.168.122.70
39+
type: bridge
3940
```
4041
4142
## Limitations
@@ -70,6 +71,7 @@ cifmw_ci_multus_net_info_patch_1:
7071
ipv4_ranges:
7172
- start: 192.168.122.30
7273
end: 192.168.122.70
74+
type: macvlan
7375
ansible.builtin.include_role:
7476
name: "ci_multus"
7577
```

roles/ci_multus/molecule/default/molecule.yml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ provisioner:
2626
ipv4_ranges:
2727
- start: 192.168.122.30
2828
end: 192.168.122.70
29+
type: macvlan
2930

3031
cifmw_path: "{{ ansible_user_dir }}/.crc/bin:{{ ansible_user_dir }}/.crc/bin/oc:{{ ansible_user_dir }}/bin:{{ ansible_env.PATH }}"
3132
cifmw_openshift_kubeconfig: "{{ ansible_user_dir }}/.crc/machines/crc/kubeconfig"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
apiVersion: k8s.cni.cncf.io/v1
3+
kind: NetworkAttachmentDefinition
4+
metadata:
5+
labels:
6+
osp/net: default
7+
name: default
8+
namespace: openstack
9+
spec:
10+
config: |
11+
{
12+
"cniVersion": "0.3.1",
13+
"name": "default",
14+
"type": "macvlan",
15+
"master": "eth1",
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
26+
metadata:
27+
labels:
28+
osp/net: patchnetwork
29+
name: patchnetwork
30+
namespace: openstack
31+
spec:
32+
config: |
33+
{
34+
"cniVersion": "0.3.1",
35+
"name": "patchnetwork",
36+
"type": "macvlan",
37+
"master": "eth2",
38+
"ipam": {
39+
"type": "whereabouts",
40+
"range": "192.168.122.0/24",
41+
"range_start": "192.168.122.30",
42+
"range_end": "192.168.122.70"
43+
}
44+
}

roles/ci_multus/molecule/default/verify_crc.yml

+20
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@
3535
(_ci_multus_molecule_nads_out is failed) or
3636
(_ci_multus_molecule_nads_out.resources | length == 0)
3737
38+
- name: Fetch file content
39+
ansible.builtin.slurp:
40+
path: "nads_output.yml"
41+
register: _ci_multus_expected
42+
43+
- name: Set _ci_multus_expected variable
44+
ansible.builtin.set_fact:
45+
_ci_multus_expected: >-
46+
{{
47+
_ci_multus_expected.content |
48+
b64decode |
49+
from_yaml_all
50+
}}
51+
52+
- name: Assert expected Network Attachment Definitions content with the expected
53+
ansible.builtin.assert:
54+
that:
55+
- _ci_multus_molecule_nads_out == _ci_multus_expected
56+
quiet: true
57+
3858
- name: Create a test pod to attach a network
3959
kubernetes.core.k8s:
4060
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"

roles/ci_multus/molecule/local/molecule.yml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ provisioner:
2626
ipv4_ranges:
2727
- start: 192.168.122.30
2828
end: 192.168.122.70
29+
type: macvlan
2930

3031
prerun: false
3132
scenario:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
apiVersion: k8s.cni.cncf.io/v1
3+
kind: NetworkAttachmentDefinition
4+
metadata:
5+
labels:
6+
osp/net: default
7+
name: default
8+
namespace: openstack
9+
spec:
10+
config: |
11+
{
12+
"cniVersion": "0.3.1",
13+
"name": "default",
14+
"type": "macvlan",
15+
"master": "eth1",
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
26+
metadata:
27+
labels:
28+
osp/net: patchnetwork
29+
name: patchnetwork
30+
namespace: openstack
31+
spec:
32+
config: |
33+
{
34+
"cniVersion": "0.3.1",
35+
"name": "patchnetwork",
36+
"type": "macvlan",
37+
"master": "eth2",
38+
"ipam": {
39+
"type": "whereabouts",
40+
"range": "192.168.122.0/24",
41+
"range_start": "192.168.122.30",
42+
"range_end": "192.168.122.70"
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
apiVersion: k8s.cni.cncf.io/v1
3+
kind: NetworkAttachmentDefinition
4+
metadata:
5+
labels:
6+
osp/net: default
7+
name: default
8+
namespace: openstack
9+
spec:
10+
config: |
11+
{
12+
"cniVersion": "0.3.1",
13+
"name": "default",
14+
"type": "macvlan",
15+
"master": "eth1",
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+
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ cifmw_networking_env_definition:
2222
default:
2323
interface_name: "eth1"
2424
network_name: default
25+
patchnetwork:
26+
interface_name: "eth2"
27+
network_name: default
2528
networks:
2629
default:
2730
gw_v4: 192.168.122.1
@@ -32,6 +35,7 @@ cifmw_networking_env_definition:
3235
ipv4_ranges:
3336
- start: 192.168.122.30
3437
end: 192.168.122.70
38+
type: bridge
3539
deny_network:
3640
gw_v4: 192.168.122.1
3741
network_name: deny_network
@@ -41,6 +45,7 @@ cifmw_networking_env_definition:
4145
ipv4_ranges:
4246
- start: 192.168.122.30
4347
end: 192.168.122.70
48+
type: bridge
4449
not_allowed_network:
4550
gw_v4: 192.168.122.1
4651
network_name: not_allowed_network
@@ -50,6 +55,7 @@ cifmw_networking_env_definition:
5055
ipv4_ranges:
5156
- start: 192.168.122.30
5257
end: 192.168.122.70
58+
type: bridge
5359
no_multus_network:
5460
gw_v4: 192.168.122.1
5561
network_name: patchnetwork

roles/ci_multus/templates/nad.yml.j2

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{% for network_name, network_details in _cifmw_ci_multus_net_info.items() %}
2+
{% if network_details.tools.get('multus', {}).get('multus_type', None) %}
3+
{% set multus_type = network_details.tools.multus.multus_type %}
4+
{% else %}
5+
{% set multus_type = cifmw_ci_multus_default_nad_type %}
6+
{% endif %}
27
---
38
apiVersion: k8s.cni.cncf.io/v1
49
kind: NetworkAttachmentDefinition
@@ -12,12 +17,11 @@ spec:
1217
{
1318
"cniVersion": "{{ cifmw_ci_multus_cniversion }}",
1419
"name": "{{ network_name }}",
15-
{% if cifmw_ci_multus_default_nad_type == "macvlan" %}
16-
"type": "macvlan",
20+
"type": "{{ multus_type }}",
21+
{% if multus_type == "macvlan" %}
1722
"master": "{{ network_details.interface_name }}",
1823
{% endif %}
19-
{% if cifmw_ci_multus_default_nad_type == "bridge" %}
20-
"type": "bridge",
24+
{% if multus_type == "bridge" %}
2125
"bridge": "{{ network_details.interface_name }}",
2226
{% endif %}
2327
"ipam": {

tests/unit/module_utils/test_files/networking-definition-valid-all-tools-full-map-out.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
],
2929
"ipv6_ranges": [],
3030
"ipv4_routes": [],
31-
"ipv6_routes": []
31+
"ipv6_routes": [],
32+
"multus_type": "macvlan"
3233
},
3334
"netconfig": {
3435
"ipv4_ranges": [
@@ -88,7 +89,8 @@
8889
"gateway": "192.168.122.1"
8990
}
9091
],
91-
"ipv6_routes": []
92+
"ipv6_routes": [],
93+
"multus_type": "bridge"
9294
},
9395
"netconfig": {
9496
"ipv4_ranges": [

tests/unit/module_utils/test_files/networking-definition-valid-all-tools-networks-out.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
],
2828
"ipv6_ranges": [],
2929
"ipv4_routes": [],
30-
"ipv6_routes": []
30+
"ipv6_routes": [],
31+
"multus_type": "macvlan"
3132
},
3233
"netconfig": {
3334
"ipv4_ranges": [
@@ -87,7 +88,8 @@
8788
"gateway": "192.168.122.1"
8889
}
8990
],
90-
"ipv6_routes": []
91+
"ipv6_routes": [],
92+
"multus_type": "bridge"
9193
},
9294
"netconfig": {
9395
"ipv4_ranges": [

tests/unit/module_utils/test_files/networking-definition-valid-all-tools-partial-map-out.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
],
2929
"ipv6_ranges": [],
3030
"ipv4_routes": [],
31-
"ipv6_routes": []
31+
"ipv6_routes": [],
32+
"multus_type": "macvlan"
3233
},
3334
"netconfig": {
3435
"ipv4_ranges": [
@@ -88,7 +89,8 @@
8889
"gateway": "192.168.122.1"
8990
}
9091
],
91-
"ipv6_routes": []
92+
"ipv6_routes": [],
93+
"multus_type": "bridge"
9294
},
9395
"netconfig": {
9496
"ipv4_ranges": [

0 commit comments

Comments
 (0)