-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Copyright (c) 2025 VEXXHOST, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import typing | ||
|
||
from attrs import asdict, define, field | ||
|
||
from magnum_cluster_api import objects | ||
|
||
|
||
@define | ||
class PatchSelectorMatchMachineDeploymentClass: | ||
names: list[str] | ||
|
||
|
||
@define | ||
class PatchSelectorMatchMachinePoolClass: | ||
names: list[str] | ||
|
||
|
||
@define | ||
class PatchSelectorMatch: | ||
controlPlane: typing.Optional[bool] = None | ||
infrastructureCluster: typing.Optional[bool] = None | ||
machineDeploymentClass: typing.Optional[ | ||
PatchSelectorMatchMachineDeploymentClass | ||
] = None | ||
machinePoolClass: typing.Optional[PatchSelectorMatchMachinePoolClass] = None | ||
|
||
|
||
@define | ||
class PatchSelector: | ||
apiVersion: str | ||
kind: str | ||
matchResources: PatchSelectorMatch | ||
|
||
|
||
@define | ||
class JSONPatchValue: | ||
variable: typing.Optional[str] = None | ||
template: typing.Optional[str] = None | ||
|
||
|
||
@define | ||
class JsonPatch: | ||
op: str | ||
path: str | ||
value: typing.Optional[typing.Any] = field(default=None) | ||
valueFrom: typing.Optional[JSONPatchValue] = field(default=None) | ||
|
||
@value.validator | ||
@valueFrom.validator | ||
def check_xor(self, attribute, value): | ||
if (self.value is None) == (self.valueFrom is None): | ||
raise ValueError( | ||
"Either 'value' or 'valueFrom' must be provided, but not both." | ||
) | ||
|
||
|
||
@define | ||
class PatchDefinition: | ||
selector: PatchSelector | ||
jsonPatches: list[JsonPatch] | ||
|
||
|
||
@define | ||
class ClusterClassPatch: | ||
name: str | ||
enabledIf: str | ||
definitions: list[PatchDefinition] | ||
|
||
def to_dict(self): | ||
return asdict(self, filter=lambda attr, value: value is not None) | ||
|
||
|
||
DISABLE_API_SERVER_FLOATING_IP = ClusterClassPatch( | ||
name="disableAPIServerFloatingIP", | ||
enabledIf="{{ if .disableAPIServerFloatingIP }}true{{end}}", | ||
definitions=[ | ||
PatchDefinition( | ||
selector=PatchSelector( | ||
apiVersion=objects.OpenStackClusterTemplate.version, | ||
kind=objects.OpenStackClusterTemplate.kind, | ||
matchResources=PatchSelectorMatch( | ||
infrastructureCluster=True, | ||
), | ||
), | ||
jsonPatches=[ | ||
JsonPatch( | ||
op="add", | ||
path="/spec/template/spec/disableAPIServerFloatingIP", | ||
valueFrom=JSONPatchValue(variable="disableAPIServerFloatingIP"), | ||
) | ||
], | ||
) | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright (c) 2025 VEXXHOST, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from oslotest import base | ||
|
||
from magnum_cluster_api import objects, patches | ||
|
||
|
||
class TestDisableApiServerFloatingIpClusterClassPatch(base.BaseTestCase): | ||
""" | ||
Test case for DisableApiServerFloatingIpClusterClassPatch. | ||
This isn't great, but it's something. We should ideally be able to template out | ||
the `enabledIf` and make sure that given a specific label, we get a specific | ||
result. | ||
""" | ||
|
||
def test_asdict(self): | ||
patch = patches.DISABLE_API_SERVER_FLOATING_IP.to_dict() | ||
|
||
self.assertEqual( | ||
{ | ||
"name": "disableAPIServerFloatingIP", | ||
"enabledIf": "{{ if .disableAPIServerFloatingIP }}true{{end}}", | ||
"definitions": [ | ||
{ | ||
"selector": { | ||
"apiVersion": objects.OpenStackClusterTemplate.version, | ||
"kind": objects.OpenStackClusterTemplate.kind, | ||
"matchResources": { | ||
"infrastructureCluster": True, | ||
}, | ||
}, | ||
"jsonPatches": [ | ||
{ | ||
"op": "add", | ||
"path": "/spec/template/spec/disableAPIServerFloatingIP", | ||
"valueFrom": {"variable": "disableAPIServerFloatingIP"}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
patch, | ||
) | ||
|
||
def test_enabled_if(self): | ||
patch = patches.DISABLE_API_SERVER_FLOATING_IP | ||
|
||
# https://github.com/kubernetes-sigs/cluster-api-provider-openstack/issues/2408 | ||
self.assertEqual( | ||
"{{ if .disableAPIServerFloatingIP }}true{{end}}", patch.enabledIf | ||
) |