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.
This commit is contained in:
Arda Güçlü 2022-12-02 16:27:18 +03:00
parent 13be899b42
commit 76ee3788cc
2 changed files with 34 additions and 5 deletions

View File

@ -144,16 +144,18 @@ func (o *ScaleOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
if err != nil { if err != nil {
return err return err
} }
o.dryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd)
if err != nil {
return err
}
cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.dryRunStrategy)
printer, err := o.PrintFlags.ToPrinter() printer, err := o.PrintFlags.ToPrinter()
if err != nil { if err != nil {
return err return err
} }
o.PrintObj = printer.PrintObj o.PrintObj = printer.PrintObj
o.dryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd)
if err != nil {
return err
}
dynamicClient, err := f.DynamicClient() dynamicClient, err := f.DynamicClient()
if err != nil { if err != nil {
return err return err
@ -238,7 +240,10 @@ func (o *ScaleOptions) RunScale() error {
for _, info := range infos { for _, info := range infos {
mapping := info.ResourceMapping() mapping := info.ResourceMapping()
if o.dryRunStrategy == cmdutil.DryRunClient { 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 { if err := o.scaler.Scale(info.Namespace, info.Name, uint(o.Replicas), precondition, retry, waitForReplicas, mapping.Resource, o.dryRunStrategy == cmdutil.DryRunServer); err != nil {

View File

@ -1272,6 +1272,20 @@ run_rc_tests() {
### Scale multiple replication controllers ### Scale multiple replication controllers
kubectl create -f test/e2e/testing-manifests/guestbook/legacy/redis-master-controller.yaml "${kube_flags[@]}" kubectl create -f test/e2e/testing-manifests/guestbook/legacy/redis-master-controller.yaml "${kube_flags[@]}"
kubectl create -f test/e2e/testing-manifests/guestbook/legacy/redis-slave-controller.yaml "${kube_flags[@]}" kubectl create -f test/e2e/testing-manifests/guestbook/legacy/redis-slave-controller.yaml "${kube_flags[@]}"
# Command dry-run client
output_message=$(kubectl scale rc/redis-master rc/redis-slave --replicas=4 --dry-run=client "${kube_flags[@]}")
# Post-condition dry-run client: 1 replicas each
kube::test::if_has_string "${output_message}" 'replicationcontroller/redis-master scaled (dry run)'
kube::test::if_has_string "${output_message}" 'replicationcontroller/redis-slave scaled (dry run)'
kube::test::get_object_assert 'rc redis-master' "{{$rc_replicas_field}}" '1'
kube::test::get_object_assert 'rc redis-slave' "{{$rc_replicas_field}}" '2'
# Command dry-run server
output_message=$(kubectl scale rc/redis-master rc/redis-slave --replicas=4 --dry-run=server "${kube_flags[@]}")
# Post-condition dry-run server: 1 replicas each
kube::test::if_has_string "${output_message}" 'replicationcontroller/redis-master scaled (server dry run)'
kube::test::if_has_string "${output_message}" 'replicationcontroller/redis-slave scaled (server dry run)'
kube::test::get_object_assert 'rc redis-master' "{{$rc_replicas_field}}" '1'
kube::test::get_object_assert 'rc redis-slave' "{{$rc_replicas_field}}" '2'
# Command # Command
kubectl scale rc/redis-master rc/redis-slave --replicas=4 "${kube_flags[@]}" kubectl scale rc/redis-master rc/redis-slave --replicas=4 "${kube_flags[@]}"
# Post-condition: 4 replicas each # Post-condition: 4 replicas each
@ -1282,6 +1296,16 @@ run_rc_tests() {
### Scale a deployment ### Scale a deployment
kubectl create -f test/fixtures/doc-yaml/user-guide/deployment.yaml "${kube_flags[@]}" kubectl create -f test/fixtures/doc-yaml/user-guide/deployment.yaml "${kube_flags[@]}"
# Command dry-run client
output_message=$(kubectl scale --current-replicas=3 --replicas=1 deployment/nginx-deployment --dry-run=client)
# Post-condition: 3 replica for nginx-deployment dry-run client
kube::test::if_has_string "${output_message}" 'nginx-deployment scaled (dry run)'
kube::test::get_object_assert 'deployment nginx-deployment' "{{${deployment_replicas:?}}}" '3'
# Command dry-run server
output_message=$(kubectl scale --current-replicas=3 --replicas=1 deployment/nginx-deployment --dry-run=server)
# Post-condition: 3 replica for nginx-deployment dry-run server
kube::test::if_has_string "${output_message}" 'nginx-deployment scaled (server dry run)'
kube::test::get_object_assert 'deployment nginx-deployment' "{{${deployment_replicas:?}}}" '3'
# Command # Command
kubectl scale --current-replicas=3 --replicas=1 deployment/nginx-deployment kubectl scale --current-replicas=3 --replicas=1 deployment/nginx-deployment
# Post-condition: 1 replica for nginx-deployment # Post-condition: 1 replica for nginx-deployment