————————————————
type PauseOptions struct {//pause结构体 PrintFlags *genericclioptions.PrintFlags ToPrinter func(string) (printers.ResourcePrinter, error) Pauser polymorphichelpers.ObjectPauserFunc Builder func() *resource.Builder Namespace string EnforceNamespace bool Resources []string resource.FilenameOptions genericclioptions.IOStreams }
//创建pause命令 func NewCmdRolloutPause(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { o := &PauseOptions{//初始化结构体 PrintFlags: genericclioptions.NewPrintFlags("paused").WithTypeSetter(scheme.Scheme), IOStreams: streams, } validArgs := []string{"deployment"} cmd := &cobra.Command{//创建cobra命令 Use: "pause RESOURCE", DisableFlagsInUseLine: true, Short: i18n.T("Mark the provided resource as paused"), Long: pauseLong, Example: pauseExample, Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(o.Complete(f, cmd, args))//准备 cmdutil.CheckErr(o.Validate())//校验 cmdutil.CheckErr(o.RunPause())//运行 }, ValidArgs: validArgs,//有效参数 } o.PrintFlags.AddFlags(cmd)//设置打印选项 usage := "identifying the resource to get from a server." cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)//设置文件选项 return cmd }
//准备方法 func (o *PauseOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error { o.Pauser = polymorphichelpers.ObjectPauserFn//设置Pauser函数 var err error o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()//获取namespace和enforcenameapace if err != nil { return err } o.Resources = args//设置resources o.Builder = f.NewBuilder//设置Builder o.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {//printflag转printer函数 o.PrintFlags.NamePrintFlags.Operation = operation return o.PrintFlags.ToPrinter() } return nil }
//校验函数 func (o *PauseOptions) Validate() error { if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {//资源和文件至少指定一个 return fmt.Errorf("required resource not specified") } return nil }
//运行 func (o *PauseOptions) RunPause() error { r := o.Builder(). WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...). NamespaceParam(o.Namespace).DefaultNamespace(). FilenameParam(o.EnforceNamespace, &o.FilenameOptions). ResourceTypeOrNameArgs(true, o.Resources...). ContinueOnError(). Latest(). Flatten(). Do()//构造info对象 if err := r.Err(); err != nil { return err } allErrs := []error{} infos, err := r.Infos()//获取info对象 if err != nil { // restore previous command behavior where // an error caused by retrieving infos due to // at least a single broken object did not result // in an immediate return, but rather an overall // aggregation of errors. allErrs = append(allErrs, err) } for _, patch := range set.CalculatePatches(infos, scheme.DefaultJSONEncoder(), set.PatchFn(o.Pauser)) {//计算patches,然后遍历 info := patch.Info if patch.Err != nil {//patch有error,处理error resourceString := info.Mapping.Resource.Resource if len(info.Mapping.Resource.Group) > 0 { resourceString = resourceString + "." + info.Mapping.Resource.Group } allErrs = append(allErrs, fmt.Errorf("error: %s %q %v", resourceString, info.Name, patch.Err)) continue } if string(patch.Patch) == "{}" || len(patch.Patch) == 0 {//patch为空 printer, err := o.ToPrinter("already paused")//printflag转printer if err != nil { allErrs = append(allErrs, err) continue } if err = printer.PrintObj(info.Object, o.Out); err != nil {//打印对象 allErrs = append(allErrs, err) } continue } obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch, nil)//应用patch到服务端 if err != nil { allErrs = append(allErrs, fmt.Errorf("failed to patch: %v", err)) continue } info.Refresh(obj, true)//刷新obj printer, err := o.ToPrinter("paused")// print flag转printer if err != nil { allErrs = append(allErrs, err) continue } if err = printer.PrintObj(info.Object, o.Out); err != nil {//打印结果 allErrs = append(allErrs, err) } } return utilerrors.NewAggregate(allErrs) }