Skip to content

Commit 88bb724

Browse files
evallespbshewale
authored andcommitted
Add multus_type to cifmw_networking_env_definition variable
We now can select the multus_type between "bridge" and macvlan" so ci_multus would render each NetworkAttachmentDefinition appropiate without the assumption that each NAD has the same multus type defined at cifmw_ci_multus_default_nad_type. For a given NAD, if there's no multus_type variable, then it'd take (as previously) the default one. For adding the multus_type to cifmw_networking_env_definition we need to add the argument "type" for the input of networking_mapper.
1 parent a3b2b78 commit 88bb724

17 files changed

+122
-24
lines changed

.ansible-lint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ exclude_paths:
2020
- roles/kustomize_deploy/molecule/flexible_loop/files/networking-environment-definition.yml # Generated
2121
- roles/kustomize_deploy/molecule/flexible_loop/prepare.yml # import_playbook
2222
- roles/*/molecule/*/side_effect.yml # syntax-check[empty-playbook] https://github.com/ansible/molecule/issues/3617
23+
- roles/ci_multus/molecule/*/nads_output.yml # internal-error due to "---" characters
2324
strict: true
2425
quiet: false
2526
verbosity: 1

plugins/module_utils/net_map/networking_definition.py

Lines changed: 25 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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 "multus_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/converge.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,6 @@
4444
ansible.builtin.include_vars:
4545
file: ../resources/vars/shared_vars.yml
4646

47-
- name: Override interface name in cifmw_networking_env_definition
48-
vars:
49-
_cifmw_networking_env_definition_patch:
50-
instances:
51-
crc:
52-
networks:
53-
default:
54-
interface_name: "{{ hostvars.crc.ansible_default_ipv4.interface }}"
55-
ansible.builtin.set_fact:
56-
cifmw_networking_env_definition: "{{ cifmw_networking_env_definition | combine(_cifmw_networking_env_definition_patch, recursive=True) }}"
57-
5847
- name: Call ci_multus role
5948
ansible.builtin.include_role:
6049
name: "ci_multus"

roles/ci_multus/molecule/default/molecule.yml

Lines changed: 1 addition & 0 deletions
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+
multus_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"
Lines changed: 44 additions & 0 deletions
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": "bridge",
15+
"bridge": "eth0",
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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@
3535
(_ci_multus_molecule_nads_out is failed) or
3636
(_ci_multus_molecule_nads_out.resources | length == 0)
3737
38+
- name: Store output spec
39+
ansible.builtin.set_fact:
40+
_ci_multus_out_spec: >-
41+
{{
42+
_ci_multus_molecule_nads_out.resources |
43+
map(attribute='spec.config')
44+
}}
45+
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') }}"
49+
ansible.builtin.assert:
50+
that: _ci_multus_out_spec == _ci_multus_expected_spec
51+
3852
- name: Create a test pod to attach a network
3953
kubernetes.core.k8s:
4054
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cifmw_networking_env_definition:
2020
name: crc
2121
networks:
2222
default:
23-
interface_name: "eth1"
23+
interface_name: "eth0"
2424
network_name: default
2525
networks:
2626
default:
@@ -32,6 +32,7 @@ cifmw_networking_env_definition:
3232
ipv4_ranges:
3333
- start: 192.168.122.30
3434
end: 192.168.122.70
35+
multus_type: bridge
3536
deny_network:
3637
gw_v4: 192.168.122.1
3738
network_name: deny_network
@@ -41,6 +42,7 @@ cifmw_networking_env_definition:
4142
ipv4_ranges:
4243
- start: 192.168.122.30
4344
end: 192.168.122.70
45+
multus_type: bridge
4446
not_allowed_network:
4547
gw_v4: 192.168.122.1
4648
network_name: not_allowed_network
@@ -50,6 +52,7 @@ cifmw_networking_env_definition:
5052
ipv4_ranges:
5153
- start: 192.168.122.30
5254
end: 192.168.122.70
55+
multus_type: bridge
5356
no_multus_network:
5457
gw_v4: 192.168.122.1
5558
network_name: patchnetwork

0 commit comments

Comments
 (0)