From 13be899b422a1f68c38e3a9c9d88831db709a32d 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] 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. --- .../src/k8s.io/kubectl/pkg/cmd/scale/scale.go | 34 ++++++++----------- test/cmd/core.sh | 9 +++++ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/scale/scale.go b/staging/src/k8s.io/kubectl/pkg/cmd/scale/scale.go index 9571351d40e..2aaa99191a9 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/scale/scale.go +++ b/staging/src/k8s.io/kubectl/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 } diff --git a/test/cmd/core.sh b/test/cmd/core.sh index 1ce79d8188a..4733c9212fe 100755 --- a/test/cmd/core.sh +++ b/test/cmd/core.sh @@ -1289,6 +1289,15 @@ run_rc_tests() { # Clean-up kubectl delete deployment/nginx-deployment "${kube_flags[@]}" + ### Scale a deployment with piped input + kubectl create -f test/fixtures/doc-yaml/user-guide/deployment.yaml "${kube_flags[@]}" + # Command + kubectl get deployment/nginx-deployment -o json | kubectl scale --replicas=2 -f - + # Post-condition: 2 replica for nginx-deployment + kube::test::get_object_assert 'deployment nginx-deployment' "{{${deployment_replicas:?}}}" '2' + # Clean-up + kubectl delete deployment/nginx-deployment "${kube_flags[@]}" + ### Expose deployments by creating a service # Uses deployment selectors for created service output_message=$(kubectl expose -f test/fixtures/pkg/kubectl/cmd/expose/appsv1deployment.yaml --port 80 2>&1 "${kube_flags[@]}")