Skip to content

Commit 7a983c1

Browse files
authored
Add Layer2 topology class (#2145)
* Add Layer2 topology class * Apply pre-commit * Refactor to use attribute validation convention Signed-off-by: Shahaf Bahar <[email protected]> * Remove client attribute * Add type annotation * Refactor dictionary initialization * Add missing value argument validation * Add layer3 constant * Delete redundant arguments * Delete redundant function * Refactor condition statement * Refactor dictionary initialization * Delete redundant if statement Signed-off-by: Shahaf Bahar <[email protected]> * Use constant instead of a class * Revert "Delete redundant if statement" This reverts commit bee20d6. * Move constant from global scope to class scope * Revert "Delete redundant if statement" This reverts commit bee20d6. * Remove redundant condition statement * Simplify variable initialization by consolidating field assignment --------- Signed-off-by: Shahaf Bahar <[email protected]>
1 parent 8ff071b commit 7a983c1

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

ocp_resources/user_defined_network.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import Any, Dict, Optional
44
from ocp_resources.resource import NamespacedResource, MissingRequiredArgumentError
5+
from typing import List
56

67

78
class UserDefinedNetwork(NamespacedResource):
@@ -66,3 +67,64 @@ def to_dict(self) -> None:
6667
_spec["localNet"] = self.local_net
6768

6869
# End of generated code
70+
71+
72+
class Layer2UserDefinedNetwork(UserDefinedNetwork):
73+
"""
74+
UserDefinedNetwork layer2 object.
75+
76+
API reference:
77+
https://ovn-kubernetes.io/api-reference/userdefinednetwork-api-spec/#layer2config
78+
"""
79+
80+
LAYER2: str = "Layer2"
81+
82+
def __init__(
83+
self,
84+
role: Optional[str] = None,
85+
mtu: Optional[int] = None,
86+
subnets: Optional[List[str]] = None,
87+
join_subnets: Optional[List[str]] = None,
88+
ipam_lifecycle: Optional[str] = None,
89+
**kwargs,
90+
):
91+
"""
92+
Create and manage UserDefinedNetwork with layer2 configuration
93+
94+
Args:
95+
role (Optional[str]): role describes the network role in the pod.
96+
mtu (Optional[int]): mtu is the maximum transmission unit for a network.
97+
subnets (Optional[List[str]]): subnets are used for the pod network across the cluster.
98+
join_subnets (Optional[List[str]]): join_subnets are used inside the OVN network topology.
99+
ipam_lifecycle (Optional[str]): ipam_lifecycle controls IP addresses management lifecycle.
100+
"""
101+
super().__init__(
102+
topology=self.LAYER2,
103+
**kwargs,
104+
)
105+
self.role = role
106+
self.mtu = mtu
107+
self.subnets = subnets
108+
self.join_subnets = join_subnets
109+
self.ipam_lifecycle = ipam_lifecycle
110+
111+
def to_dict(self) -> None:
112+
super().to_dict()
113+
if not self.kind_dict and not self.yaml_file:
114+
if not self.role:
115+
raise MissingRequiredArgumentError(argument="role")
116+
117+
self.res["spec"][self.LAYER2.lower()] = {"role": self.role}
118+
_layer2 = self.res["spec"][self.LAYER2.lower()]
119+
120+
if self.mtu:
121+
_layer2["mtu"] = self.mtu
122+
123+
if self.subnets:
124+
_layer2["subnets"] = self.subnets
125+
126+
if self.join_subnets:
127+
_layer2["joinSubnets"] = self.join_subnets
128+
129+
if self.ipam_lifecycle:
130+
_layer2["ipamLifecycle"] = self.ipam_lifecycle

0 commit comments

Comments
 (0)