Skip to content
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

Merged

Conversation

chriskim06
Copy link
Member

  • replace the part in root.go that previously informed the user that they needed to migrate with the actual indexmigration.Migrate function
  • add integration tests (1 for checking auto migration happens and another for the only scenario I can think of where it doesn't happen)

Fixes #600
Related issue: #566

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 5, 2020
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 5, 2020
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 {
Copy link
Member

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:

// 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).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch!

Copy link
Member Author

@chriskim06 chriskim06 Jun 5, 2020

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?

Copy link
Member

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

@chriskim06
Copy link
Member Author

/area multi-index

Copy link
Contributor

@corneliusweig corneliusweig left a 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 😄 !

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")
Copy link
Contributor

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.

Copy link
Member Author

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.

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")
Copy link
Contributor

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

Suggested change
return errors.Wrap(err, "failed to automatically migrate index")
return errors.Wrap(err, "index initialization")

// 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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.Error("output should include the default index after migration")
t.Error("index should have been auto-migrated")

Comment on lines 51 to 52
test.Krew().RunOrFail()
if isIndexMigrated(test) {
Copy link
Contributor

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.

Copy link
Member Author

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.

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
Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Member Author

@chriskim06 chriskim06 Jun 7, 2020

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).

Copy link
Contributor

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) {
Copy link
Member

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).

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.")
Copy link
Member

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)

@ahmetb
Copy link
Member

ahmetb commented Jun 7, 2020

/approve
minor issues here, too.

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 7, 2020
@ahmetb
Copy link
Member

ahmetb commented Jun 10, 2020

/lgtm
/approve

thanks for this!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 10, 2020
@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 4e194df into kubernetes-sigs:master Jun 10, 2020
@chriskim06 chriskim06 deleted the automatic-index-migration branch June 11, 2020 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/multi-index cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Index directory layout migration
4 participants