Skip to content

fix(manager): make DRAGONFLY_PAT seeding idempotent on restart - #5152

Open
harshitnub077 wants to merge 1 commit into
dragonflyoss:mainfrom
harshitnub077:fix/manager-pat-seed-idempotent
Open

harshitnub077 wants to merge 1 commit into
dragonflyoss:mainfrom
harshitnub077:fix/manager-pat-seed-idempotent

Conversation

@harshitnub077

@harshitnub077 harshitnub077 commented Nov 27, 2026 •

Copy link
Copy Markdown

Description

When DRAGONFLY_PAT is set, the manager seeds a personal access token named default on every start with a plain INSERT. The first start works. Every later start breaks a unique index (uk_personal_access_token_name or uk_personal_access_token), seed returns the error, and the manager exits:

Error 1062 (23000): Duplicate entry 'default' for key 'personal_access_token.uk_personal_access_token_name'

In Kubernetes this turns into a crash loop after the first manager restart. Seen with helm chart 1.11.0 (app 2.5.4), MySQL, and manager.extraEnvVars: [{name: DRAGONFLY_PAT, value: ...}].

This PR moves the seeding into seedPersonalAccessToken and creates the token with clause.OnConflict{DoNothing: true}:

  • MySQL / PolarDB: INSERT ... ON DUPLICATE KEY UPDATE id=id
  • Postgres: INSERT ... ON CONFLICT DO NOTHING

Why a conflict clause and not First / FirstOrCreate:

  • The unique indexes also cover soft-deleted rows (is_del flag), but GORM's default scope hides them. If an admin deletes the seeded token through the API, a scoped lookup finds nothing, the insert collides with the deleted row, and the manager crash-loops again.
  • The database settles the conflict, so several manager replicas starting at once can't race each other into a duplicate-key error.

If a token with that value or name already exists, it is left unchanged. The manager does not reactivate or extend an inactive, expired or deleted token. A token in that state was most likely revoked on purpose, and a pod restart should not undo that. If the existing default token has a different value (for example after DRAGONFLY_PAT was changed), the manager starts normally and logs a warning that DRAGONFLY_PAT is not used, instead of crashing. Changing the token through the env var would be a separate feature.

Related Issue

Fixes #5151

Motivation and Context

With DRAGONFLY_PAT set, the manager could only be started once against the same database. Any restart (rolling update, node drain, OOM) left it in a crash loop.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation Update (if none of the other choices apply)

Checklist

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.

Testing

  • New TestSeed_PersonalAccessToken (sqlite, same setup as manager/gc) runs migrate and then seed twice for: a fresh database, an existing inactive/expired token with the same value, a soft-deleted token with the same value, and an existing default token with a different value. All four cases fail on main with UNIQUE constraint failed and pass with this change.
  • Ran by hand against MySQL 8.0 by calling newMysql (migrate + seed) several times. On main the second start fails with the 1062 error above. With this change, three starts succeed and leave exactly one token, and a changed DRAGONFLY_PAT logs the warning.
  • go build ./..., go test ./manager/..., golangci-lint run ./manager/database/... (0 issues).

The manager seeds a personal access token from DRAGONFLY_PAT on every
start with a plain INSERT. Every start after the first breaks the unique
index on the token name or value, seed returns the error and the manager
exits, so it crash-loops after its first restart:

  Error 1062 (23000): Duplicate entry 'default' for key
  'personal_access_token.uk_personal_access_token_name'

Create the token with ON CONFLICT DO NOTHING (ON DUPLICATE KEY UPDATE
id=id on MySQL). A scoped lookup is not enough: the unique indexes also
cover soft-deleted rows, which GORM's default scope hides, and the
database also settles races between replicas that start at once.

An existing token is left unchanged. It is not reactivated or extended,
so a restart never undoes a revocation. If the "default" token holds a
different value, log a warning instead of failing.

Signed-off-by: Harshit Kudhial <harshitkudhial@gmail.com>
@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.19048% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.40%. Comparing base (6ec85c2) to head (d8289f6).

Files with missing lines Patch % Lines
manager/database/database.go 76.19% 2 Missing and 3 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5152      +/-   ##
==========================================
+ Coverage   63.08%   63.40%   +0.31%     
==========================================
  Files         232      232              
  Lines       23171    23184      +13     
==========================================
+ Hits        14618    14699      +81     
+ Misses       7902     7817      -85     
- Partials      651      668      +17     
Flag Coverage Δ
unittests 63.40% <76.19%> (+0.31%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
manager/database/database.go 43.82% <76.19%> (+43.82%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@harshitnub077

Copy link
Copy Markdown
Author

Hi maintainers, the only failing check is Classify PR, which needs exactly one label. This is a bug fix (the manager crash-loops on restart when DRAGONFLY_PAT is set), so it should get bug, but I don't have permission to add labels as an external contributor. Could someone add it? Thanks!

@harshitnub077
harshitnub077 force-pushed the fix/manager-pat-seed-idempotent branch from 86c8f4b to d8289f6 Compare November 28, 2026 16:27
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.

Manager crash-loops on restart when DRAGONFLY_PAT is set

4 participants