type DeploymentOpts struct {//option结构体
CreateSubcommandOptions *CreateSubcommandOptions
}
//创建create deployment命令
func NewCmdCreateDeployment(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
options := &DeploymentOpts{//初始化结构体
CreateSubcommandOptions: NewCreateSubcommandOptions(ioStreams),
}
cmd := &cobra.Command{//创建cobra命令
Use: "deployment NAME --image=image [--dry-run]",
DisableFlagsInUseLine: true,
Aliases: []string{"deploy"},
Short: i18n.T("Create a deployment with the specified name."),
Long: deploymentLong,
Example: deploymentExample,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(options.Complete(f, cmd, args))//准备运行
cmdutil.CheckErr(options.Run())//运行命令
},
}
options.CreateSubcommandOptions.PrintFlags.AddFlags(cmd)//设置print 选项
cmdutil.AddApplyAnnotationFlags(cmd)//设置save-config选项
cmdutil.AddValidateFlags(cmd)//设置validate选项
cmdutil.AddGeneratorFlags(cmd, "")//设置generator选项
cmd.Flags().StringSlice("image", []string{}, "Image name to run.")//设置image选项
cmd.MarkFlagRequired("image")//必须
return cmd
}
//准备运行
func (o *DeploymentOpts) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
name, err := NameFromCommandArgs(cmd, args)//获取名称
if err != nil {
return err
}
clientset, err := f.KubernetesClientSet()//获取clientset
if err != nil {
return err
}
generatorName := cmdutil.GetFlagString(cmd, "generator")//后去generator 名称
if len(generatorName) == 0 {//如果generator名称为空
generatorName = generateversioned.DeploymentBasicAppsV1GeneratorName//设置默认的generator名称
generatorNameTemp, err := generateversioned.FallbackGeneratorNameIfNecessary(generatorName, clientset.Discovery(), o.CreateSubcommandOptions.ErrOut)//回退generator,如果有必要
if err != nil {
return err
}
if generatorNameTemp != generatorName {如果generator不相同则发警告
cmdutil.Warning(o.CreateSubcommandOptions.ErrOut, generatorName, generatorNameTemp)
} else {
generatorName = generatorNameTemp
}
}
imageNames := cmdutil.GetFlagStringSlice(cmd, "image")//获取image选项
generator, ok := generatorFromName(generatorName, imageNames, name)//根据generator名称生成generator
if !ok {
return errUnsupportedGenerator(cmd, generatorName)
}
return o.CreateSubcommandOptions.Complete(f, cmd, args, generator)//运行createsubcommandoption complete方法
}
func generatorFromName(
generatorName string,
imageNames []string,
deploymentName string,
) (generate.StructuredGenerator, bool) {
switch generatorName {//判断generator名称生成相应generator
case generateversioned.DeploymentBasicAppsV1GeneratorName:
generator := &generateversioned.DeploymentBasicAppsGeneratorV1{
BaseDeploymentGenerator: generateversioned.BaseDeploymentGenerator{
Name: deploymentName,
Images: imageNames,
},
}
return generator, true
case generateversioned.DeploymentBasicAppsV1Beta1GeneratorName:
generator := &generateversioned.DeploymentBasicAppsGeneratorV1Beta1{
BaseDeploymentGenerator: generateversioned.BaseDeploymentGenerator{
Name: deploymentName,
Images: imageNames,
},
}
return generator, true
case generateversioned.DeploymentBasicV1Beta1GeneratorName:
generator := &generateversioned.DeploymentBasicGeneratorV1{
BaseDeploymentGenerator: generateversioned.BaseDeploymentGenerator{
Name: deploymentName,
Images: imageNames,
},
}
return generator, true
}
return nil, false
}