Skip to content

Commit

Permalink
Add UDN layer3 class (#2184)
Browse files Browse the repository at this point in the history
* Add UDN layer3 class

* Move constants from global scope to class scope

* Remove redundant function call

* Fix key error

* Add missing value argument validation

* Use dictionary access instead of class attributes

* Make parameter optional in a method

* Correct the docstring for the role parameter

* Improve error message for missing required arguments

* Refactor to follow conventions

* Revert "Refactor to follow conventions"

This reverts commit 5d4b942.

* Refactor type annotation

* Refactor condition statement

* Refactor docstring for layer3 subnets
  • Loading branch information
sbahar619 authored Oct 31, 2024
1 parent aeae8fb commit 42350f2
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions ocp_resources/user_defined_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,64 @@ def to_dict(self) -> None:

if self.ipam_lifecycle:
_layer2["ipamLifecycle"] = self.ipam_lifecycle


class Layer3UserDefinedNetwork(UserDefinedNetwork):
"""
UserDefinedNetwork layer3 object.
API reference:
https://ovn-kubernetes.io/api-reference/userdefinednetwork-api-spec/#layer3config
"""

LAYER3: str = "Layer3"

def __init__(
self,
role: Optional[str] = None,
mtu: Optional[int] = None,
subnets: Optional[List[Dict[str, Any]]] = None,
join_subnets: Optional[List[str]] = None,
**kwargs,
):
"""
Create and manage UserDefinedNetwork with layer3 configuration
Args:
role (Optional[str]): role describes the network role in the pod.
mtu (Optional[int]): mtu is the maximum transmission unit for a network.
subnets (Optional[List[Dict]]): subnets are used for the pod network across the cluster, each expecting:
- `cidr` (str): IP range in CIDR notation.
- `hostSubnet` (Optional[int]): Host-specific subnet.
API reference:
https://ovn-kubernetes.io/api-reference/userdefinednetwork-api-spec/#layer3subnet
join_subnets (Optional[List[str]]): join_subnets are used inside the OVN network topology.
"""
super().__init__(
topology=self.LAYER3,
**kwargs,
)
self.role = role
self.mtu = mtu
self.subnets = subnets
self.join_subnets = join_subnets

def to_dict(self) -> None:
super().to_dict()
if not self.kind_dict and not self.yaml_file:
if not self.role:
raise MissingRequiredArgumentError(argument="role")
if not self.subnets:
raise MissingRequiredArgumentError(argument="subnets")

self.res["spec"][self.LAYER3.lower()] = {"role": self.role}
_layer3 = self.res["spec"][self.LAYER3.lower()]

if self.mtu:
_layer3["mtu"] = self.mtu

if self.join_subnets:
_layer3["joinSubnets"] = self.join_subnets

if self.subnets:
_layer3["subnets"] = self.subnets

0 comments on commit 42350f2

Please sign in to comment.