Skip to content

Commit

Permalink
fix some minor things
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu committed Nov 25, 2024
1 parent 50db6e6 commit 99334a6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
9 changes: 6 additions & 3 deletions pkg/instance/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"beryju.io/gravity/pkg/roles"
"beryju.io/gravity/pkg/storage"
"github.com/Masterminds/semver/v3"
"github.com/getsentry/sentry-go"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -77,15 +78,17 @@ func (mi *Migrator) Run(ctx context.Context) (*storage.Client, error) {
}
mi.log.Debug("Checking migrations to activate for cluster version", zap.String("clusterVersion", cv.String()))
cli := mi.ri.KV()
span := sentry.TransactionFromContext(ctx).StartChild("gravity.instance.migrate")
defer span.Finish()
for _, m := range mi.migrations {
mi.log.Debug("Checking if migration needs to be run", zap.String("migration", m.Name()))
enabled, err := m.Check(cv, ctx)
enabled, err := m.Check(cv, span.Context())
if err != nil {
mi.log.Warn("failed to check if migration should be enabled", zap.String("migration", m.Name()), zap.Error(err))
return nil, err
}
if enabled {
_cli, err := m.Hook(ctx)
_cli, err := m.Hook(span.Context())
if err != nil {
mi.log.Warn("failed to hook for migration", zap.String("migration", m.Name()), zap.Error(err))
return nil, err
Expand All @@ -94,7 +97,7 @@ func (mi *Migrator) Run(ctx context.Context) (*storage.Client, error) {
cli = _cli
} else {
mi.log.Info("Running cleanup for migration", zap.String("migration", m.Name()))
err := m.Cleanup(ctx)
err := m.Cleanup(span.Context())
if err != nil {
mi.log.Warn("failed to cleanup migration", zap.String("migration", m.Name()), zap.Error(err))
continue
Expand Down
16 changes: 7 additions & 9 deletions pkg/roles/dhcp/role_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,26 @@ import (
"go.uber.org/zap"
)

func (r *Role) migrateMoveInitial() {
func (r *Role) migrateMoveInitial(ctx context.Context) {
r.log.Info("Running initial move migration")
res, err := r.i.KV().Get(
r.ctx,
r.i.KV().Key(types.KeyRole, types.KeyLegacyLeases).Prefix(true).String(),
clientv3.WithPrefix(),
)
pfx := r.i.KV().Key(types.KeyRole, types.KeyLegacyLeases).Prefix(true).String()
res, err := r.i.KV().Get(ctx, pfx, clientv3.WithPrefix())
if err != nil {
r.log.Warn("failed to get legacy leases", zap.Error(err))
return
}

Check warning on line 22 in pkg/roles/dhcp/role_migrations.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/dhcp/role_migrations.go#L15-L22

Added lines #L15 - L22 were not covered by tests
// Copy from legacy prefix (global scope) to new prefix (scope-scoped)
for _, kv := range res.Kvs {
l := &Lease{}
ident := strings.TrimPrefix(string(kv.Key), pfx)
err = json.Unmarshal(kv.Value, &l)
if err != nil {
r.log.Warn("failed to parse lease", zap.Error(err))
continue

Check warning on line 30 in pkg/roles/dhcp/role_migrations.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/dhcp/role_migrations.go#L24-L30

Added lines #L24 - L30 were not covered by tests
}
_, err = r.i.KV().Put(
r.ctx,
r.i.KV().Key(types.KeyRole, types.KeyScopes, l.ScopeKey, l.Identifier).String(),
ctx,
r.i.KV().Key(types.KeyRole, types.KeyScopes, l.ScopeKey, ident).String(),
string(kv.Value),
)
if err != nil {
Expand Down Expand Up @@ -67,7 +65,7 @@ func (r *Role) RegisterMigrations() {
pureKV := r.i.KV()
leasePrefix := r.i.KV().Key(types.KeyRole, types.KeyScopes).Prefix(true).String()

r.migrateMoveInitial()
r.migrateMoveInitial(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 99334a6

Please sign in to comment.