————————————————
type UndoOptions struct {//undo结构体 PrintFlags *genericclioptions.PrintFlags ToPrinter func(string) (printers.ResourcePrinter, error) Builder func() *resource.Builder ToRevision int64 DryRun bool Resources []string Namespace string EnforceNamespace bool RESTClientGetter genericclioptions.RESTClientGetter resource.FilenameOptions genericclioptions.IOStreams }
func NewRolloutUndoOptions(streams genericclioptions.IOStreams) *UndoOptions { return &UndoOptions{//初始化结构体 PrintFlags: genericclioptions.NewPrintFlags("rolled back").WithTypeSetter(scheme.Scheme), IOStreams: streams, ToRevision: int64(0), } }
//创建undo命令 func NewCmdRolloutUndo(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { o := NewRolloutUndoOptions(streams)//初始化结构体 validArgs := []string{"deployment", "daemonset", "statefulset"} cmd := &cobra.Command{//创建cobra命令 Use: "undo (TYPE NAME | TYPE/NAME) [flags]", DisableFlagsInUseLine: true, Short: i18n.T("Undo a previous rollout"), Long: undoLong, Example: undoExample, Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(o.Complete(f, cmd, args))//准备 cmdutil.CheckErr(o.Validate())//校验 cmdutil.CheckErr(o.RunUndo())//运行 }, ValidArgs: validArgs,//有效参数 } cmd.Flags().Int64Var(&o.ToRevision, "to-revision", o.ToRevision, "The revision to rollback to. Default to 0 (last revision).")//to-revision选项 usage := "identifying the resource to get from a server." cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, usage)//文件选项 cmdutil.AddDryRunFlag(cmd)//干跑选项 o.PrintFlags.AddFlags(cmd)//打印选项 return cmd }
//准备函数 func (o *UndoOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error { o.Resources = args//设置资源 o.DryRun = cmdutil.GetDryRunFlag(cmd)//设置干跑 var err error if o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace(); err != nil {//设置Namespace和EnforceNamespace return err } o.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {//printflag转printer函数 o.PrintFlags.NamePrintFlags.Operation = operation if o.DryRun { o.PrintFlags.Complete("%s (dry run)") } return o.PrintFlags.ToPrinter() } o.RESTClientGetter = f//设置RestClientGetter o.Builder = f.NewBuilder//设置Builder return err } //校验函数 func (o *UndoOptions) Validate() error { if len(o.Resources) == 0 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {//资源和文件至少有一个 return fmt.Errorf("required resource not specified") } return nil }
//运行 func (o *UndoOptions) RunUndo() 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()//构造REsult对象 if err := r.Err(); err != nil { return err } err := r.Visit(func(info *resource.Info, err error) error {//visit Result if err != nil { return err } rollbacker, err := polymorphichelpers.RollbackerFn(o.RESTClientGetter, info.ResourceMapping())//获取回滚器 if err != nil { return err } result, err := rollbacker.Rollback(info.Object, nil, o.ToRevision, o.DryRun)//执行回滚 if err != nil { return err } printer, err := o.ToPrinter(result)//print flag转printer if err != nil { return err } return printer.PrintObj(info.Object, o.Out)//打印结果 }) return err }
//执行回滚 func (r *DeploymentRollbacker) Rollback(obj runtime.Object, updatedAnnotations map[string]string, toRevision int64, dryRun bool) (string, error) { if toRevision < 0 {//回滚范本不能小于0 return "", revisionNotFoundErr(toRevision) } accessor, err := meta.Accessor(obj)//访问对象 if err != nil { return "", fmt.Errorf("failed to create accessor for kind %v: %s", obj.GetObjectKind(), err.Error()) } name := accessor.GetName()//获取obj名称 namespace := accessor.GetNamespace()//获取obj namespace // TODO: Fix this after kubectl has been removed from core. It is not possible to convert the runtime.Object // to the external appsv1 Deployment without round-tripping through an internal version of Deployment. We're // currently getting rid of all internal versions of resources. So we specifically request the appsv1 version // here. This follows the same pattern as for DaemonSet and StatefulSet. deployment, err := r.c.AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{})//获取deployment if err != nil { return "", fmt.Errorf("failed to retrieve Deployment %s: %v", name, err) } rsForRevision, err := deploymentRevision(deployment, r.c, toRevision)//获取对应版本的rs if err != nil { return "", err } if dryRun {//如果是干盘返回 return printTemplate(&rsForRevision.Spec.Template) } if deployment.Spec.Paused {//如果deploy已经暂停,返回错误 return "", fmt.Errorf("you cannot rollback a paused deployment; resume it first with 'kubectl rollout resume deployment/%s' and try again", name) } // Skip if the revision already matches current Deployment if equalIgnoreHash(&rsForRevision.Spec.Template, &deployment.Spec.Template) {//如果要更新的rs的template和现在的deploy的template一样则,返回提示template一样 return fmt.Sprintf("%s (current template already matches revision %d)", rollbackSkipped, toRevision), nil } // remove hash label before patching back into the deployment delete(rsForRevision.Spec.Template.Labels, appsv1.DefaultDeploymentUniqueLabelKey)//删除rs的pod-template-hash标签 // compute deployment annotations annotations := map[string]string{}//设置annotation for k := range annotationsToSkip { if v, ok := deployment.Annotations[k]; ok { annotations[k] = v } } for k, v := range rsForRevision.Annotations { if !annotationsToSkip[k] { annotations[k] = v } } // make patch to restore patchType, patch, err := getDeploymentPatch(&rsForRevision.Spec.Template, annotations)//获取patch if err != nil { return "", fmt.Errorf("failed restoring revision %d: %v", toRevision, err) } // Restore revision if _, err = r.c.AppsV1().Deployments(namespace).Patch(name, patchType, patch); err != nil {//应用patch到服务端 return "", fmt.Errorf("failed restoring revision %d: %v", toRevision, err) } return rollbackSuccess, nil//返回成功 }