func (o *DrainCmdOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
	var err error

	if len(args) == 0 && !cmd.Flags().Changed("selector") {
		return cmdutil.UsageErrorf(cmd, fmt.Sprintf("USAGE: %s [flags]", cmd.Use))
	}
	if len(args) > 0 && len(o.drainer.Selector) > 0 {
		return cmdutil.UsageErrorf(cmd, "error: cannot specify both a node name and a --selector option")
	}

	o.drainer.DryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd)
	if err != nil {
		return err
	}
	dynamicClient, err := f.DynamicClient()
	if err != nil {
		return err
	}
	discoveryClient, err := f.ToDiscoveryClient()
	if err != nil {
		return err
	}
	o.drainer.DryRunVerifier = resource.NewDryRunVerifier(dynamicClient, discoveryClient)

	if o.drainer.Client, err = f.KubernetesClientSet(); err != nil {
		return err
	}

	if len(o.drainer.PodSelector) > 0 {
		if _, err := labels.Parse(o.drainer.PodSelector); err != nil {
			return errors.New("--pod-selector=<pod_selector> must be a valid label selector")
		}
	}

	o.nodeInfos = []*resource.Info{}

	o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
	if err != nil {
		return err
	}

	o.ToPrinter = func(operation string) (printers.ResourcePrinterFunc, error) {
		o.PrintFlags.NamePrintFlags.Operation = operation
		cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.drainer.DryRunStrategy)

		printer, err := o.PrintFlags.ToPrinter()
		if err != nil {
			return nil, err
		}

		return printer.PrintObj, nil
	}

	builder := f.NewBuilder().
		WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
		NamespaceParam(o.Namespace).DefaultNamespace().
		ResourceNames("nodes", args...).
		SingleResourceType().
		Flatten()

	if len(o.drainer.Selector) > 0 {
		builder = builder.LabelSelectorParam(o.drainer.Selector).
			ResourceTypes("nodes")
	}

	r := builder.Do()

	if err = r.Err(); err != nil {
		return err
	}

	return r.Visit(func(info *resource.Info, err error) error {
		if err != nil {
			return err
		}
		if info.Mapping.Resource.GroupResource() != (schema.GroupResource{Group: "", Resource: "nodes"}) {
			return fmt.Errorf("error: expected resource of type node, got %q", info.Mapping.Resource)
		}

		o.nodeInfos = append(o.nodeInfos, info)
		return nil
	})
}
// Complete populates some fields from the factory, grabs command line
// arguments and looks up the node using Builder
Complete populates some fields from the factory, grabs command line
arguments and looks up the node using Builder
// Complete populates some fields from the factory grabs command line // arguments and looks up the node using Builder
func (o *DrainCmdOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
	var err error
	if len(args) == 0 && !cmd.Flags().Changed("selector") {
		return cmdutil.UsageErrorf(cmd, fmt.Sprintf("USAGE: %s [flags]", cmd.Use))
	}
	if len(args) > 0 && len(o.drainer.Selector) > 0 {
		return cmdutil.UsageErrorf(cmd, "error: cannot specify both a node name and a --selector option")
	}
	o.drainer.DryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd)
	if err != nil {
		return err
	}
	dynamicClient, err := f.DynamicClient()
	if err != nil {
		return err
	}
	discoveryClient, err := f.ToDiscoveryClient()
	if err != nil {
		return err
	}
	o.drainer.DryRunVerifier = resource.NewDryRunVerifier(dynamicClient, discoveryClient)
	if o.drainer.Client, err = f.KubernetesClientSet(); err != nil {
		return err
	}
	if len(o.drainer.PodSelector) > 0 {
		if _, err := labels.Parse(o.drainer.PodSelector); err != nil {
			return errors.New("--pod-selector=<pod_selector> must be a valid label selector")
		}
	}
	o.nodeInfos = []*resource.Info{}
	o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
	if err != nil {
		return err
	}
	o.ToPrinter = func(operation string) (printers.ResourcePrinterFunc, error) {
		o.PrintFlags.NamePrintFlags.Operation = operation
		cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.drainer.DryRunStrategy)
		printer, err := o.PrintFlags.ToPrinter()
		if err != nil {
			return nil, err
		}
		return printer.PrintObj, nil
	}
	builder := f.NewBuilder().
		WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
		NamespaceParam(o.Namespace).DefaultNamespace().
		ResourceNames("nodes", args...).
		SingleResourceType().
		Flatten()
	if len(o.drainer.Selector) > 0 {
		builder = builder.LabelSelectorParam(o.drainer.Selector).
			ResourceTypes("nodes")
	}
	r := builder.Do()
	if err = r.Err(); err != nil {
		return err
	}
	return r.Visit(func(info *resource.Info, err error) error {
		if err != nil {
			return err
		}
		if info.Mapping.Resource.GroupResource() != (schema.GroupResource{Group: "", Resource: "nodes"}) {
			return fmt.Errorf("error: expected resource of type node, got %q", info.Mapping.Resource)
		}
		o.nodeInfos = append(o.nodeInfos, info)
		return nil
	})
}
func ( o * DrainCmdOptions ) Complete ( f cmdutil . Factory , cmd * cobra . Command , args [ ] string ) error { var err error if len ( args ) == 0 && ! cmd . Flags ( ) . Changed ( " " ) { return cmdutil . UsageErrorf ( cmd , fmt . Sprintf ( " " , cmd . Use ) ) } if len ( args ) > 0 && len ( o . drainer . Selector ) > 0 { return cmdutil . UsageErrorf ( cmd , " " ) } o . drainer . DryRunStrategy , err = cmdutil . GetDryRunStrategy ( cmd ) if err != nil { return err } dynamicClient , err := f . DynamicClient ( ) if err != nil { return err } discoveryClient , err := f . ToDiscoveryClient ( ) if err != nil { return err } o . drainer . DryRunVerifier = resource . NewDryRunVerifier ( dynamicClient , discoveryClient ) if o . drainer . Client , err = f . KubernetesClientSet ( ) ; err != nil { return err } if len ( o . drainer . PodSelector ) > 0 { if _ , err := labels . Parse ( o . drainer . PodSelector ) ; err != nil { return errors . New ( " " ) } } o . nodeInfos = [ ] * resource . Info { } o . Namespace , _ , err = f . ToRawKubeConfigLoader ( ) . Namespace ( ) if err != nil { return err } o . ToPrinter = func ( operation string ) ( printers . ResourcePrinterFunc , error ) { o . PrintFlags . NamePrintFlags . Operation = operation cmdutil . PrintFlagsWithDryRunStrategy ( o . PrintFlags , o . drainer . DryRunStrategy ) printer , err := o . PrintFlags . ToPrinter ( ) if err != nil { return nil , err } return printer . PrintObj , nil } builder := f . NewBuilder ( ) . WithScheme ( scheme . Scheme , scheme . Scheme . PrioritizedVersionsAllGroups ( ) ... ) . NamespaceParam ( o . Namespace ) . DefaultNamespace ( ) . ResourceNames ( " " , args ... ) . SingleResourceType ( ) . Flatten ( ) if len ( o . drainer . Selector ) > 0 { builder = builder . LabelSelectorParam ( o . drainer . Selector ) . ResourceTypes ( " " ) } r := builder . Do ( ) if err = r . Err ( ) ; err != nil { return err } return r . Visit ( func ( info * resource . Info , err error ) error { if err != nil { return err } if info . Mapping . Resource . GroupResource ( ) != ( schema . GroupResource { Group : " " , Resource : " " } ) { return fmt . Errorf ( " " , info . Mapping . Resource ) } o . nodeInfos = append ( o . nodeInfos , info ) return nil } ) }
Complete populates some fields from the factory, grabs command line
arguments and looks up the node using Builder
Complete populates some fields from the factory grabs command line arguments and looks up the node using Builder