Skip to content

Commit

Permalink
add background migration
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu committed Nov 25, 2024
1 parent bc62216 commit 9043338
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions pkg/roles/dhcp/role_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,38 @@ func (r *Role) migrateMoveInitial(ctx context.Context) {
}
}

// func (r *Role) migrateMoveBackground() {
// watchChan := r.i.KV().Watch(
// r.ctx,
// r.i.KV().Key(types.KeyRole, types.KeyLegacyLeases).Prefix(true).String(),
// clientv3.WithPrefix(),
// )
// for watchResp := range watchChan {
// for _, event := range watchResp.Events {
// switch event.Type {
// case clientv3.EventTypeDelete:
// r.i.KV().Delete(r.ctx)
// }
// }
// }
// }
func (r *Role) migrateMoveBackground(ctx context.Context) {
watchChan := r.i.KV().Watch(
ctx,
r.i.KV().Key(types.KeyRole, types.KeyLegacyLeases).Prefix(true).String(),
clientv3.WithPrefix(),
)
type partialLease struct {
ScopeKey string `json:"scopeKey"`
}
for watchResp := range watchChan {
for _, event := range watchResp.Events {
pl := partialLease{}
err := json.Unmarshal(event.Kv.Value, &pl)
if err != nil {
r.log.Warn("failed to parse partial lease", zap.Error(err))
continue
}
ident := strings.Split(string(event.Kv.Key), "/")[2]
newKey := r.i.KV().Key(types.KeyRole, types.KeyScopes, pl.ScopeKey, ident).String()
switch event.Type {
case clientv3.EventTypePut:
_, err = r.i.KV().Put(ctx, newKey, string(event.Kv.Value))
case clientv3.EventTypeDelete:
_, err = r.i.KV().Delete(ctx, newKey)
}
if err != nil {
r.log.Warn("failed to mirror legacy lease operation", zap.Error(err))
continue
}
}
}
}

func (r *Role) RegisterMigrations() {
r.i.Migrator().AddMigration(&migrate.InlineMigration{
Expand All @@ -76,6 +93,7 @@ func (r *Role) RegisterMigrations() {
leasePrefix := r.i.KV().Key(types.KeyRole, types.KeyScopes).Prefix(true).String()

r.migrateMoveInitial(ctx)
go r.migrateMoveBackground(ctx)

return r.i.KV().WithHooks(storage.StorageHook{
PutPost: func(ctx context.Context, key, val string, res *clientv3.PutResponse, opts ...clientv3.OpOption) (*clientv3.PutResponse, error) {
Expand Down

0 comments on commit 9043338

Please sign in to comment.