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.
This commit is contained in:
Arda Güçlü 2022-12-02 16:00:22 +03:00
parent 3e26e104bd
commit 13be899b42
2 changed files with 23 additions and 20 deletions

View File

@ -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)
infos, err := r.Infos()
if err != nil {
return err
}
return nil
})
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
if len(infos) == 0 {
return fmt.Errorf("no objects passed to scale")
}
counter++
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)
})
err := o.PrintObj(info.Object, o.Out)
if err != nil {
return err
}
if counter == 0 {
return fmt.Errorf("no objects passed to scale")
}
return nil
}

View File

@ -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[@]}")