【K8s】cloudcfg 源码分析 01-01

2022-07-17 15:15:04 浏览数 (2)

【注】源码分析均以 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)

这里直接将参数拼接后发送, 获取对应的资源信息;

  • 查询
代码语言:go复制
method == "get" 
method == "list" {
    url = url   "?labels="   *labelQuery	
    request, err = http.NewRequest("GET", url, nil)
  • 删除
代码语言:go复制
method == "delete" {
    request, err = http.NewRequest("DELETE", url, nil)
  • 创建
代码语言:go复制
method == "create" {
    request, err = cloudcfg.RequestWithBody(*config, url, "POST")
  • 更新
代码语言:go复制
method == "update" {
    request, err = cloudcfg.RequestWithBody(*config, url, "PUT")
  • 回滚
代码语言:go复制
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})

0 人点赞