Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reduce update scope for twingate_resource_update #176

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/handlers/handlers_resource.py
Expand Up @@ -21,16 +21,20 @@ def twingate_resource_create(body, spec, memo, logger, patch, **kwargs):
)


@kopf.on.update("twingateresource")
def twingate_resource_update(old, new, diff, status, memo, logger, **kwargs):
@kopf.on.update("twingateresource", field="spec")
def twingate_resource_update(spec, new, diff, status, memo, logger, **kwargs):
logger.info(
"Got TwingateResource update request: %s. Diff: %s. Status: %s.",
ekampf marked this conversation as resolved.
Show resolved Hide resolved
new,
diff,
status,
)

crd = ResourceSpec(**new["spec"])
crd = ResourceSpec(**spec)

if len(diff) == 1 and diff[0][0] == "add" and diff[0][1] == ("id",):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I wonder if there is a nicer way to do this because the indexes are quite abstract. Maybe

if diff == (("add", ("id",), None, ANY),):

? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@minhtule I thought about it but ANY is in unittest which I wouldnt want to import on non testing code

return success(twingate_id=crd.id, message="No update required")

if crd.id:
logger.info("Updating resource %s", crd.id)
client = TwingateAPIClient(memo.twingate_settings)
Expand Down
59 changes: 43 additions & 16 deletions app/handlers/tests/test_handlers_resource.py
Expand Up @@ -55,29 +55,20 @@ def test_create(self, resource_factory, kopf_info_mock, mock_api_client):
class TestResourceUpdateHandler:
def test_update(self, mock_api_client):
rid = "UmVzb3VyY2U6OTMxODE3"
old = {
"spec": {
"id": rid,
"address": "my.default.cluster.local",
"name": "My K8S Resource",
}
}
new = {
"spec": {
"id": rid,
"address": "my.default.cluster.local",
"name": "new-name",
}
spec = new = {
"id": rid,
"address": "my.default.cluster.local",
"name": "new-name",
}
diff = (("change", ("spec", "name"), "My K8S Resource", "new-name"),)
diff = (("change", ("name"), "My K8S Resource", "new-name"),)
status = {
"twingate_resource_create": {
"twingate_id": rid,
"created_at": "2023-09-27T04:02:55.249011+00:00",
"updated_at": "2023-09-27T04:02:55.249035+00:00",
}
}
new_resource_spec = ResourceSpec(**new["spec"])
new_resource_spec = ResourceSpec(**new)

mock_api_client.resource_update.return_value = MagicMock(id=rid)

Expand All @@ -87,7 +78,7 @@ def test_update(self, mock_api_client):
patch_mock.spec = {}

result = twingate_resource_update(
old, new, diff, status, memo_mock, logger_mock
spec, new, diff, status, memo_mock, logger_mock
)
assert result == {
"success": True,
Expand All @@ -100,6 +91,42 @@ def test_update(self, mock_api_client):
mock_api_client.resource_update.assert_called_once_with(new_resource_spec)
assert patch_mock.spec == {}

def test_update_caused_by_create_does_nothing(self, mock_api_client):
rid = "UmVzb3VyY2U6OTMxODE3"
spec = new = {
"id": rid,
"address": "my.default.cluster.local",
"name": "new-name",
}
diff = (("add", ("id",), rid),)
ekampf marked this conversation as resolved.
Show resolved Hide resolved
status = {
"twingate_resource_create": {
"twingate_id": rid,
"created_at": "2023-09-27T04:02:55.249011+00:00",
"updated_at": "2023-09-27T04:02:55.249035+00:00",
}
}

mock_api_client.resource_update.return_value = MagicMock(id=rid)

logger_mock = MagicMock()
memo_mock = MagicMock()
patch_mock = MagicMock()
patch_mock.spec = {}

result = twingate_resource_update(
spec, new, diff, status, memo_mock, logger_mock
)
assert result == {
"success": True,
"twingate_id": rid,
"message": "No update required",
"ts": ANY,
}

mock_api_client.resource_update.assert_not_called()
assert patch_mock.spec == {}


class TestResourceDeleteHandler:
def test_delete(self, mock_api_client):
Expand Down