mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-19 18:02:01 +00:00
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:
parent
3e26e104bd
commit
13be899b42
@ -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
|
||||
}
|
||||
|
||||
|
@ -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[@]}")
|
||||
|
Loading…
Reference in New Issue
Block a user