Skip to content

Commit

Permalink
Add patches + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaser committed Feb 4, 2025
1 parent 0dd8fe7 commit 1415f23
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 24 deletions.
97 changes: 97 additions & 0 deletions magnum_cluster_api/patches.py
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"),
)
],
)
],
)
26 changes: 2 additions & 24 deletions magnum_cluster_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
image_utils,
images,
json_patches,
patches,
objects,
utils,
)
Expand Down Expand Up @@ -1896,30 +1897,7 @@ def get_object(self) -> objects.ClusterClass:
},
],
},
{
"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"
},
},
],
},
],
},
patches.DISABLE_API_SERVER_FLOATING_IP.to_dict()
{
"name": "controlPlaneAvailabilityZones",
"enabledIf": '{{ if ne (index .controlPlaneAvailabilityZones 0) "" }}true{{end}}',
Expand Down
53 changes: 53 additions & 0 deletions magnum_cluster_api/tests/unit/test_patches.py
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
)

0 comments on commit 1415f23

Please sign in to comment.