在使用 kubernetes 过程中由于某些需求往往要修改一下 k8s 官方的源码,然后重新编译才行。本文就以修改 kubeadm 生成证书为默认 100 年为例,来讲解如何使用 GitHub Actions 来编译和发布生成的二进制文件。

构建

clone repo

将 kubernetes 官方源码 fork 到自己的 repo 中

workflow

  • .github/workflows/kubeadm.yaml

  • build/run.sh

: Run a command in a build docker container. Common invocations:

  • build/run.sh make: Build just linux binaries in the container. Pass options and packages as necessary.
  • build/run.sh make cross: Build all binaries for all platforms. To build only a specific platform, add KUBE_BUILD_PLATFORMS=<os>/<arch>
  • build/run.sh make kubectl KUBE_BUILD_PLATFORMS=darwin/amd64: Build the specific binary for the specific platform (kubectl and darwin/amd64 respectively in this example)
  • build/run.sh make test: Run all unit tests
  • build/run.sh make test-integration: Run integration test
  • build/run.sh make test-cmd: Run CLI tests

修改源码

  • cmd/kubeadm/app/constants/constants.go

找到 CertificateValidity 变量将它在 365 天后面加两个 0,就将证书续命为 100 年了。

  • staging/src/k8s.io/client-go/util/cert

找到NotAfter,默认生成ca证书是10年,改为100年

cherry-pick

在分支上完成修改之后,我们将这个修改 cherry-pick 到其他的 tag 上面去,下面以 v1.21.4 为例子:在 v1.21.4 tag 的基础之上将上述的修改 cherry-pick 过来,重新打上新的 tag。

  • 获取上述修改的 commit id

  • checkout 到 v1.21.4 这个 tag 上

  • 将修改 cherry-pick 到当前 tag 上

  • 重新打上新的 tag,如 v1.21.4-patch-1.0

  • 将 tag push 到 repo 中触发 workflow

总结

上面只展示了以一个 tag 为单位进行构建的流程,想要构建其他版本的 kubeadm ,可以按照同样的流程和方法来完成。其实写一个 shell 脚本来处理也是十分简单,如下:

使用 GitHub Actions 的好处就是能够为我们解决代码管理和产物管理,构建好的二进制文件存放在 GitHub release 当中,下载和使用起来十分方便,不用在自己搞一台单独的机器或者存储服务器,节省很多人力维护成本。

Categories: HANDBOOK

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *