Coverage for ocp_resources/network_attachment_definition.py: 0%
85 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-30 10:48 +0200
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-30 10:48 +0200
1import json
3from ocp_resources.resource import MissingRequiredArgumentError, NamespacedResource
5DEFAULT_CNI_VERSION = "0.3.1"
8class NetworkAttachmentDefinition(NamespacedResource):
9 """
10 NetworkAttachmentDefinition object.
11 """
13 api_group = NamespacedResource.ApiGroup.K8S_CNI_CNCF_IO
14 resource_name = None
16 def __init__(
17 self,
18 name=None,
19 namespace=None,
20 client=None,
21 cni_type=None,
22 cni_version=DEFAULT_CNI_VERSION,
23 config=None,
24 *args,
25 **kwargs,
26 ):
27 """
28 Create and manage NetworkAttachmentDefinition
30 Args:
31 name (str): Name of the NetworkAttachmentDefinition.
32 namespace (str): Namespace of the NetworkAttachmentDefinition
33 client: (DynamicClient): DynamicClient to use.
34 cni_type (str): NetworkAttachmentDefinition CNI.
35 cni_version (str): NetworkAttachmentDefinition CNI version.
36 config (dict): NetworkAttachmentDefinition spec["config"] to use,
37 if config is None basic config will be created.
38 """
39 super().__init__(
40 name=name,
41 namespace=namespace,
42 client=client,
43 *args,
44 **kwargs,
45 )
46 self.cni_type = cni_type
47 self.cni_version = cni_version
48 self.config = config
50 def to_dict(self) -> None:
51 super().to_dict()
52 if not self.kind_dict and not self.yaml_file:
53 if self.resource_name is not None:
54 self.res["metadata"]["annotations"] = {
55 f"{NamespacedResource.ApiGroup.K8S_V1_CNI_CNCF_IO}/resourceName": (self.resource_name)
56 }
57 self.res["spec"] = {}
58 if self.config:
59 self.res["spec"]["config"] = self.config
60 else:
61 self.res["spec"]["config"] = {
62 "cniVersion": self.cni_version,
63 "type": self.cni_type,
64 }
67class BridgeNetworkAttachmentDefinition(NetworkAttachmentDefinition):
68 def __init__(
69 self,
70 name,
71 namespace,
72 bridge_name,
73 cni_type,
74 cni_version=DEFAULT_CNI_VERSION,
75 vlan=None,
76 client=None,
77 mtu=None,
78 macspoofchk=None,
79 teardown=True,
80 old_nad_format=False,
81 add_resource_name=True,
82 dry_run=None,
83 ):
84 super().__init__(
85 name=name,
86 namespace=namespace,
87 client=client,
88 teardown=teardown,
89 dry_run=dry_run,
90 cni_version=cni_version,
91 cni_type=cni_type,
92 )
93 self.old_nad_format = old_nad_format
94 self.bridge_name = bridge_name
95 self.vlan = vlan
96 self.mtu = mtu
97 self.macspoofchk = macspoofchk
98 self.add_resource_name = add_resource_name
100 def to_dict(self) -> None:
101 super().to_dict()
102 spec_config = self.res["spec"]["config"]
103 spec_config["name"] = self.bridge_name
104 spec_config["bridge"] = self.bridge_name
106 if self.mtu:
107 spec_config["mtu"] = self.mtu
109 if self.vlan:
110 spec_config["vlan"] = self.vlan
112 if self.macspoofchk:
113 spec_config["macspoofchk"] = self.macspoofchk
115 self.res["spec"]["config"] = spec_config
118class LinuxBridgeNetworkAttachmentDefinition(BridgeNetworkAttachmentDefinition):
119 def __init__(
120 self,
121 name,
122 namespace,
123 bridge_name,
124 cni_type="cnv-bridge",
125 cni_version=DEFAULT_CNI_VERSION,
126 vlan=None,
127 client=None,
128 mtu=None,
129 tuning_type=None,
130 teardown=True,
131 macspoofchk=None,
132 add_resource_name=True,
133 dry_run=None,
134 ):
135 super().__init__(
136 name=name,
137 namespace=namespace,
138 bridge_name=bridge_name,
139 cni_type=cni_type,
140 vlan=vlan,
141 client=client,
142 mtu=mtu,
143 teardown=teardown,
144 macspoofchk=macspoofchk,
145 add_resource_name=add_resource_name,
146 dry_run=dry_run,
147 cni_version=cni_version,
148 )
149 self.tuning_type = tuning_type
151 def to_dict(self) -> None:
152 super().to_dict()
153 if self.tuning_type:
154 self.old_nad_format = True
155 self.res["spec"]["config"].setdefault("plugins", []).append({"type": self.tuning_type})
157 self.res["spec"]["config"] = json.dumps(self.res["spec"]["config"])
159 @property
160 def resource_name(self):
161 if self.add_resource_name:
162 return f"bridge.network.kubevirt.io/{self.bridge_name}"
165class OvsBridgeNetworkAttachmentDefinition(BridgeNetworkAttachmentDefinition):
166 def __init__(
167 self,
168 name,
169 namespace,
170 bridge_name,
171 vlan=None,
172 client=None,
173 mtu=None,
174 teardown=True,
175 dry_run=None,
176 cni_version=DEFAULT_CNI_VERSION,
177 ):
178 super().__init__(
179 name=name,
180 namespace=namespace,
181 bridge_name=bridge_name,
182 cni_type="ovs",
183 vlan=vlan,
184 client=client,
185 mtu=mtu,
186 teardown=teardown,
187 dry_run=dry_run,
188 cni_version=cni_version,
189 )
191 def to_dict(self) -> None:
192 super().to_dict()
193 self.res["spec"]["config"] = json.dumps(self.res["spec"]["config"])
195 @property
196 def resource_name(self):
197 return f"ovs-cni.network.kubevirt.io/{self.bridge_name}"
200class OVNOverlayNetworkAttachmentDefinition(NetworkAttachmentDefinition):
201 def __init__(
202 self,
203 network_name=None,
204 topology=None,
205 vlan=None,
206 mtu=None,
207 **kwargs,
208 ):
209 """
210 Create and manage an OVN k8s overlay NetworkAttachmentDefinition (a switched, layer 2, topology network).
212 API reference:
213 https://docs.openshift.com/container-platform/4.14/networking/multiple_networks/configuring-additional-network.html#configuration-ovnk-additional-networks_configuring-additional-network
215 Args:
216 network_name (str, optional): The name of the network, used to connect
217 resources created in different namespaces to the same network.
218 vlan (int, optional): A vlan tag ID that will be assigned to traffic from this
219 additional network.
220 mtu (str, optional): The maximum transmission unit (MTU).
221 topology (str): The secondary network topology to be created.
222 """
223 super().__init__(
224 cni_type="ovn-k8s-cni-overlay",
225 **kwargs,
226 )
227 self.network_name = network_name
228 self.topology = topology
229 self.vlan = vlan
230 self.mtu = mtu
232 def to_dict(self) -> None:
233 super().to_dict()
234 if not self.kind_dict and not self.yaml_file:
235 if not self.network_name and not self.topology:
236 raise MissingRequiredArgumentError(argument="'network_name' and 'topology'")
238 spec_config = self.res["spec"]["config"]
239 if self.vlan:
240 spec_config["vlanID"] = self.vlan
241 if self.mtu:
242 spec_config["mtu"] = self.mtu
243 spec_config["name"] = self.network_name
244 spec_config["topology"] = self.topology
245 spec_config["netAttachDefName"] = f"{self.namespace}/{self.name}"
246 self.res["spec"]["config"] = json.dumps(spec_config)