Using Go’s dep to organize your Kubernetes client-go dependencies
Update: after you read this, please see comments from sam boyer for updates and changes to the
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.
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:
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.
- Get dep tool
Get dep with
$> go get -u github.com/golang/dep/cmd/dep
Or, you can also get dep with
$> brew install dep
- Initialize the project
Change directory to the root package where your source is located and run the
initcommand to initialize the project:
$> dep init
This will generate
Gopkg.toml. You should also see your
- 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
- Specify api-machinery version
At this point, if we attempt to run the code, it will fail. That is because project
api-machinerydoes not semver tag the project and
depnaively 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.7with the following:
name = "k8s.io/apimachinery"
branch = "release-1.7"
dep ensureto apply the constraint declared above.
Next, you can run
dep status to verify the proper dependencies are reflected:
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
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 ensure k8s.io/client-go@^4.0.0
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!