-
Notifications
You must be signed in to change notification settings - Fork 369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatic index migration #607
Automatic index migration #607
Conversation
if !isMigrated && cmd.Use != "index-upgrade" { | ||
fmt.Fprintln(os.Stderr, "You need to perform a migration to continue using krew.\nPlease run `kubectl krew system index-upgrade`") | ||
return errors.New("krew index outdated") | ||
if !isMigrated { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems indexmigration.Migrate already has the check and return:
krew/internal/indexmigration/migration.go
Lines 37 to 46 in ea01855
// Migrate removes the index directory and then clones krew-index to the new default index path. | |
func Migrate(paths environment.Paths) error { | |
isMigrated, err := Done(paths) | |
if err != nil { | |
return errors.Wrap(err, "failed to check if index migration is complete") | |
} | |
if isMigrated { | |
klog.V(2).Infoln("Already migrated") | |
return nil | |
} |
so we're duplicating indexmigration.Done
call here.
also try adding as many log statements around this path as you can ("detected migration needed, starting..." etc).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah good catch!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this logging be leveled or always displayed to the user? The confirmation at the end was displayed to the user back when this was a manual process through system index-upgrade
but now that its automatic should this happen at some log level and be silent normally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can show the one-time operations like these to the user (after all they would be shown only "once"):
- migrating krew index layout
- migration completed
/area multi-index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just minor nits. This is getting serious now 😄 !
internal/indexmigration/migration.go
Outdated
if err != nil { | ||
return errors.Wrap(err, "failed to check if index migration is complete") | ||
} | ||
if isMigrated { | ||
klog.V(2).Infoln("Already migrated") | ||
klog.V(2).Infoln("already migrated") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, log messages can be capitalized and end with a dot. Errors on the other hand should not, because they can be wrapped and then it looks weird.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok I can change it, I misremembered reading that log messages shouldn't be capitalized in addition to error messages.
cmd/krew/cmd/root.go
Outdated
fmt.Fprintln(os.Stderr, "You need to perform a migration to continue using krew.\nPlease run `kubectl krew system index-upgrade`") | ||
return errors.New("krew index outdated") | ||
if err := indexmigration.Migrate(paths); err != nil { | ||
return errors.Wrap(err, "failed to automatically migrate index") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error is no longer accurate. The index could already have been migrated, but the check failed. In this particular situation, I think it's best not to wrap at all, because the errors from Migrate
are already descriptive. If you feel strongly about wrapping, I'd suggest something like
return errors.Wrap(err, "failed to automatically migrate index") | |
return errors.Wrap(err, "index initialization") |
integration_test/migration_test.go
Outdated
// any command here should cause the index migration to occur | ||
test.Krew("index", "list").RunOrFail() | ||
if !isIndexMigrated(test) { | ||
t.Error("output should include the default index after migration") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.Error("output should include the default index after migration") | |
t.Error("index should have been auto-migrated") |
integration_test/migration_test.go
Outdated
test.Krew().RunOrFail() | ||
if isIndexMigrated(test) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that behaviour something we really require or just a coincidence of the implementation? If the latter is the case, consider removing the test, because it doesn't test a relevant behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think just a coincidence of the implementation (cobra doesn't seem to execute the prerun in the case of no subcommand or krew -h
). It was something I noticed when I was testing the migration locally and wasn't really sure if I should add a test for or not. I can remove this if it seems unnecessary.
cmd/krew/cmd/root.go
Outdated
fmt.Fprintln(os.Stderr, "You need to perform a migration to continue using krew.\nPlease run `kubectl krew system index-upgrade`") | ||
return errors.New("krew index outdated") | ||
if err := indexmigration.Migrate(paths); err != nil { | ||
return err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this err needs to be wrapped. I don't see any indication of what was being done for err messages inside Migrate() (saying "migration failed"). So it's good to add that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should say "migration failed", because the index could already have been migrated, but the check failed. Wrapping with a message which communicates this ambiguity would be fine though.
Maybe the function is also not named well. It checks and maybe migrates instead of just migrate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you guys think about moving the migration needed check out of Migrate
then? I think that would solve these issues (make it easier to wrap the migration failed/check failed errors and help the name make more sense).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, let's separate the check from the migration. That will make it easier to understand what is actually happening. 👍
return err == nil | ||
} | ||
|
||
func prepareOldIndexLayout(it *ITest) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this something we should eventually delete?
if so please add a TODO (e.g. // TODO remove when testing indexmigration is no longer necessary).
internal/indexmigration/migration.go
Outdated
if err != nil { | ||
return errors.Wrap(err, "failed to check if index migration is complete") | ||
} | ||
if isMigrated { | ||
klog.V(2).Infoln("Already migrated") | ||
klog.V(2).Infoln("Already migrated.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so imagine every time krew runs in -v≥2, this will show up in the output.
but it's missing context:
- "Already migrated" → what’s migrated
- when is it started (there's no "checking index migration" msg)
/approve |
Add integration test for auto migration
Just call migrate in root and add logging around migration. Also make migration check function private
601a4cd
to
1521b2b
Compare
/lgtm thanks for this! |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: ahmetb, chriskim06 The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
root.go
that previously informed the user that they needed to migrate with the actualindexmigration.Migrate
functionFixes #600
Related issue: #566