From 0eb2f03176aad64012dfeae9b476379c8713af64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Fri, 2 Dec 2022 16:00:22 +0300 Subject: [PATCH 1/3] kubectl scale: Use visitor only once `kubectl scale` calls visitor two times. Second call fails when the piped input is passed by returning an `error: no objects passed to scale` error. This PR uses the result of first visitor and fixes that piped input problem. In addition to that, this PR also adds new scale test to verify. Kubernetes-commit: 13be899b422a1f68c38e3a9c9d88831db709a32d --- pkg/cmd/scale/scale.go | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/pkg/cmd/scale/scale.go b/pkg/cmd/scale/scale.go index 9571351d4..2aaa99191 100644 --- a/pkg/cmd/scale/scale.go +++ b/pkg/cmd/scale/scale.go @@ -209,13 +209,10 @@ func (o *ScaleOptions) RunScale() error { return err } - infos := []*resource.Info{} - r.Visit(func(info *resource.Info, err error) error { - if err == nil { - infos = append(infos, info) - } - return nil - }) + infos, err := r.Infos() + if err != nil { + return err + } if len(o.ResourceVersion) != 0 && len(infos) > 1 { return fmt.Errorf("cannot use --resource-version with multiple resources") @@ -234,17 +231,16 @@ func (o *ScaleOptions) RunScale() error { waitForReplicas = scale.NewRetryParams(1*time.Second, o.Timeout) } - counter := 0 - err = r.Visit(func(info *resource.Info, err error) error { - if err != nil { - return err - } - counter++ + if len(infos) == 0 { + return fmt.Errorf("no objects passed to scale") + } + for _, info := range infos { mapping := info.ResourceMapping() if o.dryRunStrategy == cmdutil.DryRunClient { return o.PrintObj(info.Object, o.Out) } + if err := o.scaler.Scale(info.Namespace, info.Name, uint(o.Replicas), precondition, retry, waitForReplicas, mapping.Resource, o.dryRunStrategy == cmdutil.DryRunServer); err != nil { return err } @@ -263,14 +259,12 @@ func (o *ScaleOptions) RunScale() error { } } - return o.PrintObj(info.Object, o.Out) - }) - if err != nil { - return err - } - if counter == 0 { - return fmt.Errorf("no objects passed to scale") + err := o.PrintObj(info.Object, o.Out) + if err != nil { + return err + } } + return nil } From 68a87e8d32f385cd3ead4e3a45e036fe1ac78b70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Fri, 2 Dec 2022 16:27:18 +0300 Subject: [PATCH 2/3] kubectl scale: Add dry-run prefix to indicate result is not applied Currently, if user executes `kubectl scale --dry-run`, output has no indicator showing that this is not applied in reality. This PR adds dry run suffix to the output as well as more integration tests to verify it. Kubernetes-commit: 76ee3788ccbac9003e3f24de9000ebd91c27611f --- pkg/cmd/scale/scale.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/scale/scale.go b/pkg/cmd/scale/scale.go index 2aaa99191..1f1f0a531 100644 --- a/pkg/cmd/scale/scale.go +++ b/pkg/cmd/scale/scale.go @@ -144,16 +144,18 @@ func (o *ScaleOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st if err != nil { return err } + + o.dryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd) + if err != nil { + return err + } + cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.dryRunStrategy) printer, err := o.PrintFlags.ToPrinter() if err != nil { return err } o.PrintObj = printer.PrintObj - o.dryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd) - if err != nil { - return err - } dynamicClient, err := f.DynamicClient() if err != nil { return err @@ -238,7 +240,10 @@ func (o *ScaleOptions) RunScale() error { for _, info := range infos { mapping := info.ResourceMapping() if o.dryRunStrategy == cmdutil.DryRunClient { - return o.PrintObj(info.Object, o.Out) + if err := o.PrintObj(info.Object, o.Out); err != nil { + return err + } + continue } if err := o.scaler.Scale(info.Namespace, info.Name, uint(o.Replicas), precondition, retry, waitForReplicas, mapping.Resource, o.dryRunStrategy == cmdutil.DryRunServer); err != nil { From ee99503f34bae37bec4ab137a7298569b213c596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Fri, 2 Dec 2022 17:23:44 +0300 Subject: [PATCH 3/3] kubectl scale: proceed even if there is invalid resource in multi Kubernetes-commit: b84f192acc61b5fa9dc438950e6cc57f75889853 --- pkg/cmd/scale/scale.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/scale/scale.go b/pkg/cmd/scale/scale.go index 1f1f0a531..9529aeef8 100644 --- a/pkg/cmd/scale/scale.go +++ b/pkg/cmd/scale/scale.go @@ -211,10 +211,11 @@ func (o *ScaleOptions) RunScale() error { return err } - infos, err := r.Infos() - if err != nil { - return err - } + // We don't immediately return infoErr if it is not nil. + // Because we want to proceed for other valid resources and + // at the end of the function, we'll return this + // to show invalid resources to the user. + infos, infoErr := r.Infos() if len(o.ResourceVersion) != 0 && len(infos) > 1 { return fmt.Errorf("cannot use --resource-version with multiple resources") @@ -270,7 +271,7 @@ func (o *ScaleOptions) RunScale() error { } } - return nil + return infoErr } func scaler(f cmdutil.Factory) (scale.Scaler, error) {