Using Go’s dep to organize your Kubernetes client-go dependencies

Vladimir Vivien
Programming Kubernetes
3 min readAug 18, 2017

--

Update: after you read this, please see comments from sam boyer for updates and changes to the dep project!

When you want to write Kubernetes API clients, one of the best way to do it is with the client-go project. The project is fast moving and changes all the time. Great efforts have been made to keep it sync’d with the main Kubernetes project and keep the versions stable. However, there is still some rough edges when it comes to setting up dependencies.

This write up shows how to use the dep tool to setup dependencies for the client-go project so that it build properly. This was inspired by the HeptioProtip series last week from Andy Goldstein where he showed how to manage dependencies for client-go using Godep.

Setting dep for client-go 4.0.0

Project client-go is built with Godep, so why am I using dep? Well, because dep is the future (that’s right, I am calling it). It is simple to use and requires little input from human or files to figure out dependency graph.

I will not go in depth about dep [read about it here].

Consider the following simple client that prints out all deployed pods in the cluster:

Since dep depends on the import statements in the source code to figure out its graph, make sure you have some import statements in your file. Next, follow these steps.

  1. Get dep tool
    Get dep with go get
    $> go get -u github.com/golang/dep/cmd/dep
    Or, you can also get dep with brew:
    $> brew install dep
  2. Initialize the project
    Change directory to the root package where your source is located and run the init command to initialize the project:
    $> dep init
    This will generate Gopkg.lock and Gopkg.toml. You should also see your vendor directory
  3. Ensure a specific version
    Next, we use dep to ensure dependency on client-go version 4.0.0 with the following command:
    $> dep ensure k8s.io/client-go@^4.0.0
  4. Specify api-machinery version
    At this point, if we attempt to run the code, it will fail. That is because project api-machinery does not semver tag the project and dep naively pulls tip from master. To get the correct version of api-machinery for client-go 4.0.0, update file Gopkg.toml to pull branch release-1.7 with the following:
[[constraint]]
name = "k8s.io/apimachinery"
branch = "release-1.7"

Now do dep ensureto apply the constraint declared above.

Next, you can run dep status to verify the proper dependencies are reflected:

$>dep status
...
k8s.io/apimachinery branch release-1.7 branch release-1.7 1fd2e63
k8s.io/client-go * v4.0.0 d92e849

At this point you can run the code with no issues:

go run listpods.go

Conclusion

Use dep when setting up dependencies for client-go (or elsewhere too). It’s easy and seems to work really well. To recap, here are the commands used above to setup dependency with dep:

dep init
dep ensure k8s.io/client-go@^4.0.0
dep ensure

Dont forget to update Gopkg.toml with the proper branch name for api-machinery.

Hope this will help you in your quest of writing Kubernetes clients.

Update: please see comments from sam boyer for updates and changes to the dep project since this was published!

--

--