Skip to content

Commit 5cbd696

Browse files
authored
Merge pull request kubernetes#114937 from seans3/export-delete-option
Exports WarningPrinter field in DeleteOptions
2 parents 2ed47bd + 75ff830 commit 5cbd696

File tree

7 files changed

+50
-35
lines changed

7 files changed

+50
-35
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/auth/cani.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type CanIOptions struct {
6363
List bool
6464

6565
genericclioptions.IOStreams
66-
warningPrinter *printers.WarningPrinter
66+
WarningPrinter *printers.WarningPrinter
6767
}
6868

6969
var (
@@ -145,7 +145,10 @@ func NewCmdCanI(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.C
145145

146146
// Complete completes all the required options
147147
func (o *CanIOptions) Complete(f cmdutil.Factory, args []string) error {
148-
o.warningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
148+
// Set default WarningPrinter if not already set.
149+
if o.WarningPrinter == nil {
150+
o.WarningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
151+
}
149152

150153
if o.List {
151154
if len(args) != 0 {
@@ -206,8 +209,8 @@ func (o *CanIOptions) Validate() error {
206209
return nil
207210
}
208211

209-
if o.warningPrinter == nil {
210-
return fmt.Errorf("warningPrinter can not be used without initialization")
212+
if o.WarningPrinter == nil {
213+
return fmt.Errorf("WarningPrinter can not be used without initialization")
211214
}
212215

213216
if o.NonResourceURL != "" {
@@ -218,18 +221,18 @@ func (o *CanIOptions) Validate() error {
218221
return fmt.Errorf("NonResourceURL and ResourceName can not specified together")
219222
}
220223
if !isKnownNonResourceVerb(o.Verb) {
221-
o.warningPrinter.Print(fmt.Sprintf("verb '%s' is not a known verb\n", o.Verb))
224+
o.WarningPrinter.Print(fmt.Sprintf("verb '%s' is not a known verb\n", o.Verb))
222225
}
223226
} else if !o.Resource.Empty() && !o.AllNamespaces && o.DiscoveryClient != nil {
224227
if namespaced, err := isNamespaced(o.Resource, o.DiscoveryClient); err == nil && !namespaced {
225228
if len(o.Resource.Group) == 0 {
226-
o.warningPrinter.Print(fmt.Sprintf("resource '%s' is not namespace scoped\n", o.Resource.Resource))
229+
o.WarningPrinter.Print(fmt.Sprintf("resource '%s' is not namespace scoped\n", o.Resource.Resource))
227230
} else {
228-
o.warningPrinter.Print(fmt.Sprintf("resource '%s' is not namespace scoped in group '%s'\n", o.Resource.Resource, o.Resource.Group))
231+
o.WarningPrinter.Print(fmt.Sprintf("resource '%s' is not namespace scoped in group '%s'\n", o.Resource.Resource, o.Resource.Group))
229232
}
230233
}
231234
if !isKnownResourceVerb(o.Verb) {
232-
o.warningPrinter.Print(fmt.Sprintf("verb '%s' is not a known verb\n", o.Verb))
235+
o.WarningPrinter.Print(fmt.Sprintf("verb '%s' is not a known verb\n", o.Verb))
233236
}
234237
}
235238

@@ -317,9 +320,9 @@ func (o *CanIOptions) resourceFor(mapper meta.RESTMapper, resourceArg string) sc
317320
if err != nil {
318321
if !nonStandardResourceNames.Has(groupResource.String()) {
319322
if len(groupResource.Group) == 0 {
320-
o.warningPrinter.Print(fmt.Sprintf("the server doesn't have a resource type '%s'\n", groupResource.Resource))
323+
o.WarningPrinter.Print(fmt.Sprintf("the server doesn't have a resource type '%s'\n", groupResource.Resource))
321324
} else {
322-
o.warningPrinter.Print(fmt.Sprintf("the server doesn't have a resource type '%s' in group '%s'\n", groupResource.Resource, groupResource.Group))
325+
o.WarningPrinter.Print(fmt.Sprintf("the server doesn't have a resource type '%s' in group '%s'\n", groupResource.Resource, groupResource.Group))
323326
}
324327
}
325328
return schema.GroupVersionResource{Resource: resourceArg}
@@ -331,7 +334,7 @@ func (o *CanIOptions) resourceFor(mapper meta.RESTMapper, resourceArg string) sc
331334

332335
func (o *CanIOptions) printStatus(status authorizationv1.SubjectRulesReviewStatus) error {
333336
if status.Incomplete {
334-
o.warningPrinter.Print(fmt.Sprintf("the list may be incomplete: %v", status.EvaluationError))
337+
o.WarningPrinter.Print(fmt.Sprintf("the list may be incomplete: %v", status.EvaluationError))
335338
}
336339

337340
breakdownRules := []rbacv1.PolicyRule{}

staging/src/k8s.io/kubectl/pkg/cmd/auth/cani_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func TestRunResourceFor(t *testing.T) {
293293

294294
ioStreams, _, _, buf := genericclioptions.NewTestIOStreams()
295295
test.o.IOStreams = ioStreams
296-
test.o.warningPrinter = printers.NewWarningPrinter(test.o.IOStreams.ErrOut, printers.WarningPrinterOptions{Color: false})
296+
test.o.WarningPrinter = printers.NewWarningPrinter(test.o.IOStreams.ErrOut, printers.WarningPrinterOptions{Color: false})
297297

298298
restMapper, err := tf.ToRESTMapper()
299299
if err != nil {

staging/src/k8s.io/kubectl/pkg/cmd/debug/debug.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ type DebugOptions struct {
130130
podClient corev1client.CoreV1Interface
131131

132132
genericclioptions.IOStreams
133-
warningPrinter *printers.WarningPrinter
133+
WarningPrinter *printers.WarningPrinter
134134

135135
applier ProfileApplier
136136
}
@@ -224,8 +224,10 @@ func (o *DebugOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
224224
o.attachChanged = cmd.Flags().Changed("attach")
225225
o.shareProcessedChanged = cmd.Flags().Changed("share-processes")
226226

227-
// Warning printer
228-
o.warningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
227+
// Set default WarningPrinter
228+
if o.WarningPrinter == nil {
229+
o.WarningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
230+
}
229231
o.applier, err = NewProfileApplier(o.Profile)
230232
if err != nil {
231233
return err
@@ -307,9 +309,9 @@ func (o *DebugOptions) Validate() error {
307309
return fmt.Errorf("-i/--stdin is required for containers with -t/--tty=true")
308310
}
309311

310-
// warningPrinter
311-
if o.warningPrinter == nil {
312-
return fmt.Errorf("warningPrinter can not be used without initialization")
312+
// WarningPrinter
313+
if o.WarningPrinter == nil {
314+
return fmt.Errorf("WarningPrinter can not be used without initialization")
313315
}
314316

315317
return nil
@@ -764,7 +766,7 @@ func (o *DebugOptions) waitForContainer(ctx context.Context, ns, podName, contai
764766
return true, nil
765767
}
766768
if !o.Quiet && s.State.Waiting != nil && s.State.Waiting.Message != "" {
767-
o.warningPrinter.Print(fmt.Sprintf("container %s: %s", containerName, s.State.Waiting.Message))
769+
o.WarningPrinter.Print(fmt.Sprintf("container %s: %s", containerName, s.State.Waiting.Message))
768770
}
769771
return false, nil
770772
})

staging/src/k8s.io/kubectl/pkg/cmd/debug/debug_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,8 @@ func TestCompleteAndValidate(t *testing.T) {
15981598
t.Fatalf("CompleteAndValidate got error: '%v', wantError: %v", gotError, tc.wantError)
15991599
}
16001600

1601-
if diff := cmp.Diff(tc.wantOpts, opts, cmpFilter, cmpopts.IgnoreUnexported(DebugOptions{})); diff != "" {
1601+
if diff := cmp.Diff(tc.wantOpts, opts, cmpFilter, cmpopts.IgnoreFields(DebugOptions{},
1602+
"attachChanged", "shareProcessedChanged", "podClient", "WarningPrinter", "applier")); diff != "" {
16021603
t.Error("CompleteAndValidate unexpected diff in generated object: (-want +got):\n", diff)
16031604
}
16041605
})

staging/src/k8s.io/kubectl/pkg/cmd/delete/delete.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ type DeleteOptions struct {
133133
Result *resource.Result
134134

135135
genericclioptions.IOStreams
136-
warningPrinter *printers.WarningPrinter
136+
WarningPrinter *printers.WarningPrinter
137137
}
138138

139139
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
226226
}
227227
}
228228

229-
o.warningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
229+
// Set default WarningPrinter if not already set.
230+
if o.WarningPrinter == nil {
231+
o.WarningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
232+
}
230233

231234
return nil
232235
}
@@ -242,13 +245,13 @@ func (o *DeleteOptions) Validate() error {
242245
if o.DeleteAll && len(o.FieldSelector) > 0 {
243246
return fmt.Errorf("cannot set --all and --field-selector at the same time")
244247
}
245-
if o.warningPrinter == nil {
246-
return fmt.Errorf("warningPrinter can not be used without initialization")
248+
if o.WarningPrinter == nil {
249+
return fmt.Errorf("WarningPrinter can not be used without initialization")
247250
}
248251

249252
switch {
250253
case o.GracePeriod == 0 && o.ForceDeletion:
251-
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.")
254+
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.")
252255
case o.GracePeriod > 0 && o.ForceDeletion:
253256
return fmt.Errorf("--force and --grace-period greater than 0 cannot be specified together")
254257
}
@@ -312,7 +315,7 @@ func (o *DeleteOptions) DeleteResult(r *resource.Result) error {
312315
options.PropagationPolicy = &o.CascadingStrategy
313316

314317
if warnClusterScope && info.Mapping.Scope.Name() == meta.RESTScopeNameRoot {
315-
o.warningPrinter.Print("deleting cluster-scoped resources, not scoped to the provided namespace")
318+
o.WarningPrinter.Print("deleting cluster-scoped resources, not scoped to the provided namespace")
316319
warnClusterScope = false
317320
}
318321

staging/src/k8s.io/kubectl/pkg/cmd/drain/drain.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type DrainCmdOptions struct {
4949
nodeInfos []*resource.Info
5050

5151
genericclioptions.IOStreams
52-
warningPrinter *printers.WarningPrinter
52+
WarningPrinter *printers.WarningPrinter
5353
}
5454

5555
var (
@@ -254,7 +254,10 @@ func (o *DrainCmdOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
254254
return printer.PrintObj, nil
255255
}
256256

257-
o.warningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
257+
// Set default WarningPrinter if not already set.
258+
if o.WarningPrinter == nil {
259+
o.WarningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
260+
}
258261

259262
builder := f.NewBuilder().
260263
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
@@ -336,7 +339,7 @@ func (o *DrainCmdOptions) deleteOrEvictPodsSimple(nodeInfo *resource.Info) error
336339
return utilerrors.NewAggregate(errs)
337340
}
338341
if warnings := list.Warnings(); warnings != "" {
339-
o.warningPrinter.Print(warnings)
342+
o.WarningPrinter.Print(warnings)
340343
}
341344
if o.drainer.DryRunStrategy == cmdutil.DryRunClient {
342345
for _, pod := range list.Pods() {

staging/src/k8s.io/kubectl/pkg/cmd/set/set_env.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ type EnvOptions struct {
130130
clientset *kubernetes.Clientset
131131

132132
genericclioptions.IOStreams
133-
warningPrinter *printers.WarningPrinter
133+
WarningPrinter *printers.WarningPrinter
134134
}
135135

136136
// NewEnvOptions returns an EnvOptions indicating all containers in the selected
@@ -207,7 +207,7 @@ func contains(key string, keyList []string) bool {
207207
func (o *EnvOptions) keyToEnvName(key string) string {
208208
envName := strings.ToUpper(validEnvNameRegexp.ReplaceAllString(key, "_"))
209209
if envName != key {
210-
o.warningPrinter.Print(fmt.Sprintf("key %s transferred to %s", key, envName))
210+
o.WarningPrinter.Print(fmt.Sprintf("key %s transferred to %s", key, envName))
211211
}
212212
return envName
213213
}
@@ -247,7 +247,10 @@ func (o *EnvOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
247247
return err
248248
}
249249
o.builder = f.NewBuilder
250-
o.warningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
250+
// Set default WarningPrinter if not already set.
251+
if o.WarningPrinter == nil {
252+
o.WarningPrinter = printers.NewWarningPrinter(o.ErrOut, printers.WarningPrinterOptions{Color: term.AllowsColorOutput(o.ErrOut)})
253+
}
251254

252255
return nil
253256
}
@@ -266,8 +269,8 @@ func (o *EnvOptions) Validate() error {
266269
if len(o.Keys) > 0 && len(o.From) == 0 {
267270
return fmt.Errorf("when specifying --keys, a configmap or secret must be provided with --from")
268271
}
269-
if o.warningPrinter == nil {
270-
return fmt.Errorf("warningPrinter can not be used without initialization")
272+
if o.WarningPrinter == nil {
273+
return fmt.Errorf("WarningPrinter can not be used without initialization")
271274
}
272275
return nil
273276
}
@@ -421,7 +424,7 @@ func (o *EnvOptions) RunEnv() error {
421424
}
422425
}
423426

424-
o.warningPrinter.Print(fmt.Sprintf("%s/%s does not have any containers matching %q", objKind, objName, o.ContainerSelector))
427+
o.WarningPrinter.Print(fmt.Sprintf("%s/%s does not have any containers matching %q", objKind, objName, o.ContainerSelector))
425428
}
426429
return nil
427430
}

0 commit comments

Comments
 (0)