【注】源码分析均以 k8s 的第一个 commit 代码分析;
cloudcfg 可以看成 kubectl 的早期版本;
入口文件位置:
代码语言:go复制cmd/cloudcfg/cloudcfg.go
首先从命令行中,获取 method 的参数,拼接成发送给 api-server 的 url 信息;
代码语言:go复制method := flag.Arg(0)
url := *httpServer "/api/v1beta1" flag.Arg(1)
获取鉴权的参数:
代码语言:go复制auth, err := cloudcfg.LoadAuthInfo(*authConfig)
这里直接将参数拼接后发送, 获取对应的资源信息;
- 查询
method == "get"
method == "list" {
url = url "?labels=" *labelQuery
request, err = http.NewRequest("GET", url, nil)
- 删除
method == "delete" {
request, err = http.NewRequest("DELETE", url, nil)
- 创建
method == "create" {
request, err = cloudcfg.RequestWithBody(*config, url, "POST")
- 更新
method == "update" {
request, err = cloudcfg.RequestWithBody(*config, url, "PUT")
- 回滚
method == "rollingupdate" {
client := &kube_client.Client{
Host: *httpServer,
Auth: &auth,
}
cloudcfg.Update(flag.Arg(1), client, *updatePeriod)
最重要的部分是对控制器的管理:
代码语言:go复制method == "run" {
err = cloudcfg.RunController(image, name, replicas, kube_client.Client{Host: *httpServer, Auth: &auth}, *portSpec, *servicePort)
method == "stop" {
err = cloudcfg.StopController(flag.Arg(1), kube_client.Client{Host: *httpServer, Auth: &auth})
method == "rm" {
err = cloudcfg.DeleteController(flag.Arg(1), kube_client.Client{Host: *httpServer, Auth: &auth})