client-go: k8s.io/client-go vendors k8s.io/apimachinery which breaks build

TL;DR: libraries must not vendor their dependencies

It is not possible to build programs that vendor k8s.io/client-go as the latter has vendored k8s.io/apimachinery.

Sample program:

package main

import (
        "flag"
        "fmt"
        "log"
        "os"
        "path/filepath"
        "time"

        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/client-go/tools/clientcmd"

        "k8s.io/client-go/kubernetes"
)

func main() {
        kubeconfig := flag.String("kubeconfig", filepath.Join(os.Getenv("HOME"), ".kube", "config"), "absolute path to the kubeconfig file")

        flag.Parse()
        // uses the current context in kubeconfig
        config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
        check(err)

        clientset, err := kubernetes.NewForConfig(config)
        check(err)

        for {
                pods, err := clientset.Core().Pods("").List(metav1.ListOptions{})
                check(err)

                fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
                time.Sleep(10 * time.Second)
        }
}

func check(err error) {
        if err != nil {
                log.Fatalf("fatal: %v", err)
        }
}

This program will not compile because the vendored copy of k8s.io/apimachinery (https://github.com/kubernetes/client-go/tree/master/vendor/k8s.io/apimachinery) aliases the copy in my own project, this breaks type equality.

# stash.atlassian.com/scm/kube/rds-resource-provider/cmd/postgres-resource-provider
cmd/postgres-resource-provider/main.go:29: cannot use "stash.atlassian.com/scm/kube/rds-resource-provider/vendor/k8s.io/apimachinery/pkg/apis/meta/v1".ListOptions literal (type "stash.atlassian.com/scm/kube/rds-resource-provider/vendor/k8s.io/apimachinery/pkg/apis/meta/v1".ListOptions) as type "stash.atlassian.com/scm/kube/rds-resource-provider/vendor/k8s.io/client-go/vendor/k8s.io/apimachinery/pkg/apis/meta/v1".ListOptions in argument to clientset.Core().Pods("").List
Makefile:11: recipe for target 'build' failed
make: *** [build] Error 2

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 16
  • Comments: 32 (25 by maintainers)

Most upvoted comments

So, I’m sure you all are familiar with https://github.com/ericchiang/k8s, they manage to provide from what I can tell the same functionality with only two dependencies. I am curious from a design perspective what advantage your numerous dependencies have over their two dependency solution?

dpw/vendetta. I’ve worked around the issue for the moment.

Please feel free to close this issue.

On Wed, Feb 1, 2017 at 8:39 PM, Chao Xu notifications@github.com wrote:

This program will not compile because the vendored copy of k8s.io/apimachinery (https://github.com/kubernetes/client-go/tree/ master/vendor/k8s.io/apimachinery) aliases the copy in my own project

@davecheney https://github.com/davecheney do you use a vendor package management tool? Both godep and glide will flatten the dependencies.

However, godep won’t handle version conflicts in dependencies. This is why we want to switch to dep or glide, which supports specifying a range of versions, lowing the probability of conflicts.

For now, can you use the release-2.0 branch of client-go? That branch doesn’t depend on k8s.io/apimachinary.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kubernetes/client-go/issues/83#issuecomment-276613181, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAcA0CWgCsBYohiA8mIaGTztY2-Vcoyks5rYFK5gaJpZM4LyGG0 .

This program will not compile because the vendored copy of k8s.io/apimachinery (https://github.com/kubernetes/client-go/tree/master/vendor/k8s.io/apimachinery) aliases the copy in my own project

@davecheney do you use a vendor package management tool? Both godep and glide will flatten the dependencies.

However, godep won’t handle version conflicts in dependencies. This is why we want to switch to dep or glide, which supports specifying a range of versions, lowing the probability of conflicts.

For now, can you use the release-2.0 branch of client-go? That branch doesn’t depend on k8s.io/apimachinary.

@lavalamp no argument there, vendor/ is utterly broken for library writers. /cc @spf13

We are working on switching to the new dep vendoring management tool, after which this should be acceptable. (You’ll run dep ensure and it will flatten the dependencies.)

This tool is marked pre alpha, are you sure that is prudent?

Can you at least update the vendored copy of k8s.io/apimachinery ?