Skip to content

fix: restrict get_or_create to get_or_create_on_node - #462

Open
AloizioMacedo wants to merge 5 commits into
canonical:masterfrom
AloizioMacedo:lp-2147706
Open

AloizioMacedo wants to merge 5 commits into
canonical:masterfrom
AloizioMacedo:lp-2147706

Conversation

@AloizioMacedo

Copy link
Copy Markdown
Contributor

LP:2147706 reports that get_or_create is erroring with multiple entries after commissioning an initial set of servers. The only way that this seems possible is if there are multiple interfaces with the same mac address, likely on different nodes since the issue happens after creating a first set of nodes and not on a clean environment.

The code in update_physical_interface does not account for this, but accounts for the possibility of the interface being in the incorrect node and does some cleanup related to this. This cleanup has duplicated logic in get_or_create_on_node, so we just use that instead (which should avoid the multiple entries issues due to the restriction on the node).

I took the liberty of also returning the information of whether the interface was created from get_or_create_on_node, to mirror what get_or_create does instead of having to do extra queries to check if something was added.

Resolves: LP:2147706

The function was doing some duplicated work with get_or_create_on_node
precisely for the case of a mac address being on the wrong node, which
is very likely related to LP:2147706
@AloizioMacedo AloizioMacedo self-assigned this Jul 14, 2026
@maas-lander

Copy link
Copy Markdown
Collaborator

Check where you would like a Mattermost message to be sent to when CI completes and this PR is merged

  • Direct message
  • ~maas

@AloizioMacedo

Copy link
Copy Markdown
Contributor Author

I put this in draft since I was not able to reproduce the issue yet. Will try a couple times, but the changes seem to be meaningful regardless.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[maas-code-reviewer review]

LLM-generated review from https://github.com/canonical/maas-code-reviewer.
Intended to assist a human reviewer, not replace one — suggestions may be
incorrect, please verify before acting.

Overall, this is a clean approach to fix the bug where multiple objects are returned by get_or_create. Shifting the creation and moving logic to get_or_create_on_node removes duplication and keeps the interface logic central. However, I noticed a critical issue regarding how updates are saved to the database. Because of the removal of the explicit update_fields logic for name and node_config in update_physical_interface, modifications made to existing interfaces inside get_or_create_on_node are no longer saved to the database. Additionally, please ensure that any tests calling or mocking get_or_create_on_node are updated to expect a tuple.

Comment thread src/maasserver/models/interface.py
@AloizioMacedo
AloizioMacedo marked this pull request as ready for review July 15, 2026 17:38
@AloizioMacedo
AloizioMacedo requested a review from a team July 15, 2026 17:38
@AloizioMacedo
AloizioMacedo marked this pull request as draft July 15, 2026 20:33
@AloizioMacedo
AloizioMacedo marked this pull request as ready for review July 15, 2026 21:25
node.is_commissioning() or (node.enable_hw_sync and port is not None)
)

interface, created = PhysicalInterface.objects.get_or_create_on_node(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe add a regression test which sets up that duplicate state and runs the commissioning network update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants