本课程将详细介绍k8s所有命令,以及命令的go源码分析,学习知其然,知其所以然
————————————————
type ServiceAccountOpts struct {//选项结构体 CreateSubcommandOptions *CreateSubcommandOptions }
//创建serviceaccount cobra命令 func NewCmdCreateServiceAccount(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { options := &ServiceAccountOpts{//初始化选项 CreateSubcommandOptions: NewCreateSubcommandOptions(ioStreams), } cmd := &cobra.Command{//创建命令 Use: "serviceaccount NAME [--dry-run]", DisableFlagsInUseLine: true, Aliases: []string{"sa"},//别名 Short: i18n.T("Create a service account with the specified name"), Long: serviceAccountLong, Example: serviceAccountExample, Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(options.Complete(f, cmd, args))//准备工作 cmdutil.CheckErr(options.Run())//运行 }, } options.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)//创建print flag 选项 cmdutil.AddApplyAnnotationFlags(cmd)//创建save-config选项 cmdutil.AddValidateFlags(cmd)//创建validate选项 cmdutil.AddGeneratorFlags(cmd, generateversioned.ServiceAccountV1GeneratorName)//创建generator选项 return cmd }
//准备工作 func (o *ServiceAccountOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error { name, err := NameFromCommandArgs(cmd, args)//从参数获取资源名称 if err != nil { return err } var generator generate.StructuredGenerator switch generatorName := cmdutil.GetFlagString(cmd, "generator"); generatorName { case generateversioned.ServiceAccountV1GeneratorName://获取generator选项并判断 generator = &generateversioned.ServiceAccountGeneratorV1{Name: name}//构造generator对象 default: return errUnsupportedGenerator(cmd, generatorName) } return o.CreateSubcommandOptions.Complete(f, cmd, args, generator)//运行CreateSubCommandOption的complete }
其他和上一个命令一样,不做介绍