为什么kubeadm可以用于生产?
第一是kubeadm已经被官方收编,可以在官网文档中找到它的身影。
另外一个是kubeadm是用golang调用k8s的命令去执行创建k8s集群的。查看一下kubeadm的代码即可知道。
本文来源于csdn作者 sunican
挨个打开目录查看文件可知,源代码基本就在kinder目录。为什么这个目录名叫kinder呢?熟悉k8s项目的同学应该知道,有个项目叫kind。
查看roadmap.md可知,原来kinder(或者说kubeadm)作者从kind项目得到了灵感。
查看kinder/main.go可知,主体代码在kinder/cmd/kinder目录下
import (
"k8s.io/kubeadm/kinder/cmd/kinder"
)
func main() {
kinder.Main()
}
根据这个去看一下kinder.go文件,可以看到是Main函数调用了Run函数,而Run函数就一行
return NewCommand().Execute()
再找到NewCommand函数的定义就会发现,这个就是我们今天要看的主角。它借用了cobra来对构建k8s用到的命令进行统一的封装和调用。
func NewCommand() *cobra.Command {
flags := &Flags{}
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "kinder",
Short: "kinder is an example of kind(https://github.com/kubernetes-sigs/kind) used as a library",
Long: " _ ___ _\n" +
" | |/ (_)_ _ __| |___ _ _\n" +
" | ' <| | ' \\/ _` / -_) '_|\n" +
" |_|\\_\\_|_||_\\__,_\\___|_|\n\n" +
"kinder is an example of kind(https://github.com/kubernetes-sigs/kind) used as a library.\n\n" +
"All the kind commands will be available in kinder, side by side with additional commands \n" +
"designed for helping kubeadm contributors.\n\n" +
"kinder is still a work in progress. Test It, Break It, Send feedback!",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return runE(flags, cmd, args)
},
SilenceUsage: true,
Version: constants.KinderVersion,
}
cmd.PersistentFlags().StringVar(
&flags.LogLevel,
"loglevel",
defaultLevel.String(),
"logrus log level [panic, fatal, error, warning, info, debug, trace]",
)
logger := kindcmd.NewLogger()
ioStreams := kindcmd.StandardIOStreams()
// add kind top level subcommands re-used without changes
cmd.AddCommand(kinddelete.NewCommand(logger, ioStreams))
cmd.AddCommand(kindexport.NewCommand(logger, ioStreams))
// add kind commands customized in kind
cmd.AddCommand(build.NewCommand())
cmd.AddCommand(create.NewCommand())
cmd.AddCommand(version.NewCommand())
cmd.AddCommand(get.NewCommand())
// add kinder only commands
cmd.AddCommand(cp.NewCommand())
cmd.AddCommand(do.NewCommand())
cmd.AddCommand(exec.NewCommand())
cmd.AddCommand(test.NewCommand())
return cmd
}
cmd对应的每个函数调用,大家可以自行下载代码之后查看。今天分享就到这里。