本课程将详细介绍k8s所有命令,以及命令的go源码分析,学习知其然,知其所以然
————————————————----------------------------------------------------------------------
type ClusterRoleBindingOpts struct {//clusterrolebinding结构体 CreateSubcommandOptions *CreateSubcommandOptions }
//创建create clusterrolebinding命令 func NewCmdCreateClusterRoleBinding(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { o := &ClusterRoleBindingOpts{//初始化结构体 CreateSubcommandOptions: NewCreateSubcommandOptions(ioStreams), } cmd := &cobra.Command{//创建cobra命令 Use: "clusterrolebinding NAME --clusterrole=NAME [--user=username] [--group=groupname] [--serviceaccount=namespace:serviceaccountname] [--dry-run]", DisableFlagsInUseLine: true, Short: i18n.T("Create a ClusterRoleBinding for a particular ClusterRole"), Long: clusterRoleBindingLong, Example: clusterRoleBindingExample, Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(o.Complete(f, cmd, args))//准备运行 cmdutil.CheckErr(o.Run())//运行 }, } o.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)//设置print 选项 cmdutil.AddApplyAnnotationFlags(cmd)//设置save-config选项 cmdutil.AddValidateFlags(cmd)//设置validate选项 cmdutil.AddGeneratorFlags(cmd, generateversioned.ClusterRoleBindingV1GeneratorName)//设置generator选项 cmd.Flags().String("clusterrole", "", i18n.T("ClusterRole this ClusterRoleBinding should reference"))//设置clusterrole选项 cmd.MarkFlagCustom("clusterrole", "__kubectl_get_resource_clusterrole") cmd.Flags().StringArray("user", []string{}, "Usernames to bind to the clusterrole")//设置user选项 cmd.Flags().StringArray("group", []string{}, "Groups to bind to the clusterrole")//设置group选项 cmd.Flags().StringArray("serviceaccount", []string{}, "Service accounts to bind to the clusterrole, in the format <namespace>:<name>")//设置serviceaccount选项 return cmd }
//准备运行 func (o *ClusterRoleBindingOpts) 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.ClusterRoleBindingV1GeneratorName://获取generator选项并判断 generator = &generateversioned.ClusterRoleBindingGeneratorV1{//构造generator对象 Name: name, ClusterRole: cmdutil.GetFlagString(cmd, "clusterrole"), Users: cmdutil.GetFlagStringArray(cmd, "user"), Groups: cmdutil.GetFlagStringArray(cmd, "group"), ServiceAccounts: cmdutil.GetFlagStringArray(cmd, "serviceaccount"), } default: return errUnsupportedGenerator(cmd, generatorName) } return o.CreateSubcommandOptions.Complete(f, cmd, args, generator)//运行createSubCommandOption complete方法 }