Development Guide

Setting Up The Development Environment

Here's an example of how you'd configure a Kubernetes development environment on Ubuntu:

apt-get install -y gcc make socat git build-essential

# Install Docker
sh -c 'echo"deb https://apt.dockerproject.org/repo ubuntu-$(lsb_release -cs) main"> /etc/apt/sources.list.d/docker.list'
curl -fsSL https://apt.dockerproject.org/gpg | sudo apt-key add -
apt-key fingerprint 58118E89F3A912897C070ADBF76221572C52609D
apt-get update
apt-get -y install "docker-engine=1.13.1-0~ubuntu-$(lsb_release -cs)"

# Install etcd
ETCD_VER=v3.2.18
DOWNLOAD_URL="https://github.com/coreos/etcd/releases/download"
curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
sudo /bin/cp -f etcd-${ETCD_VER}-linux-amd64/{etcd,etcdctl} /usr/bin
rm -rf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz etcd-${ETCD_VER}-linux-amd64

# Install Go
curl -sL https://storage.googleapis.com/golang/go1.10.2.linux-amd64.tar.gz | tar -C /usr/local -zxf -
export GOPATH=/gopath
export PATH=$PATH:$GOPATH/bin:/usr/local/bin:/usr/local/go/bin/

# Download Kubernetes code
mkdir -p $GOPATH/src/k8s.io
git clone https://github.com/kubernetes/kubernetes $GOPATH/src/k8s.io/kubernetes
cd $GOPATH/src/k8s.io/kubernetes

# Start a local cluster
export KUBERNETES_PROVIDER=local
hack/local-up-cluster.sh

Open another terminal, configure kubectl and you're all set:

cd $GOPATH/src/k8s.io/kubernetes
export KUBECONFIG=/var/run/kubernetes/admin.kubeconfig
cluster/kubectl.sh

Unit Tests

Unit testing is an indispensable aspect of Kubernetes development. Code modifications generally come with corresponding unit tests update or addition. These tests can be run directly on different systems, such as OSX, Linux, etc.

For instance, after modifying the pkg/kubelet/kuberuntime code,

# Test with the Go package’s full path
go test -v k8s.io/kubernetes/pkg/kubelet/kuberuntime
# Or with the relative directory
go test -v ./pkg/kubelet/kuberuntime

End-To-End (e2e) Tests

End-to-end (e2e) tests require the launch of a Kubernetes cluster and can only be run on a Linux system.

Here's an example of how to launch the tests locally:

make WHAT='test/e2e/e2e.test'
make ginkgo

export KUBERNETES_PROVIDER=local
go run hack/e2e.go -v -test --test_args='--ginkgo.focus=Port\sforwarding'
go run hack/e2e.go -v -test --test_args='--ginkgo.focus=Feature:SecurityContext'

Note: Each PR in Kubernetes automatically triggers a series of e2e tests.

Node e2e Tests

Node e2e tests involve starting the Kubelet and can currently only be run on Linux systems.

export KUBERNETES_PROVIDER=local
make test-e2e-node FOCUS="InitContainer"

Note: Each PR in Kubernetes automatically triggers node e2e tests.

Useful git Commands

Many times, PRs need to be fetched locally for testing. To pull PR #365, for instance, you'd use

git fetch upstream pull/365/merge:branch-fix-1
git checkout branch-fix-1

You can also configure .git/config to fetch all PRs using git fetch (be warned, however, that Kubernetes has a large PR count, thus this process may take a while):

fetch = +refs/pull/*:refs/remotes/origin/pull/*

Additional Resources

最后更新于