Building the Kubernetes go-client using dep

Building the Kubernetes go-client using dep

Kubernetes is an extensible system with a powerful API. A common problem is to compile the Kubernetes go-client, without errors due to dependencies.

In this post, I will show the steps to resolve dependencies using dep, the new Golang dependency manager developed with the goal of becoming part of the Go toolchain.

https://nirmata.io/

http://nirmata.io/

Managing dependencies has been a active topic for Golang (if you are interested in the details see:The Saga of Go Dependency Management).While dep is still new and changing, as of recently it is considered ready for production use!

A major advantage of dep is that it does not clog up your local GOPATH with dependencies. You can now have a single GOPATH and manage several projects, and their dependencies, in it.  (If you prefer using godep, checkout this great post from Andy Goldstein at Heptio.)

Briefly, the way dep works is that you have manage dependencies using “Gopkg.toml” file and dep uses this file and its dependency analysis to produce a “Gopkg.lock” file. These files are version controlled – it’s optional to check-in the vendor folder. If you want more information on dep, here is an excellent presentation by Carolyn Van Slyck at Rackspace.

dep: https://github.com/golang/dep

Here are the steps to build a project that uses the client-go library:

Step 1: Write code that references client-go

To manage a dependency, dep requires you have code that references the external package. A great reference to writing custom Kubernetes controllers, or other applications that use the Kubernetes API, is this KubeCon talk by Aaron Levy from CoreOS.

Using some of the techniques Aaron covers, here is a simple main.go file that initializes a Kubernetes client and lists all Pods in the cluster:

Step 2: run ‘dep init’

Once you have some code that references client-go, you can run dep to automatically analyse dependencies and generate the vendor artifacts:

This may take a while to complete. The “-v” flag provides details on the dependency analysis.

Step 3: update the client-go version in Gopkg.toml

When dep completes, it will generate a Gopkg.toml file. Update this to the version of client-go you want to use. Currently “v4.0.0-beta.0” is good option:

Step 4: update the API dependency in Gopkg.toml

Next, we need to make another edit to Gopkg.toml. The Kubernetes client-go depends on the API packages. So, we need to determine the correct revisions to use.

The easiest way to determine this is to check the client-go Git repo. For example, since I selected the “v4.0.0-beta.0” version I can browse to:

https://github.com/kubernetes/client-go/tree/v4.0.0-beta.0

Then select Godeps and view the Godeps.json file. In there , searching for “apimachinery” reveals the revision used to compile the “v4.0.0-beta.0” version of client-go:

Copy this revision and add it to the Gopkg.toml file as an override. The final Gopkg.toml should look like this:

Step 5: run ‘dep ensure’ 

Finally, run ‘dep ensure’ to re-generate the Gopkg.lock file and update the vendor folder:

One this completes, you can build your code as you normally would:

Summary

Kubernetes is a fast moving project, and can seem daunting at first. It took me a few attempts, and lots of web searches, to figure out how to reliably compile and build applications the use the client-go package. I hope this post helps some of you avoid that pain, and get started with building Kubernetes solutions more easily.

 

Nirmata joins the CNCF!
Service Availability for Container-Native Applications
1 Comment
  • Reinhard Lüdiger

    July 20, 2018 at 7:14 am Reply

    This blog post helped me a lot to get the dependencies of k8s.io/client-go under control.

    kind regards

    Reinhard Lüdiger

Post a Comment