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

Index migration #505

Merged
merged 9 commits into from
Feb 21, 2020
Merged

Conversation

chriskim06
Copy link
Member

@chriskim06 chriskim06 commented Feb 21, 2020

This only adds the migration code to change the directory structure that will be needed to support custom indexes. The other changes will be in subsequent PRs.

I'm using the existence of the index/default/index/.git directory to indicate whether or not a migration needs to be performed.

Related issue: #483

This only changes the directory structure that will be needed to support
custom indexes. The other changes will be in subsequent PRs.
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 21, 2020
@codecov-io
Copy link

codecov-io commented Feb 21, 2020

Codecov Report

Merging #505 into master will decrease coverage by 1.29%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #505     +/-   ##
=========================================
- Coverage   58.51%   57.21%   -1.3%     
=========================================
  Files          22       23      +1     
  Lines         969      991     +22     
=========================================
  Hits          567      567             
- Misses        346      368     +22     
  Partials       56       56
Impacted Files Coverage Δ
internal/indexmigration/migration.go 0% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fb5343a...4a0c3e0. Read the comment docs.

tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

_ = os.MkdirAll(tmpDir.Path(test.dirPath), os.ModePerm)
Copy link
Member

Choose a reason for hiding this comment

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

handle err :)

return err
}
if isMigrated {
klog.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.

please use leveled logs (e.g. klog.v(2) ) for stuff users won't see.

also "already migrated" is not a good log line by itself.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. I've seen that throughout the code base, is there some guide for what levels should be used in certain scenarios? I briefly looked through the klog repo but didn't see anything immediately.

Copy link
Member

Choose a reason for hiding this comment

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

Non-leveled logs: always visible to users
Leveled logs: never visible to users, unless -v LEVEL is specified.

We frequently use -v 2 or -v 10 etc for debugging.
So use judgement from the rest of the repo. :) If it's too off, we'll comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually wouldn't we want to display to the user that the migration has already been performed?

Copy link
Member

Choose a reason for hiding this comment

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

🤔 you’re right but this actually sounds like an error.
But both are fine since this is just a one off command.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok I've already changed it to use leveled logging, so I'll just leave it that way if you think either way is cool.

Comment on lines 54 to 58
err = gitutil.EnsureCloned(constants.IndexURI, paths.DefaultIndexPath())
if err != nil {
return err
}
return nil
Copy link
Member

Choose a reason for hiding this comment

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

this simply can be returned as "errors.Wrap"

func Migrate(paths environment.Paths) error {
isMigrated, err := Done(paths)
if err != nil {
return err
Copy link
Member

@ahmetb ahmetb Feb 21, 2020

Choose a reason for hiding this comment

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

we rarely do return err in this codebase.
try errors.Wrap(err, "failed to check if index migration is complete")

Comment on lines 62 to 63
// DefaultIndexPath returns the base directory where the default plugin index repository is cloned.
func (p Paths) DefaultIndexPath() string { return filepath.Join(p.base, "index", "default") }
Copy link
Member

Choose a reason for hiding this comment

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

I feel like we don't need a method like this. Something like we eventually need to refactor this to IndexPath(name string).

The method above (IndexPath()) ideally should not be necessary (except for some tests, e.g. below).

Copy link
Member Author

Choose a reason for hiding this comment

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

Ya that makes sense, I'll remove it since this will check for the .git directory.

Copy link
Member

Choose a reason for hiding this comment

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

SGTM.

func init() {
if _, ok := os.LookupEnv("X_KREW_ENABLE_MULTI_INDEX"); ok {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add the migration check behind this feature gate to the PreRunE of root as well?

Copy link
Member Author

@chriskim06 chriskim06 Feb 21, 2020

Choose a reason for hiding this comment

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

Sure I can add that, I didn't include the check in root to prevent other krew operations when the user hasn't migrated but it makes sense to include it in this PR

@ahmetb
Copy link
Member

ahmetb commented Feb 21, 2020

/lgtm
just one nit.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 21, 2020
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 21, 2020
@ahmetb
Copy link
Member

ahmetb commented Feb 21, 2020

/lgtm
/approve
Thanks.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 21, 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 added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 21, 2020
@k8s-ci-robot k8s-ci-robot merged commit df44611 into kubernetes-sigs:master Feb 21, 2020
@chriskim06 chriskim06 deleted the index-migration branch February 21, 2020 21:16
}

// Migrate removes the index directory and then clones krew-index to the new default index path.
func Migrate(paths environment.Paths) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

@chriskim06 This is currently untested. I think this is mostly because the migration code has external dependencies. What about renaming the directory index -> index/default? Then you could also test it :)

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'm not sure I totally understand what you mean about renaming the directory. If you could elaborate a little more I'd love to add a unit test for this. I was thinking that this would be covered more in integration tests after looking at the receiptmigration test.

Since this has the call to gitutil.EnsureCloned I didn't want that to be called during the unit test. Normally I would do something like using an interface for that function and a different implementation in my test that doesn't actually use git to mock that behavior, but I thought that might overcomplicate this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

So what I was trying to say is that the function is hard to test, because it has an external dependency due to the hidden git clone. After the current function exits, the result is the same as if $ mv index index/default was called (this is just pseudo-mv, it doesn't work like that :) ).

So if your implementation would rely on mv instead of git-clone, we could write a unit test for it, because file system operations are fine in unit tests. I think I'll give it a try to see if it works.

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops, I meant to reply to this yesterday. But ya, that makes sense, I didn't think to do it that way. It does seem more testable in your PR.

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.

5 participants