Kubernetesをソースからビルトする

# 概要

ときとき Kubernetes のソースを編集したいので、この記事は Kubernetes をソースからビルとする手順の記録です。

# 環境

* Ubuntu 20.04
* Kubernetes v1.18.3

## 依頼されたものを準備

Go と jq と build-essential

```sh
$ apt-get update && apt-get install -y golang-go jq build-essential
```

Dockerをインストールする

```sh
$ curl -sLO https://download.docker.com/linux/ubuntu/dists/focal/pool/stable/amd64/containerd.io_1.2.13-2_amd64.deb
$ curl -sLO https://download.docker.com/linux/ubuntu/dists/focal/pool/stable/amd64/docker-ce_19.03.11~3-0~ubuntu-focal_amd64.deb
$ curl -sLO https://download.docker.com/linux/ubuntu/dists/focal/pool/stable/amd64/docker-ce-cli_19.03.11~3-0~ubuntu-focal_amd64.deb

$ dpkg -i containerd.io_1.2.13-2_amd64.deb
$ dpkg -i docker-ce-cli_19.03.11~3-0~ubuntu-focal_amd64.deb
$ dpkg -i docker-ce_19.03.11~3-0~ubuntu-focal_amd64.deb
```

## ソースコートをダウンロード

```sh
$ wget https://github.com/kubernetes/kubernetes/archive/v1.18.3.tar.gz -O kubernetes_v1.18.3.tar.gz
$ tar -xzf kubernetes_v1.18.3.tar.gz
```

# ビルトする

デバッグシンボルがほしかったので、`GOLDFLAGS`に`-s`を加えない。

```sh
$ cd kubernetes_v1.18.3
$ make quick-release-images GOGCFLAGS="-N -l" GOLDFLAGS="" KUBE_BUILD_CONFORMANCE=n KUBE_FASTBUILD=true KUBE_VERBOSE=1 KUBE_BUILD_HYPERKUBE=n
```
イメージをロードする

```sh
$ docker load -i _output/release-images/amd64/kube-apiserver.tar
e5fbf08fb194: Loading layer [==================================================>]  162.4MB/162.4MB
Loaded image: k8s.gcr.io/kube-apiserver-amd64:v1.18.3
$ docker tag k8s.gcr.io/kube-apiserver-amd64:v1.18.3 k8s.gcr.io/kube-apiserver:v1.18.3
```

最後にデバッグシンボルがいることを確認する

```sh
$ readelf -S $(docker inspect k8s.gcr.io/kube-apiserver:v1.18.3 -f '{{.GraphDriver.Data.UpperDir}}')/usr/local/bin/kube-apiserver
There are 25 section headers, starting at offset 0x1c8:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .text             PROGBITS         0000000000401000  00001000
       00000000032a9828  0000000000000000  AX       0     0     16
  [ 2] .rodata           PROGBITS         00000000036ab000  032ab000
       0000000001bd7be1  0000000000000000   A       0     0     32
  [ 3] .shstrtab         STRTAB           0000000000000000  04e82c00
       00000000000001a1  0000000000000000           0     0     1
  [ 4] .typelink         PROGBITS         0000000005282dc0  04e82dc0
       0000000000036a14  0000000000000000   A       0     0     32
  [ 5] .itablink         PROGBITS         00000000052b97d8  04eb97d8
       0000000000009ad0  0000000000000000   A       0     0     8
  [ 6] .gosymtab         PROGBITS         00000000052c32a8  04ec32a8
       0000000000000000  0000000000000000   A       0     0     1
  [ 7] .gopclntab        PROGBITS         00000000052c32c0  04ec32c0
       000000000236f73b  0000000000000000   A       0     0     32
  [ 8] .go.buildinfo     PROGBITS         0000000007633000  07233000
       0000000000000020  0000000000000000  WA       0     0     16
  [ 9] .noptrdata        PROGBITS         0000000007633020  07233020
       0000000000094ec0  0000000000000000  WA       0     0     32
  [10] .data             PROGBITS         00000000076c7ee0  072c7ee0
       0000000000025f10  0000000000000000  WA       0     0     32
  [11] .bss              NOBITS           00000000076ede00  072ede00
       0000000000030610  0000000000000000  WA       0     0     32
  [12] .noptrbss         NOBITS           000000000771e420  0731e420
       0000000000003e88  0000000000000000  WA       0     0     32
  [13] .zdebug_abbrev    PROGBITS         0000000007723000  072ee000
       0000000000000119  0000000000000000           0     0     8
  [14] .zdebug_line      PROGBITS         0000000007723119  072ee119
       000000000033ffd5  0000000000000000           0     0     8
  [15] .zdebug_frame     PROGBITS         0000000007a630ee  0762e0ee
       000000000012c31d  0000000000000000           0     0     8
  [16] .zdebug_pubnames  PROGBITS         0000000007b8f40b  0775a40b
       00000000000192f9  0000000000000000           0     0     8
  [17] .zdebug_pubtypes  PROGBITS         0000000007ba8704  07773704
       00000000000b5edb  0000000000000000           0     0     8
  [18] .debug_gdb_script PROGBITS         0000000007c5e5df  078295df
       000000000000002a  0000000000000000           0     0     1
  [19] .zdebug_info      PROGBITS         0000000007c5e609  07829609
       00000000008e69e7  0000000000000000           0     0     8
  [20] .zdebug_loc       PROGBITS         0000000008544ff0  0810fff0
       00000000004aa163  0000000000000000           0     0     8
  [21] .zdebug_ranges    PROGBITS         00000000089ef153  085ba153
       000000000017b277  0000000000000000           0     0     8
  [22] .note.go.buildid  NOTE             0000000000400f9c  00000f9c
       0000000000000064  0000000000000000   A       0     0     4
  [23] .symtab           SYMTAB           0000000000000000  08736000
       000000000042a1a0  0000000000000018          24   600     8
  [24] .strtab           STRTAB           0000000000000000  08b601a0
       0000000000f767eb  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  l (large), p (processor specific)
```