Merge pull request #114937 from seans3/export-delete-option

Exports WarningPrinter field in DeleteOptions
This commit is contained in:
Kubernetes Prow Robot 2023-01-10 06:59:28 -08:00 committed by GitHub
commit 5cbd6960c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 35 deletions

View File

@ -63,7 +63,7 @@ type CanIOptions struct {
List bool List bool
genericclioptions.IOStreams genericclioptions.IOStreams
warningPrinter *printers.WarningPrinter WarningPrinter *printers.WarningPrinter
} }
var ( var (
@ -145,7 +145,10 @@ func NewCmdCanI(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.C
// Complete completes all the required options // Complete completes all the required options
func (o *CanIOptions) Complete(f cmdutil.Factory, args []string) error { func (o *CanIOptions) Complete(f cmdutil.Factory, args []string) error {
o.warningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)}) // Set default WarningPrinter if not already set.
if o.WarningPrinter == nil {
o.WarningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
}
if o.List { if o.List {
if len(args) != 0 { if len(args) != 0 {
@ -206,8 +209,8 @@ func (o *CanIOptions) Validate() error {
return nil return nil
} }
if o.warningPrinter == nil { if o.WarningPrinter == nil {
return fmt.Errorf("warningPrinter can not be used without initialization") return fmt.Errorf("WarningPrinter can not be used without initialization")
} }
if o.NonResourceURL != "" { if o.NonResourceURL != "" {
@ -218,18 +221,18 @@ func (o *CanIOptions) Validate() error {
return fmt.Errorf("NonResourceURL and ResourceName can not specified together") return fmt.Errorf("NonResourceURL and ResourceName can not specified together")
} }
if !isKnownNonResourceVerb(o.Verb) { if !isKnownNonResourceVerb(o.Verb) {
o.warningPrinter.Print(fmt.Sprintf("verb '%s' is not a known verb\n", o.Verb)) o.WarningPrinter.Print(fmt.Sprintf("verb '%s' is not a known verb\n", o.Verb))
} }
} else if !o.Resource.Empty() && !o.AllNamespaces && o.DiscoveryClient != nil { } else if !o.Resource.Empty() && !o.AllNamespaces && o.DiscoveryClient != nil {
if namespaced, err := isNamespaced(o.Resource, o.DiscoveryClient); err == nil && !namespaced { if namespaced, err := isNamespaced(o.Resource, o.DiscoveryClient); err == nil && !namespaced {
if len(o.Resource.Group) == 0 { if len(o.Resource.Group) == 0 {
o.warningPrinter.Print(fmt.Sprintf("resource '%s' is not namespace scoped\n", o.Resource.Resource)) o.WarningPrinter.Print(fmt.Sprintf("resource '%s' is not namespace scoped\n", o.Resource.Resource))
} else { } else {
o.warningPrinter.Print(fmt.Sprintf("resource '%s' is not namespace scoped in group '%s'\n", o.Resource.Resource, o.Resource.Group)) o.WarningPrinter.Print(fmt.Sprintf("resource '%s' is not namespace scoped in group '%s'\n", o.Resource.Resource, o.Resource.Group))
} }
} }
if !isKnownResourceVerb(o.Verb) { if !isKnownResourceVerb(o.Verb) {
o.warningPrinter.Print(fmt.Sprintf("verb '%s' is not a known verb\n", o.Verb)) o.WarningPrinter.Print(fmt.Sprintf("verb '%s' is not a known verb\n", o.Verb))
} }
} }
@ -317,9 +320,9 @@ func (o *CanIOptions) resourceFor(mapper meta.RESTMapper, resourceArg string) sc
if err != nil { if err != nil {
if !nonStandardResourceNames.Has(groupResource.String()) { if !nonStandardResourceNames.Has(groupResource.String()) {
if len(groupResource.Group) == 0 { if len(groupResource.Group) == 0 {
o.warningPrinter.Print(fmt.Sprintf("the server doesn't have a resource type '%s'\n", groupResource.Resource)) o.WarningPrinter.Print(fmt.Sprintf("the server doesn't have a resource type '%s'\n", groupResource.Resource))
} else { } else {
o.warningPrinter.Print(fmt.Sprintf("the server doesn't have a resource type '%s' in group '%s'\n", groupResource.Resource, groupResource.Group)) o.WarningPrinter.Print(fmt.Sprintf("the server doesn't have a resource type '%s' in group '%s'\n", groupResource.Resource, groupResource.Group))
} }
} }
return schema.GroupVersionResource{Resource: resourceArg} return schema.GroupVersionResource{Resource: resourceArg}
@ -331,7 +334,7 @@ func (o *CanIOptions) resourceFor(mapper meta.RESTMapper, resourceArg string) sc
func (o *CanIOptions) printStatus(status authorizationv1.SubjectRulesReviewStatus) error { func (o *CanIOptions) printStatus(status authorizationv1.SubjectRulesReviewStatus) error {
if status.Incomplete { if status.Incomplete {
o.warningPrinter.Print(fmt.Sprintf("the list may be incomplete: %v", status.EvaluationError)) o.WarningPrinter.Print(fmt.Sprintf("the list may be incomplete: %v", status.EvaluationError))
} }
breakdownRules := []rbacv1.PolicyRule{} breakdownRules := []rbacv1.PolicyRule{}

View File

@ -293,7 +293,7 @@ func TestRunResourceFor(t *testing.T) {
ioStreams, _, _, buf := genericclioptions.NewTestIOStreams() ioStreams, _, _, buf := genericclioptions.NewTestIOStreams()
test.o.IOStreams = ioStreams test.o.IOStreams = ioStreams
test.o.warningPrinter = printers.NewWarningPrinter(test.o.IOStreams.ErrOut, printers.WarningPrinterOptions{Color: false}) test.o.WarningPrinter = printers.NewWarningPrinter(test.o.IOStreams.ErrOut, printers.WarningPrinterOptions{Color: false})
restMapper, err := tf.ToRESTMapper() restMapper, err := tf.ToRESTMapper()
if err != nil { if err != nil {

View File

@ -130,7 +130,7 @@ type DebugOptions struct {
podClient corev1client.CoreV1Interface podClient corev1client.CoreV1Interface
genericclioptions.IOStreams genericclioptions.IOStreams
warningPrinter *printers.WarningPrinter WarningPrinter *printers.WarningPrinter
applier ProfileApplier applier ProfileApplier
} }
@ -224,8 +224,10 @@ func (o *DebugOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
o.attachChanged = cmd.Flags().Changed("attach") o.attachChanged = cmd.Flags().Changed("attach")
o.shareProcessedChanged = cmd.Flags().Changed("share-processes") o.shareProcessedChanged = cmd.Flags().Changed("share-processes")
// Warning printer // Set default WarningPrinter
o.warningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)}) if o.WarningPrinter == nil {
o.WarningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
}
o.applier, err = NewProfileApplier(o.Profile) o.applier, err = NewProfileApplier(o.Profile)
if err != nil { if err != nil {
return err return err
@ -307,9 +309,9 @@ func (o *DebugOptions) Validate() error {
return fmt.Errorf("-i/--stdin is required for containers with -t/--tty=true") return fmt.Errorf("-i/--stdin is required for containers with -t/--tty=true")
} }
// warningPrinter // WarningPrinter
if o.warningPrinter == nil { if o.WarningPrinter == nil {
return fmt.Errorf("warningPrinter can not be used without initialization") return fmt.Errorf("WarningPrinter can not be used without initialization")
} }
return nil return nil
@ -764,7 +766,7 @@ func (o *DebugOptions) waitForContainer(ctx context.Context, ns, podName, contai
return true, nil return true, nil
} }
if !o.Quiet && s.State.Waiting != nil && s.State.Waiting.Message != "" { if !o.Quiet && s.State.Waiting != nil && s.State.Waiting.Message != "" {
o.warningPrinter.Print(fmt.Sprintf("container %s: %s", containerName, s.State.Waiting.Message)) o.WarningPrinter.Print(fmt.Sprintf("container %s: %s", containerName, s.State.Waiting.Message))
} }
return false, nil return false, nil
}) })

View File

@ -1598,7 +1598,8 @@ func TestCompleteAndValidate(t *testing.T) {
t.Fatalf("CompleteAndValidate got error: '%v', wantError: %v", gotError, tc.wantError) t.Fatalf("CompleteAndValidate got error: '%v', wantError: %v", gotError, tc.wantError)
} }
if diff := cmp.Diff(tc.wantOpts, opts, cmpFilter, cmpopts.IgnoreUnexported(DebugOptions{})); diff != "" { if diff := cmp.Diff(tc.wantOpts, opts, cmpFilter, cmpopts.IgnoreFields(DebugOptions{},
"attachChanged", "shareProcessedChanged", "podClient", "WarningPrinter", "applier")); diff != "" {
t.Error("CompleteAndValidate unexpected diff in generated object: (-want +got):\n", diff) t.Error("CompleteAndValidate unexpected diff in generated object: (-want +got):\n", diff)
} }
}) })

