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

Usage as a Go library with Go modules #98

Closed
garethr opened this issue May 1, 2019 · 8 comments · Fixed by #101
Closed

Usage as a Go library with Go modules #98

garethr opened this issue May 1, 2019 · 8 comments · Fixed by #101

Comments

@garethr
Copy link

garethr commented May 1, 2019

I'm relatively new to using Go modules, so some of this might be a little incorrect. A word of warning.

I wanted to use oras as a library, and successfully tried out the example provided in examples. I then wanted to recreate the functionality in oras which reuses the docker config to authenticate (in my case against an Azure registry. That led me to the code in the auth package.

In theory it should be enough to:

auth "github.com/deislabs/oras/pkg/auth/docker"

And then liberally copy the code from https://github.com/deislabs/oras/blob/master/cmd/oras/resolver.go#L22-L30.

In theory...

In practice this imports docker, which doesn't use tags on the repo to the go mod liking, which means you get an old version of Docker which imports logrus as with a capital letter "S" which causes the world to explode.

I think that means Oras is using Docker v17.03.2-ce, which was shipped 2 years ago https://github.com/moby/moby/releases.

go: github.com/Sirupsen/logrus@v1.4.1: parsing go.mod: unexpected module path "github.com/sirupsen/logrus"                                                                                           
go: error loading module requirements

Some context for this issue sirupsen/logrus#451.

I've got this working by:

  1. importing the latest version of Docker
go get github.com/docker/docker master
go mod tidy
  1. Updating where the AuthConfig type comes from in https://github.com/deislabs/oras/blob/master/pkg/auth/docker/resolver.go#L8
+ "github.com/docker/cli/cli/config/types"
- "github.com/docker/docker/api/types"

I think the fix here is to:

  • move over to modules from dep (this might be optional)
  • Find a suitable, modern, Docker tag to depend on
  • Update the types as noted above

At the moment anyone using Modules (recommended with 1.12) will run into this issue I think. It's possible this doesn't hit you if you're on macOS due to the case-incensitive filesystem, I've not tested that. The above was done on Linux.

@jdolitsky
Copy link
Contributor

Hey @garethr - we've only tried using this from dep-based project. I've just opened #99 to switch over to go modules, which hopefully would fix this? Will look into it in the near future unless you want to attempt :)

@garethr
Copy link
Author

garethr commented May 1, 2019

I don't think swapping over to Go modules will fix the issue directly, though it may trigger the issue which would make it more pressing.

I think the answer lies in "Find a suitable, modern, Docker tag to depend on". I'm not sure what the community consensus is there so lets ask nice Docker maintainer folks: @thaJeztah, @vdemeester, @silvin-lubecki.

@thaJeztah
Copy link

The problem is that go modules enforce the use of SemVer; what happens on the docker/moby repository is that

a) (as you mentioned) the moby repository has not tagged releases (releases are now tagged in the downstream docker/engine repository)
b) even if releases were tagged, go mod will ignore tags that do not match the SemVer format (I'm explicitly mentioning format, because it will assume any tag that's parseable as SemVer "is SemVer"). In case of the docker/moby repositories it will therefore use the 1.13.1 tag, which is parseable as SemVer

While go mod can make sense for small repositories that provide a single package, it becomes quite problematic for repositories that provide a large number of packages (not all of which may follow the same "release cadence", so should be versioned independently).

Things are still being discussed, but I think the current solution is to define a pseudo version in the go.mod (which must be higher than 1.13, otherwise go mod may downgrade to an older version), and specify a commit in that pseudo version; see an example in the BuildKit repository; https://github.com/moby/buildkit/blob/ac18391f2b29c73e0c87cafea8cb63e0c3ac6501/go.mod#L21

@jdolitsky
Copy link
Contributor

@garethr it looks like this is now fixed (thank you @olegsu and @thaJeztah!)

Please verify when you have time. I used the following to test this out:

rm -rf orastest/ && mkdir orastest/ && cd orastest/
wget -O main.go https://raw.githubusercontent.com/deislabs/oras/master/examples/simple_push_pull.go
export GO111MODULE=on
go mod init
go mod vendor

@jdolitsky
Copy link
Contributor

jdolitsky commented May 6, 2019

I've verified the set of commands above also work on Linux, running in official Go Docker container:

docker run --rm -it --entrypoint sh -w /go/src golang

@garethr
Copy link
Author

garethr commented May 7, 2019

On first importing master into my module like so:

go get -u github.com/deislabs/oras@master

I ran into several build failures, due to the change in types mentioned above.

# github.com/deislabs/oras/pkg/auth/docker
../../go/pkg/mod/github.com/deislabs/oras@v0.4.1-0.20190506165825-e903bf0b0930/pkg/auth/docker/login.go:39:50: cannot use cred (type "github.com/docker/docker/api/types".AuthConfig) as type "github.com/docker/cli/cli/config/types".AuthConfig in argument to c.primaryCredentialsStore(hostname).Store                                                                                                 
../../go/pkg/mod/github.com/deislabs/oras@v0.4.1-0.20190506165825-e903bf0b0930/pkg/auth/docker/resolver.go:27:13: cannot assign "github.com/docker/cli/cli/config/types".AuthConfig to auth (type "github.com/docker/docker/api/types".AuthConfig) in multiple assignment

This comes down to a coupling between the docker package and the docker/cli package. In order to correctly use oras in another module you need to pin the cli version like so:

go get -u github.com/docker/cli@v0.0.0-20181219132003-336b2a5cac7f 

You can see this is specified here: https://github.com/deislabs/oras/blob/master/go.mod#L14

I previously had github.com/docker/cli v0.0.0-20190429053159-3273c2e23546.

So things work, but I think it would be best to update the CLI package as well.

garethr added a commit to open-policy-agent/conftest that referenced this issue May 7, 2019
Oras now supports go modules in master, so update to use that. Details in oras-project/oras#98
@jdolitsky jdolitsky reopened this May 9, 2019
@jdolitsky
Copy link
Contributor

alright this should be fixed. @garethr, would you mind trying once again?

garethr added a commit to open-policy-agent/conftest that referenced this issue May 11, 2019
@garethr
Copy link
Author

garethr commented May 11, 2019

@jdolitsky just checked this by updating cli and oras to the latest versions and everything works for me. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants