准备工作

  1. 获取代码

  2. 切换分支release-1.21, commitID:1a401979386

  3. 概览代码的情况,可以看到当前版本的kubernetes代码共有440万+行代码,其中go代码占了356万+行

    ❯ cloc .
       23659 text files.
       18378 unique files.
        6789 files ignored.
    
    7 errors:
    Line count, exceeded timeout:  ./plugin/pkg/auth/authorizer/rbac/bootstrappolicy/policy.go
    Line count, exceeded timeout:  ./vendor/golang.org/x/net/idna/tables10.0.0.go
    Line count, exceeded timeout:  ./vendor/golang.org/x/net/idna/tables11.0.0.go
    Line count, exceeded timeout:  ./vendor/golang.org/x/net/idna/tables12.0.0.go
    Line count, exceeded timeout:  ./vendor/golang.org/x/net/idna/tables13.0.0.go
    Line count, exceeded timeout:  ./vendor/golang.org/x/net/idna/tables9.0.0.go
    Line count, exceeded timeout:  ./vendor/k8s.io/api/extensions/v1beta1/types_swagger_doc_generated.go
    
    github.com/AlDanial/cloc v 1.90  T=59.92 s (282.4 files/s, 96715.7 lines/s)
    --------------------------------------------------------------------------------
    Language                      files          blank        comment           code
    --------------------------------------------------------------------------------
    Go                            14576         474019         842221        3569843
    JSON                            280              4              0         608735
    YAML                            839           1174           1279         116804
    Markdown                        479          10735              0          30778
    Bourne Shell                    328           5978          11764          29440
    PO File                          12           1693           2374          18936
    Assembly                        107           1987           2604          11257
    Protocol Buffers                113           5265          17261          10444
    make                             66            508            915           4054
    PowerShell                        7            402           1051           2493
    C/C++ Header                      1            399           4367            839
    Bourne Again Shell               12             89             74            773
    Dockerfile                       56            243            808            490
    Lua                               1             30             26            453
    sed                               4              4             32            438
    Python                            7            119            157            412
    SVG                               4              4              4            378
    TOML                             11            141            153            278
    XML                               5              0              0            198
    C                                 7             49             79            156
    Bazel                             4             10              0            118
    INI                               1              2              0             10
    HTML                              3              0              0              3
    CSV                               1              0              0              1
    --------------------------------------------------------------------------------
    SUM:                          16924         502855         885169        4407331
    --------------------------------------------------------------------------------

mac本地构建

  • 硬件:macbook pro 18

  • OS:mac os 11.4

  • go version go1.16.3 darwin/amd64

  • bash:最小版本4.2,使用brew install bash升级mac自带的bash

  • 根据kubernetes开发指南,如果需要在本地开发环境直接构建kubernetes,需要准备一些GNU工具

    brew install coreutils ed findutils gawk gnu-sed gnu-tar grep make

    然后将以下语句加入你的~/.bashrc中,使得GNU工具优先被使用

    GNUBINS="$(find /usr/local/opt -type d -follow -name gnubin -print)"
    
    for bindir in ${GNUBINS[@]}
    do
      export PATH=$bindir:$PATH
    done
    
    export PATH
  • 使用make help可以查看makefile中各个target的帮助信息,比如以下节选的all的帮助信息

    ❯ make help
    --------------------------------------------------------------------------------
    all
    # Build code.
    #
    # Args:
    #   WHAT: Directory names to build.  If any of these directories has a 'main'
    #     package, the build will produce executable files under _output/bin.
    #     If not specified, "everything" will be built.
    #   GOFLAGS: Extra flags to pass to 'go' when building.
    #   GOLDFLAGS: Extra linking flags passed to 'go' when building.
    #   GOGCFLAGS: Additional go compile flags passed to 'go' when building.
    #
    # Example:
    #   make
    #   make all
    #   make all WHAT=cmd/kubelet GOFLAGS=-v
    #   make all GOLDFLAGS=""
    #     Note: Specify GOLDFLAGS as an empty string for building unstripped binaries, which allows
    #           you to use code debugging tools like delve. When GOLDFLAGS is unspecified, it defaults
    #           to "-s -w" which strips debug information. Other flags that can be used for GOLDFLAGS
    #           are documented at https://golang.org/cmd/link/
    ---------------------------------------------------------------------------------
    ...
  • 正式构建:make all

    • 注意:如果日常使用的是zsh,在zsh下即使上面流程都准备无误,直接执行make all也无法正常构建,需要手动进入bash后再make

    • 实际调用的是shell脚本:hack/make-rules/build.sh

    • 主要流程:kube::golang::build_binaries函数及kube::golang::place_bins

    • 这种方式构建的二进制为当前机器平台

    • make all后加WHAT参数可以只构建部分二进制。通过在shell代码中查找,可以找到所有支持的二进制

      # kubernetes在linux服务器上会用到的binary
      kube::golang::server_targets() {
        local targets=(
          cmd/kube-proxy
          cmd/kube-apiserver
          cmd/kube-controller-manager
          cmd/kubelet
          cmd/kubeadm
          cmd/kube-scheduler
          vendor/k8s.io/kube-aggregator
          vendor/k8s.io/apiextensions-apiserver
          cluster/gce/gci/mounter
        )
        echo "${targets[@]}"
      }
      
      # 各个平台都会用到的客户端binary
      readonly KUBE_CLIENT_TARGETS=(
        cmd/kubectl
        cmd/kubectl-convert
      )
      
      # 测试用到的binary,支持跨平台
      kube::golang::test_targets() {
        local targets=(
          cmd/gendocs
          cmd/genkubedocs
          cmd/genman
          cmd/genyaml
          cmd/genswaggertypedocs
          cmd/linkcheck
          vendor/github.com/onsi/ginkgo/ginkgo
          test/e2e/e2e.test
          cluster/images/conformance/go-runner
        )
        echo "${targets[@]}"
      }
      
      # 在集群中跑的测试用binary,只需要构建linux平台
      kube::golang::server_test_targets() {
        local targets=(
          cmd/kubemark
          vendor/github.com/onsi/ginkgo/ginkgo
        )
      
        if [[ "${OSTYPE:-}" == "linux"* ]]; then
          targets+=( test/e2e_node/e2e_node.test )
        fi
      
        echo "${targets[@]}"
      }
    • 构建结果输出

      bash-5.1$ make all
      +++ [0614 15:37:24] Building go targets for darwin/amd64:
          ./vendor/k8s.io/code-generator/cmd/prerelease-lifecycle-gen
      Generating prerelease lifecycle code for 27 targets
      +++ [0614 15:37:30] Building go targets for darwin/amd64:
          ./vendor/k8s.io/code-generator/cmd/deepcopy-gen
      Generating deepcopy code for 227 targets
      +++ [0614 15:37:43] Building go targets for darwin/amd64:
          ./vendor/k8s.io/code-generator/cmd/defaulter-gen
      Generating defaulter code for 89 targets
      +++ [0614 15:37:56] Building go targets for darwin/amd64:
          ./vendor/k8s.io/code-generator/cmd/conversion-gen
      Generating conversion code for 124 targets
      +++ [0614 15:38:26] Building go targets for darwin/amd64:
          ./vendor/k8s.io/kube-openapi/cmd/openapi-gen
      Generating openapi code for KUBE
      Generating openapi code for AGGREGATOR
      Generating openapi code for APIEXTENSIONS
      Generating openapi code for CODEGEN
      Generating openapi code for SAMPLEAPISERVER
      +++ [0614 15:38:39] Building go targets for darwin/amd64:
          ./vendor/github.com/go-bindata/go-bindata/go-bindata
      +++ [0614 15:38:41] Building go targets for darwin/amd64:
          cmd/kube-proxy
          cmd/kube-apiserver
          cmd/kube-controller-manager
          cmd/kubelet
          cmd/kubeadm
          cmd/kube-scheduler
          vendor/k8s.io/kube-aggregator
          vendor/k8s.io/apiextensions-apiserver
          cluster/gce/gci/mounter
          cmd/kubectl
          cmd/kubectl-convert
          cmd/gendocs
          cmd/genkubedocs
          cmd/genman
          cmd/genyaml
          cmd/genswaggertypedocs
          cmd/linkcheck
          vendor/github.com/onsi/ginkgo/ginkgo
          test/e2e/e2e.test
          cluster/images/conformance/go-runner
          cmd/kubemark
          vendor/github.com/onsi/ginkgo/ginkgo
    • 构建产物位于_output/bin

    • 清理:make clean

容器构建

  • 宿主机信息同上

  • docker desktop for mac 3.3.3(64133)

  • make quick-release

    • KUBE_RELEASE_RUN_TESTS = n KUBE_FASTBUILD = true build/release.sh
  • make release

    • 相比于quick-release,移除了两个环境变量配置,移除KUBE_RELEASE_RUN_TESTS=n会运行测试,移除KUBE_FASTBUILD=true会交叉编译
  • 构建平台的确定:kube::golang::setup_platforms

  • 构建结果输出

    ❯ make quick-release
    +++ [0614 16:16:14] Verifying Prerequisites....
    +++ [0614 16:16:14] Using Docker for MacOS
    +++ [0614 16:16:15] Building Docker image kube-build:build-4d0e18f23b-5-v1.16.5-1
    +++ [0614 16:24:43] Creating data container kube-build-data-4d0e18f23b-5-v1.16.5-1
    +++ [0614 16:24:50] Syncing sources to container
    Object "-Version" is unknown, try "ip help".
    +++ [0614 16:25:06] Running build command...
    +++ [0614 16:25:17] Building go targets for linux/amd64:
        ./vendor/k8s.io/code-generator/cmd/prerelease-lifecycle-gen
    Generating prerelease lifecycle code for 27 targets
    +++ [0614 16:25:22] Building go targets for linux/amd64:
        ./vendor/k8s.io/code-generator/cmd/deepcopy-gen
    Generating deepcopy code for 227 targets
    +++ [0614 16:25:30] Building go targets for linux/amd64:
        ./vendor/k8s.io/code-generator/cmd/defaulter-gen
    Generating defaulter code for 89 targets
    +++ [0614 16:25:39] Building go targets for linux/amd64:
        ./vendor/k8s.io/code-generator/cmd/conversion-gen
    Generating conversion code for 124 targets
    +++ [0614 16:25:58] Building go targets for linux/amd64:
        ./vendor/k8s.io/kube-openapi/cmd/openapi-gen
    Generating openapi code for KUBE
    Generating openapi code for AGGREGATOR
    Generating openapi code for APIEXTENSIONS
    Generating openapi code for CODEGEN
    Generating openapi code for SAMPLEAPISERVER
    +++ [0614 16:26:14] Building go targets for linux/amd64:
        ./vendor/github.com/go-bindata/go-bindata/go-bindata
    +++ [0614 16:26:15] Building go targets for linux/amd64:
        cmd/kube-proxy
        cmd/kube-apiserver
        cmd/kube-controller-manager
        cmd/kubelet
        cmd/kubeadm
        cmd/kube-scheduler
        vendor/k8s.io/kube-aggregator
        vendor/k8s.io/apiextensions-apiserver
        cluster/gce/gci/mounter
    Generating prerelease lifecycle code for 27 targets
    Generating deepcopy code for 207 targets
    Generating defaulter code for 89 targets
    Generating conversion code for 5 targets
    +++ [0614 16:36:43] Building go targets for linux/amd64:
        cmd/kube-proxy
        cmd/kubeadm
        cmd/kubelet
    +++ [0614 16:37:02] Multiple platforms requested, but available 4G < threshold 20G, building platforms in serial
    +++ [0614 16:37:02] Building go targets for darwin/amd64:
        cmd/kubectl
        cmd/kubectl-convert
    +++ [0614 16:39:05] Building go targets for linux/amd64:
        cmd/kubectl
        cmd/kubectl-convert
    +++ [0614 16:39:43] Multiple platforms requested, but available 4G < threshold 20G, building platforms in serial
    +++ [0614 16:39:43] Building go targets for darwin/amd64:
        cmd/gendocs
        cmd/genkubedocs
        cmd/genman
        cmd/genyaml
        cmd/genswaggertypedocs
        cmd/linkcheck
        vendor/github.com/onsi/ginkgo/ginkgo
        test/e2e/e2e.test
        cluster/images/conformance/go-runner
    +++ [0614 16:47:27] Building go targets for linux/amd64:
        cmd/gendocs
        cmd/genkubedocs
        cmd/genman
        cmd/genyaml
        cmd/genswaggertypedocs
        cmd/linkcheck
        vendor/github.com/onsi/ginkgo/ginkgo
        test/e2e/e2e.test
        cluster/images/conformance/go-runner
    +++ [0614 16:52:35] Building go targets for linux/amd64:
        cmd/kubemark
        vendor/github.com/onsi/ginkgo/ginkgo
        test/e2e_node/e2e_node.test
    +++ [0614 16:53:38] Syncing out of container
    Object "-Version" is unknown, try "ip help".
    +++ [0614 16:54:44] Starting tarball: client darwin-amd64
    +++ [0614 16:54:44] Building tarball: src
    +++ [0614 16:54:44] Building tarball: manifests
    +++ [0614 16:54:44] Starting tarball: client linux-amd64
    +++ [0614 16:54:44] Waiting on tarballs
    gtar: Removing leading `/' from member names
    gtar: Removing leading `/' from hard link targets
    gtar: Removing leading `/' from member names
    gtar: Removing leading `/' from member names
    gtar: Removing leading `/' from hard link targets
    gtar: Removing leading `/' from member names
    +++ [0614 16:54:50] Building tarball: node linux-amd64
    +++ [0614 16:54:50] Building images: linux-amd64
    +++ [0614 16:54:51] Starting docker build for image: kube-apiserver-amd64
    +++ [0614 16:54:51] Starting docker build for image: kube-controller-manager-amd64
    +++ [0614 16:54:51] Starting docker build for image: kube-scheduler-amd64
    +++ [0614 16:54:51] Starting docker build for image: kube-proxy-amd64
    +++ [0614 16:55:09] Deleting docker image k8s.gcr.io/kube-scheduler-amd64:v1.21.2-rc.0.69_1a401979386052
    +++ [0614 16:55:16] Deleting docker image k8s.gcr.io/kube-controller-manager-amd64:v1.21.2-rc.0.69_1a401979386052
    +++ [0614 16:55:28] Deleting docker image k8s.gcr.io/kube-proxy-amd64:v1.21.2-rc.0.69_1a401979386052
    +++ [0614 16:55:30] Deleting docker image k8s.gcr.io/kube-apiserver-amd64:v1.21.2-rc.0.69_1a401979386052
    +++ [0614 16:55:30] Docker builds done
    +++ [0614 16:55:30] Building tarball: server linux-amd64
    +++ [0614 16:56:17] Building tarball: final
    +++ [0614 16:56:17] Waiting on test tarballs
    +++ [0614 16:56:17] Starting tarball: test linux-amd64
    +++ [0614 16:56:50] Building tarball: test portable
  • 构建产物_output

参考

community/development.md at master · kubernetes/community (github.com):kubernetes开发指南

Kubernetes源码剖析 (豆瓣) (douban.com)

文章目录