Skip to content

Commit 9043338

Browse files
committed
add background migration
1 parent bc62216 commit 9043338

1 file changed

Lines changed: 33 additions & 15 deletions

File tree

pkg/roles/dhcp/role_migrations.go

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,38 @@ func (r *Role) migrateMoveInitial(ctx context.Context) {
4141
}
4242
}
4343

44-
// func (r *Role) migrateMoveBackground() {
45-
// watchChan := r.i.KV().Watch(
46-
// r.ctx,
47-
// r.i.KV().Key(types.KeyRole, types.KeyLegacyLeases).Prefix(true).String(),
48-
// clientv3.WithPrefix(),
49-
// )
50-
// for watchResp := range watchChan {
51-
// for _, event := range watchResp.Events {
52-
// switch event.Type {
53-
// case clientv3.EventTypeDelete:
54-
// r.i.KV().Delete(r.ctx)
55-
// }
56-
// }
57-
// }
58-
// }
44+
func (r *Role) migrateMoveBackground(ctx context.Context) {
45+
watchChan := r.i.KV().Watch(
46+
ctx,
47+
r.i.KV().Key(types.KeyRole, types.KeyLegacyLeases).Prefix(true).String(),
48+
clientv3.WithPrefix(),
49+
)
50+
type partialLease struct {
51+
ScopeKey string `json:"scopeKey"`
52+
}
53+
for watchResp := range watchChan {
54+
for _, event := range watchResp.Events {
55+
pl := partialLease{}
56+
err := json.Unmarshal(event.Kv.Value, &pl)
57+
if err != nil {
58+
r.log.Warn("failed to parse partial lease", zap.Error(err))
59+
continue
60+
}
61+
ident := strings.Split(string(event.Kv.Key), "/")[2]
62+
newKey := r.i.KV().Key(types.KeyRole, types.KeyScopes, pl.ScopeKey, ident).String()
63+
switch event.Type {
64+
case clientv3.EventTypePut:
65+
_, err = r.i.KV().Put(ctx, newKey, string(event.Kv.Value))
66+
case clientv3.EventTypeDelete:
67+
_, err = r.i.KV().Delete(ctx, newKey)
68+
}
69+
if err != nil {
70+
r.log.Warn("failed to mirror legacy lease operation", zap.Error(err))
71+
continue
72+
}
73+
}
74+
}
75+
}
5976

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

7895
r.migrateMoveInitial(ctx)
96+
go r.migrateMoveBackground(ctx)
7997

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

0 commit comments

Comments
 (0)