logo

Kubernetes - Source Code

Kubernetes source code can be found on GitHub: https://github.com/kubernetes/kubernetes

Honestly Kubernetes source code is a bit hard to navigate for a new comer.

Sure the repo structure looks familiar, you will find cmd/ and pkg/ as in most of the Go projects, and pkg/api and pkg/apis matches what we learned about core and non-core APIs. However if you expect to find definitions of Pod in pkg/api/pod, you would be disappointed.

The actual definitions of the types can be found either in staging/src/k8s.io/api/core/v1/types.go of the same kubernetes/kubernetes repo, or in a totally different kubernetes/api repo (https://github.com/kubernetes/api/blob/master/core/v1/types.go). Why is that?

Staging

Kubernetes is developed in a mono-repo kubernetes/kubernetes. However some of the subprojects are developed in kubernetes/staging/src/k8s.io/ folder then synced (by a publishing-bot) to their own repos.

For example, the APIs are defined in staging/src/k8s.io/api, but will be synced to https://github.com/kubernetes/api. And why is API definition separate from the main Kubernetes code tree? The official doc says

It is published separately to avoid diamond dependency problems for users who depend on more than one of k8s.io/client-go, k8s.io/apimachinery, k8s.io/apiserver..."

Similarly many other components can be found in the staging folder and they have their own repos under https://github.com/kubernetes:

  • k8s.io/apimachinery: a shared dependency for servers and clients to work with Kubernetes API infrastructure without direct type dependencies.
  • k8s.io/apiserver: https://github.com/kubernetes/apiserver
  • k8s.io/client-go: depends on k8s.io/api and k8s.io/apimachinery
  • k8s.io/cli-runtime: common helper functions for CLI, used by kubectl; depends on client-go
  • k8s.io/kubectl: depends on cli-runtime. https://github.com/kubernetes/kubectl

Note on api and apimachinery: types (structs only) go to api, "machineries" (scheme, encoding, decoding, conversion, etc) go to apimachinery.

Structure

  • build/: scripts to build Kubernetes; more on build in the next section.
  • cmd/: the main functions
    • kube-apiserver/
    • kube-controller-manager/
    • kube-scheduler/
    • cloud-controller-manager/
    • kubelet/
    • kube-proxy/
    • CLI tools:
      • kubeadm
      • kubectl
  • pkg/
    • api/: the core apis
    • apis/: the non-core apis
    • kubeapiserver/:
      • admission/
      • authenticator/
      • authorizer/
    • kubectl
    • kubelet
  • staging: what we covered in the previous section.
  • test
  • vendor: many of them are symlinks to ../../staging/src/k8s.io/

Build

https://github.com/kubernetes/kubernetes/tree/master/build

Kubernetes uses containerized build: "This simplifies initial set up and provides for a very consistent build and test environment."

Using buildx: https://github.com/docker/buildx/blob/master/README.md

Objects

  • api/v1: API type definitions, e.g. pods (kind: Pod), nodes (Node)
  • apiextensions.k8s.io/v1: crds (CustomResourceDefinition), apiservices (APIService)