K8s-解析云上初始化(keadm init)

什么是keadm init

keadm init通过keadm init可以实现初始化cloudcore。

解析keadm init过程

  1. ExecuteInit就是真正执行init的方法,实际上是通过实现了InstallTools接口函数的方法实现的

  2. 所以InstallTools方法实现了init的进入函数

  3. 真正实现下载安装的函数是runHelmInstall

// IsKubeEdgeProcessRunning checks if the given process is running or not
func IsKubeEdgeProcessRunning(proc string) (bool, error) {
//pidof是Linux系统中用来查找正在运行进程的进程号(pid)的工具
// proc=cloudcore
procRunning := fmt.Sprintf("pidof %s 2>&1", proc)
cmd := NewCommand(procRunning)

err := cmd.Exec()
// 通过退出码检查是否安装
if cmd.ExitCode == 0 {
return true, nil
} else if cmd.ExitCode == 1 {
return false, nil
}

return false, err
}
// 真正的拉起Kubeedge的Cloudcore
func (cu *KubeCloudHelmInstTool) runHelmInstall(r *Renderer) (*release.Release, error) {
cf := genericclioptions.NewConfigFlags(true)
cf.KubeConfig = &cu.Common.KubeConfig
cf.Namespace = &cu.Namespace

cfg := &action.Configuration{}
// let the os.Stdout not print the details
logFunc := func(format string, v ...interface{}) {}
if err := cfg.Init(cf, cu.Namespace, "", logFunc); err != nil {
return nil, err
}

// a flag to confirm the install/upgrade action
var performInstall bool
_, err := cfg.Releases.Last(r.componentName)
if err != nil && errors.Is(err, driver.ErrReleaseNotFound) {
performInstall = true
}

if performInstall {
helmInstall := action.NewInstall(cfg)
helmInstall.DryRun = cu.DryRun
helmInstall.Namespace = cu.Namespace
// --force would not wait.
if !cu.Force {
helmInstall.Wait = DefaultHelmWait
helmInstall.Timeout = DefaultHelmTimeout
}
helmInstall.CreateNamespace = DefaultHelmCreateNs
helmInstall.ReleaseName = r.componentName

rel, err := helmInstall.Run(r.chart, r.profileValsMap)
if err != nil {
return nil, err
}
return rel, nil
}

// try to update a version
helmUpgrade := action.NewUpgrade(cfg)
helmUpgrade.DryRun = cu.DryRun
helmUpgrade.Namespace = cu.Namespace
// --force would not wait.
if !cu.Force {
helmUpgrade.Wait = DefaultHelmWait
helmUpgrade.Timeout = DefaultHelmTimeout
}

rel, err := helmUpgrade.Run(r.componentName, r.chart, r.profileValsMap)
if err != nil {
return nil, err
}
return rel, nil
}
  1. 其实本质就是通过调用了helm的接口安装的kubeedge-cloudocorehelmInstall