Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glog flag redefined error

I am using glog flag log_dir in my project. Recently I imported kubernetes library and started getting this runtime panic

panic: ./aaa.test flag redefined: log_dir

May 16 23:51:35 ecmdev03-core01 docker[26867]: goroutine 1 [running]:
May 16 23:51:35 ecmdev03-core01 docker[26867]: panic(0x15ebc60, 0xc8201aae90)
May 16 23:51:35 ecmdev03-core01 docker[26867]:         /usr/local/go/src/runtime/panic.go:464 +0x3e6
May 16 23:51:35 ecmdev03-core01 docker[26867]: flag.(*FlagSet).Var(0xc8200160c0, 0x7f561118c1c0, 0xc8201aae40, 0x1bddd70, 0x7, 0x1d75860, 0x2f)
May 16 23:51:35 ecmdev03-core01 docker[26867]:         /usr/local/go/src/flag/flag.go:776 +0x454
May 16 23:51:35 ecmdev03-core01 docker[26867]: flag.(*FlagSet).StringVar(0xc8200160c0, 0xc8201aae40, 0x1bddd70, 0x7, 0x0, 0x0, 0x1d75860, 0x2f)
May 16 23:51:35 ecmdev03-core01 docker[26867]:         /usr/local/go/src/flag/flag.go:679 +0xc7
May 16 23:51:35 ecmdev03-core01 docker[26867]: flag.(*FlagSet).String(0xc8200160c0, 0x1bddd70, 0x7, 0x0, 0x0, 0x1d75860, 0x2f, 0xc8201aae30)
May 16 23:51:35 ecmdev03-core01 docker[26867]:         /usr/local/go/src/flag/flag.go:692 +0x83
May 16 23:51:35 ecmdev03-core01 docker[26867]: flag.String(0x1bddd70, 0x7, 0x0, 0x0, 0x1d75860, 0x2f, 0xba3950)
May 16 23:51:35 ecmdev03-core01 docker[26867]:         /usr/local/go/src/flag/flag.go:699 +0x5f
May 16 23:51:35 ecmdev03-core01 docker[26867]: k8s.io/kubernetes/vendor/github.com/golang/glog.init()
May 16 23:51:35 ecmdev03-core01 docker[26867]:         /src/ecm_infra/go/src/k8s.io/kubernetes/vendor/github.com/golang/glog/glog_file.go:41 +0x13e
May 16 23:51:35 ecmdev03-core01 docker[26867]: k8s.io/kubernetes/pkg/labels.init()
May 16 23:51:35 ecmdev03-core01 docker[26867]:         /src/ecm_infra/go/src/k8s.io/kubernetes/pkg/labels/selector.go:810 +0x6b
May 16 23:51:35 ecmdev03-core01 docker[26867]: k8s.io/kubernetes/pkg/api/unversioned.init()
May 16 23:51:35 ecmdev03-core01 docker[26867]:         /src/ecm_infra/go/src/k8s.io/kubernetes/pkg/api/unversioned/well_known_labels.go:30 +0x6f
May 16 23:51:35 ecmdev03-core01 docker[26867]: k8s.io/kubernetes/pkg/api.init()
May 16 23:51:35 ecmdev03-core01 docker[26867]:         /src/ecm_infra/go/src/k8s.io/kubernetes/pkg/api/types.go:2731 +0x64

It looks like glog library which is vendored by k8s is conflicting with the one that I am using. Is introducing vendor directory a solution for this problem? Should I use vendoring libraries like glide, govendor, gb etc? If yes, which one is preferred?

like image 864
Smithsonian Avatar asked May 17 '16 19:05

Smithsonian


4 Answers

That error doesn't have to do with conflicting libraries, it is a conflicting flag (log_dir). It means you're adding a "--log_dir" flag, and the glog library used by kubernetes also has a log_dir flag. This is a problem with adding flags in libraries during package init. Unfortunately vendoring won't change anything. You might be able to work around this by manipulating the flag.CommandLine global variable to point to a different flag.FlagSet when you import your log library or kubernetes, but that will be tricky since it depends on import ordering.

like image 74
Tim Allclair Avatar answered Oct 11 '22 13:10

Tim Allclair


I faced a similar issue when vendorizing my dependencies using glide.

According to https://github.com/kubernetes/kubernetes/issues/25572 kubernetes does not have a glide.lock / glide.yml, dependency flattening does not happen and in the end glog gets defined twice since vendor/k8s.io/kubernetes/vendor is also included.

The workaround from the aformentioned link worked good to me:

glide install --strip-vendor --strip-vcs

like image 24
Jefferson Girao Avatar answered Oct 11 '22 14:10

Jefferson Girao


I use k8s library and faced flag redefined: log_dir problem too. I found several packages of kubernetes are not in my vendor.Then I govendor add them, then it was fixed.

I guess two copy of glog was built (one in my vendor folder, one in k8s.io/kubernetes/vendor in my $GOPATH ) when those packages are not found in my vendor folder.

How to find out missing packages? I use stupid way, rename $GOPATH/src/k8s.io to something else and build my project, see what was not found.Then add it~

like image 3
Tong Avatar answered Oct 11 '22 13:10

Tong


I was seeing flag redefined: log_dir error and the stack trace pointed glog/glog_file.go:41 +0xd3.

The root cause of the problem was the GOPATH.

I had two copies of the same repo at different locations. While trying to run tests in one of the copies, the go package was picked from the other repo. So I had two definitions of everything in glog.

I renamed the other repo to a new name, and then it started working.

So, please check your GOPATH and make sure you are not importing libraries from somewhere else.

like image 1
Vishal Kanaujia Avatar answered Oct 11 '22 12:10

Vishal Kanaujia