Skip to content

sonic: document and test config generator ownership model#2297

Draft
ideaship wants to merge 2 commits into
mainfrom
doc-sonic-generator-ownership-model
Draft

sonic: document and test config generator ownership model#2297
ideaship wants to merge 2 commits into
mainfrom
doc-sonic-generator-ownership-model

Conversation

@ideaship
Copy link
Copy Markdown
Contributor

Motivation

PRs #2279 and #2290 both ran into the same unresolved question: when generate_sonic_config() regenerates a SONiC config, which sections does it own outright, and which can an operator customize directly in config_db.json?

In #2279, a reviewer noted the assumption implied by unconditional assignment to ROUTE_REDISTRIBUTE but left the question open. In #2290, new tests used empty scaffolds, so they neither confirmed nor denied base-config preservation — and the same question arises on closer inspection of that PR. This PR tries to break the pattern.

What this PR does

Docstring (generate_sonic_config): states the ownership model as the code largely implements it — generated sections (BGP_GLOBALS, VLAN, VLAN_INTERFACE, ROUTE_REDISTRIBUTE, VXLAN_TUNNEL, VXLAN_TUNNEL_MAP, etc.) are fully owned by the generator; entries in those sections are unconditionally overwritten on each regen, and pre-existing values from config_db.json are not preserved.

Illustrative tests (test_config_generator_ownership.py): five passing tests showing what "owned" means in practice — pre-existing operator fields in BGP_GLOBALS[vrf_name], VLAN[Vlan{vni}], ROUTE_REDISTRIBUTE[key], and VLAN[VlanX] are silently dropped on regen.

Violation test (test_config_generator_orchestrator.py): one failing test that exposes an inconsistency in the current implementation — BGP_GLOBALS["default"] is handled differently from the rest. The orchestrator updates it with key-level writes rather than replacing the entry, so pre-existing fields survive regen. The test is intentionally left failing to flag this for discussion.

What this PR is not

This is not a proposal to enforce a specific policy, and it does not fix the BGP_GLOBALS["default"] inconsistency. The goal is to make the implicit rule visible and invite a decision:

  • Is the ownership model as stated correct? If not, what should it say?
  • For BGP_GLOBALS["default"] specifically: should the orchestrator be fixed to replace the entry on regen (which would make the failing test pass), or should the rule be refined to treat the default VRF as a merge-only exception?

Settling this once would avoid relitigating it in every PR that touches the config generator.

The SONiC config generator starts from a deep copy of the device's
/etc/sonic/config_db.json, but helpers subsequently replace whole
entries in certain sections via unconditional assignment rather than
merging.  The implicit rule is that generated sections are fully
owned by the generator and always overwritten on regen; only sections
not touched by the generator pass through unchanged.

This assumption was first surfaced during review of PR #2279, which
expanded the set of overwritten keys in ROUTE_REDISTRIBUTE.  It was
noted again during review of PR #2290, whose new tests use empty
scaffolds and therefore do not constrain the base-config overwrite
behavior.  In both cases the assumption was undocumented, leaving
reviewers and contributors without a clear statement of intent.

Document the ownership model in the generate_sonic_config() docstring
so that the rule is visible to contributors maintaining the function,
and so that future PRs can be evaluated against an explicit contract
rather than an implicit one.

AI-assisted: Claude Code
Signed-off-by: Roger Luethi <luethi@osism.tech>
The previous commit documented the config generator ownership model in
the generate_sonic_config() docstring: generated sections are fully
owned by the generator and entries in those sections are unconditionally
overwritten on regen, not preserved from /etc/sonic/config_db.json.

Without tests the rule is only a claim.  This commit adds two groups
of tests to make it concrete:

Illustrative tests (test_config_generator_ownership.py):
  - _add_vrf_configuration replaces BGP_GLOBALS[vrf_name] wholesale;
    any operator-added field (e.g. a custom timer) is silently dropped.
  - _add_vrf_configuration replaces VLAN[Vlan{vni}] wholesale; any
    operator-added field (e.g. a description) is silently dropped.
  - _add_vrf_configuration resets the ROUTE_REDISTRIBUTE entry for the
    generated key to {}; any pre-existing route policy is dropped.
  - Sections not written by these helpers pass through unchanged.
  - _add_vlan_configuration replaces VLAN[VlanX] wholesale; any
    operator-added field is silently dropped.

Violation test (test_config_generator_orchestrator.py):
  - Pre-existing fields in BGP_GLOBALS['default'] must not survive
    regen per the ownership rule.  This test currently fails because
    the orchestrator writes BGP_GLOBALS['default'] via key-level
    updates rather than replacing the entry.  Reviewers must decide
    whether to fix the orchestrator to comply, or refine the ownership
    rule to treat the default VRF as a merge-only exception.

AI-assisted: Claude Code
Signed-off-by: Roger Luethi <luethi@osism.tech>
@ideaship ideaship force-pushed the doc-sonic-generator-ownership-model branch from cae57c6 to 9952d93 Compare May 20, 2026 06:44
@ideaship ideaship requested a review from berendt May 20, 2026 06:45
@ideaship ideaship moved this from Ready to In review in Human Board May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

2 participants