View File

@ -133,7 +133,7 @@ type DeleteOptions struct {
Result *resource.Result Result *resource.Result
genericclioptions.IOStreams genericclioptions.IOStreams
warningPrinter *printers.WarningPrinter WarningPrinter *printers.WarningPrinter
} }
func NewCmdDelete(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { func NewCmdDelete(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
@ -226,7 +226,10 @@ func (o *DeleteOptions) Complete(f cmdutil.Factory, args []string, cmd *cobra.Co
} }
} }
o.warningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)}) // Set default WarningPrinter if not already set.
if o.WarningPrinter == nil {
o.WarningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
}
return nil return nil
} }
@ -242,13 +245,13 @@ func (o *DeleteOptions) Validate() error {
if o.DeleteAll && len(o.FieldSelector) > 0 { if o.DeleteAll && len(o.FieldSelector) > 0 {
return fmt.Errorf("cannot set --all and --field-selector at the same time") return fmt.Errorf("cannot set --all and --field-selector at the same time")
} }
if o.warningPrinter == nil { if o.WarningPrinter == nil {
return fmt.Errorf("warningPrinter can not be used without initialization") return fmt.Errorf("WarningPrinter can not be used without initialization")
} }
switch { switch {
case o.GracePeriod == 0 && o.ForceDeletion: case o.GracePeriod == 0 && o.ForceDeletion:
o.warningPrinter.Print("Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.") o.WarningPrinter.Print("Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.")
case o.GracePeriod > 0 && o.ForceDeletion: case o.GracePeriod > 0 && o.ForceDeletion:
return fmt.Errorf("--force and --grace-period greater than 0 cannot be specified together") return fmt.Errorf("--force and --grace-period greater than 0 cannot be specified together")
} }
@ -312,7 +315,7 @@ func (o *DeleteOptions) DeleteResult(r *resource.Result) error {
options.PropagationPolicy = &o.CascadingStrategy options.PropagationPolicy = &o.CascadingStrategy
if warnClusterScope && info.Mapping.Scope.Name() == meta.RESTScopeNameRoot { if warnClusterScope && info.Mapping.Scope.Name() == meta.RESTScopeNameRoot {
o.warningPrinter.Print("deleting cluster-scoped resources, not scoped to the provided namespace") o.WarningPrinter.Print("deleting cluster-scoped resources, not scoped to the provided namespace")
warnClusterScope = false warnClusterScope = false
} }

View File

@ -49,7 +49,7 @@ type DrainCmdOptions struct {
nodeInfos []*resource.Info nodeInfos []*resource.Info
genericclioptions.IOStreams genericclioptions.IOStreams
warningPrinter *printers.WarningPrinter WarningPrinter *printers.WarningPrinter
} }
var ( var (
@ -254,7 +254,10 @@ func (o *DrainCmdOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
return printer.PrintObj, nil return printer.PrintObj, nil
} }
o.warningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)}) // Set default WarningPrinter if not already set.
if o.WarningPrinter == nil {
o.WarningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
}
builder := f.NewBuilder(). builder := f.NewBuilder().
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...). WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
@ -336,7 +339,7 @@ func (o *DrainCmdOptions) deleteOrEvictPodsSimple(nodeInfo *resource.Info) error
return utilerrors.NewAggregate(errs) return utilerrors.NewAggregate(errs)
} }
if warnings := list.Warnings(); warnings != "" { if warnings := list.Warnings(); warnings != "" {
o.warningPrinter.Print(warnings) o.WarningPrinter.Print(warnings)
} }
if o.drainer.DryRunStrategy == cmdutil.DryRunClient { if o.drainer.DryRunStrategy == cmdutil.DryRunClient {
for _, pod := range list.Pods() { for _, pod := range list.Pods() {

View File

@ -130,7 +130,7 @@ type EnvOptions struct {
clientset *kubernetes.Clientset clientset *kubernetes.Clientset
genericclioptions.IOStreams genericclioptions.IOStreams
warningPrinter *printers.WarningPrinter WarningPrinter *printers.WarningPrinter
} }
// NewEnvOptions returns an EnvOptions indicating all containers in the selected // NewEnvOptions returns an EnvOptions indicating all containers in the selected
@ -207,7 +207,7 @@ func contains(key string, keyList []string) bool {
func (o *EnvOptions) keyToEnvName(key string) string { func (o *EnvOptions) keyToEnvName(key string) string {
envName := strings.ToUpper(validEnvNameRegexp.ReplaceAllString(key, "_")) envName := strings.ToUpper(validEnvNameRegexp.ReplaceAllString(key, "_"))
if envName != key { if envName != key {
o.warningPrinter.Print(fmt.Sprintf("key %s transferred to %s", key, envName)) o.WarningPrinter.Print(fmt.Sprintf("key %s transferred to %s", key, envName))
} }
return envName return envName
} }
@ -247,7 +247,10 @@ func (o *EnvOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
return err return err
} }
o.builder = f.NewBuilder o.builder = f.NewBuilder
o.warningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)}) // Set default WarningPrinter if not already set.
if o.WarningPrinter == nil {
o.WarningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
}
return nil return nil
} }
@ -266,8 +269,8 @@ func (o *EnvOptions) Validate() error {
if len(o.Keys) > 0 && len(o.From) == 0 { if len(o.Keys) > 0 && len(o.From) == 0 {
return fmt.Errorf("when specifying --keys, a configmap or secret must be provided with --from") return fmt.Errorf("when specifying --keys, a configmap or secret must be provided with --from")
} }
if o.warningPrinter == nil { if o.WarningPrinter == nil {
return fmt.Errorf("warningPrinter can not be used without initialization") return fmt.Errorf("WarningPrinter can not be used without initialization")
} }
return nil return nil
} }
@ -421,7 +424,7 @@ func (o *EnvOptions) RunEnv() error {
} }
} }
o.warningPrinter.Print(fmt.Sprintf("%s/%s does not have any containers matching %q", objKind, objName, o.ContainerSelector)) o.WarningPrinter.Print(fmt.Sprintf("%s/%s does not have any containers matching %q", objKind, objName, o.ContainerSelector))
} }
return nil return nil
} }