Skip to content

feat(nautobotop): per-item sync resilience for vlan groups and vlans - #2558

Open
geetikabatra wants to merge 1 commit into
mainfrom
feat/nautobotop-sync-resilience
Open

geetikabatra wants to merge 1 commit into
mainfrom
feat/nautobotop-sync-resilience

Conversation

@geetikabatra

@geetikabatra geetikabatra commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

The operator previously stopped at the first item Nautobot rejected within a resource type, and stopped at the first resource type that failed in the reconcile loop. A single bad VLAN group (e.g. a 400 because a range cannot be shrunk while VLANs exist in it) would skip every other VLAN group, every later resource type, and leave no lasting record in status.

This change makes the sync carry on past per-item data errors, surface them in status and metrics, and retry them on the next reconcile pass.

What changed

Error classification (#1)

Sort each error from the Nautobot client into one of two kinds:

  • Data error — a per-item 4xx: note it, skip the item, keep going.
  • Infra error — auth, 5xx, transport: stop and let controller-runtime back off.

New package: internal/nautobot/syncerr

Item-loop helper (#2)

Add syncItems (and exported SyncItems for sub-packages) in internal/nautobot/sync/helpers.go. Loops over items, isolates data errors, stops on infra errors, returns collected failures + any stopping error.

Per-item isolation in every sync plugin (#3, 8a–8n) Port all 17 SyncAll methods to the syncItems helper. Each plugin now wraps its loop in an apply* orchestrator method (e.g. applyClusters, applyVlans) that is independently unit-testable:

cluster, clusterGroup, clusterType, device, devicetype,
namespace, prefix, rack, rir, role, tenant, tenantGroup,
vlan, vlanGroup, admin/permissionGroup

Special cases:

  • devicetype: extracted inline per-item logic into syncSingleDeviceType and a deleteObsoleteDeviceTypes helper before porting.
  • prefix: syncSinglePrefix returns (string, error) to track synced IDs for the prune step; a closure wrapper adapts it to syncItems.
  • admin/permissionGroup: lives in a sub-package; uses exported SyncItems; group-name-to-ID cache is shared across items via closure.

deleteObsolete* still runs after a partial failure so that items which did apply are still authoritative for the prune.

Continue reconcile across resource types on partial failure (#4) The reconcile loop in internal/controller/nautobot_controller.go no longer returns on the first resource type that fails. A type that returned a PartialSyncError is noted and the loop continues to the next type. Infrastructure errors still return immediately so backoff still works. A type's SyncHash is only advanced when the type finishes cleanly, so types with failures are retried on the next reconcile pass.

Status and metrics (#5)

  • Status is saved even on a partial-failure run.
  • Per-resource failure counts are added to NautobotStatus (map[resource]failureCount).
  • Message is set to describe the partial state, e.g. 'sync completed with 1 item failure across 1 resource'.
  • A gauge nautobotop_sync_item_failures{nautobot, resource} is published each run; it drops to 0 when a resource syncs cleanly.

Docs

Expanded go/nautobotop/docs/vlan-group-sync-resilience.md task map with individual checkbox tasks for each plugin (§8a–8n flat, §9a–9c recursive).

Tests

Every ported plugin has three unit tests exercising the apply* method directly (no live Nautobot required):

  • OneBadItemDoesNotBlockTheRest
  • InfraErrorAborts
  • AllSuccess

What does this change do?

Upgrade impact

  • This change requires operator action to upgrade. If checked, add the
    upgrade-impact label and a release note: run scriv create from the
    repository root and describe the required action in the generated
    changelog.d/ file. See RELEASING.md.

Operator action means anything a deployment has to do beyond a normal resync:
deploy repo or values changes, new or removed secrets, enabling or disabling a
component, or a manual one-time step.

@geetikabatra
geetikabatra force-pushed the feat/nautobotop-sync-resilience branch from 603c94b to 7068001 Compare October 22, 2026 07:32
if err == nil {
continue
}
var de *syncerr.DataError

@cardoe cardoe Oct 22, 2026 •

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.

SyncItems can only isolate errors that have already been wrapped as DataError, but I only see HTTP response classification in VlanGroupService.Create and Update. The other migrated services still return raw SDK errors, so a 400 from clusters, devices, locations, tenants, etc. reaches the infrastructure-error path below and stops reconciliation. Could we classify create/update responses for every migrated resource, ideally through a shared client helper, and add at least one test at a real service boundary rather than injecting NewDataError directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I agree. I am adding a shared helper method in the client package so every mitigated service's Create/update maps 4XX(excluding 403,401/429) to DataError, while 5XX and transport errors stay infra I am also adding boundary tests using an httptest server returning 400 against real services(devices, clustersm tenants) to verify that one bad item doesn't halt reconcillation.

return err
}

prune(ctx, syncedIDs)

@cardoe cardoe Oct 22, 2026 •

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.

I think pruning from only syncedIDs makes a partial failure destructive. If an existing, operator-owned prefix gets a data error during update, its ID is not added to this map, and deleteObsoletePrefixes will treat that desired prefix as obsolete and destroy it. Could we preserve IDs for all resolved desired prefixes, or skip pruning when any item fails? It would also be good for the partial-failure test to verify that the failed desired prefix is retained.

// given Nautobot CR. A count of zero clears the gauge entry so recovered
// resource types no longer show (and any alert on them resolves).
func setSyncItemFailuresGauge(crName, resource string, count int) {
syncItemFailuresGauge.WithLabelValues(crName, resource).Set(float64(count))

@cardoe cardoe Oct 22, 2026 •

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.

How will this label set be removed when the Nautobot CR is deleted? Reconcile currently returns immediately on IgnoreNotFound, so a CR deleted while this gauge is nonzero will leave a stale series and the alert can continue firing for an object that no longer exists. I think the deletion/NotFound path should remove the CR labels, for example with DeletePartialMatch. Also, Set(0) resolves the alert but does not actually clear the gauge entry as the comment says.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, correct. I am adding deleteIteFailure when CR is absent.

setSyncItemFailuresGauge(nautobotCR.Name, res.name, len(partial.Failures))
totalItemFailures += len(partial.Failures)
// Do not advance SyncHash: retry this resource next reconcile.
continue

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.

The controller has an explicit dependency graph, but this continue still lets transitive dependents run after a prerequisite only partially synced. For example, if a new vlanGroup is rejected, vlan runs next and buildVlanGroupReference silently returns an empty reference, so we can create or update a VLAN without its requested group. The same risk applies to the other dependency edges. Could we continue independent branches but skip resources whose dependency had a partial failure, and add a controller test for that behavior?

@cardoe cardoe left a comment

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.

Stepping back, I think the test changes need a refactor before this is ready. This PR adds 2,118 lines of tests, but most of the per-resource apply* tests repeat the same three cases against injected callbacks: one data error continues, one infrastructure error aborts, and all items succeed. That repetition makes the PR much harder to review without giving us confidence at the production boundary—the current tests all manufacture DataError directly, which is why they did not catch that only VLAN groups classify real SDK responses.

I would concentrate the generic continuation/abort coverage in SyncItems, then keep focused coverage for the genuinely different paths: one real HTTP/service-boundary test for classification, one flat resource integration path, one recursive tree path, the prefix pruning special case, and controller tests for dependency skipping, hashes, status, and metrics. If every resource still needs a smoke test, a shared/table-driven harness would make that intent much clearer than 17 copies of the same tests.

The earlier threads about missing production classification, destructive prefix pruning, and stale metric labels are still unresolved on this commit. I have also added an inline comment about not running transitive dependents after a prerequisite partially fails. Given those correctness issues, I do not think the current size is justified yet.

@geetikabatra
geetikabatra force-pushed the feat/nautobotop-sync-resilience branch from e98f224 to ad3bd39 Compare December 1, 2026 13:22
- Add syncerr to sort Nautobot errors into data errors (4xx) and infrastructure errors
- Add SyncItems helper that skips items with data errors and stops on infrastructure errors
- Classify vlan and vlanGroup service errors with syncerr.Classify at the service boundary
- Use per-item handling in the vlan and vlanGroup syncs
- Continue across resource types after a partial failure and advance SyncHash only on clean syncs
- Skip resource types whose dependency partially failed, including transitive dependents
- Clear SyncHash of failed and skipped types so the next reconcile retries them
- Save status, report and SyncItemFailures on partial and aborted runs
- Return non-NotFound Get errors from Reconcile instead of reconciling an empty CR
- Add the nautobotop_sync_item_failures gauge, published from Status.SyncItemFailures
- Clear counts for recovered and unconfigured resources and keep the last count for skipped or aborted ones
- Remove a CR's gauge series when it is deleted or disabled
- Add a PrometheusRule that alerts on item failures that persist
- Expose operator metrics in the Helm chart with a metrics Service, ServiceMonitor and token-check RBAC
- Add SyncItemFailures to the CRD status and regenerate deepcopy
- Add an httptest-based service classification test helper with vlan and vlanGroup cases
- Add controller tests for dependency skipping, hashes, status message and gauge lifecycle
- Drop apply tests that repeat SyncItems coverage
- Fix configmap_watch and dag tests and lint issues
- Add the sync resilience design doc with follow-up task map and a dev testing guide
@geetikabatra
geetikabatra force-pushed the feat/nautobotop-sync-resilience branch from 0c261a0 to 6932d64 Compare December 3, 2026 10:17
@geetikabatra geetikabatra changed the title feat(nautobotop): per-item sync resilience across all 17 resource types feat(nautobotop): per-item sync resilience for vlan groups and vlans Dec 3, 2026

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants