From a24be450b20017788e02372a44bc839d9b53f5bd Mon Sep 17 00:00:00 2001 From: zhengjiajin Date: Wed, 23 Aug 2017 17:09:16 +0800 Subject: [PATCH 1/2] fix issue(49965)kubectl scale also says that it can work based on a label selector or all --- pkg/kubectl/cmd/scale.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/kubectl/cmd/scale.go b/pkg/kubectl/cmd/scale.go index 950c55ba763..c3cb64b86db 100644 --- a/pkg/kubectl/cmd/scale.go +++ b/pkg/kubectl/cmd/scale.go @@ -77,6 +77,8 @@ func NewCmdScale(f cmdutil.Factory, out io.Writer) *cobra.Command { ValidArgs: validArgs, ArgAliases: argAliases, } + cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)") + cmd.Flags().Bool("all", false, "Select all resources in the namespace of the specified resource types") cmd.Flags().String("resource-version", "", i18n.T("Precondition for resource version. Requires that the current resource version match this value in order to scale.")) cmd.Flags().Int("current-replicas", -1, "Precondition for current size. Requires that the current size of the resource match this value in order to scale.") cmd.Flags().Int("replicas", -1, "The new desired number of replicas. Required.") @@ -98,13 +100,17 @@ func RunScale(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []strin return err } + selector := cmdutil.GetFlagString(cmd, "selector") + all := cmdutil.GetFlagBool(cmd, "all") + mapper, _ := f.Object() r := f.NewBuilder(true). ContinueOnError(). NamespaceParam(cmdNamespace).DefaultNamespace(). FilenameParam(enforceNamespace, options). - ResourceTypeOrNameArgs(false, args...). + ResourceTypeOrNameArgs(all, args...). Flatten(). + SelectorParam(selector). Do() err = r.Err() if resource.IsUsageError(err) { From f441de6be82f74c853574816829903a219bb78c4 Mon Sep 17 00:00:00 2001 From: zhengjiajin Date: Sun, 27 Aug 2017 15:42:20 +0800 Subject: [PATCH 2/2] Add bash test for kubectl scale --selector and --all --- hack/make-rules/test-cmd-util.sh | 19 +++++++++++++++++++ hack/testdata/scale-deploy-1.yaml | 25 +++++++++++++++++++++++++ hack/testdata/scale-deploy-2.yaml | 25 +++++++++++++++++++++++++ hack/testdata/scale-deploy-3.yaml | 25 +++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 hack/testdata/scale-deploy-1.yaml create mode 100644 hack/testdata/scale-deploy-2.yaml create mode 100644 hack/testdata/scale-deploy-3.yaml diff --git a/hack/make-rules/test-cmd-util.sh b/hack/make-rules/test-cmd-util.sh index 7a32851dfa4..481b5a05695 100755 --- a/hack/make-rules/test-cmd-util.sh +++ b/hack/make-rules/test-cmd-util.sh @@ -2899,8 +2899,27 @@ run_rs_tests() { kubectl scale --current-replicas=3 --replicas=2 replicasets frontend "${kube_flags[@]}" # Post-condition: 2 replicas kube::test::get_object_assert 'rs frontend' "{{$rs_replicas_field}}" '2' + + # Set up three deploy, two deploy have same label + kubectl create -f hack/testdata/scale-deploy-1.yaml "${kube_flags[@]}" + kubectl create -f hack/testdata/scale-deploy-2.yaml "${kube_flags[@]}" + kubectl create -f hack/testdata/scale-deploy-3.yaml "${kube_flags[@]}" + kube::test::get_object_assert 'deploy scale-1' "{{.spec.replicas}}" '1' + kube::test::get_object_assert 'deploy scale-2' "{{.spec.replicas}}" '1' + kube::test::get_object_assert 'deploy scale-3' "{{.spec.replicas}}" '1' + # Test kubectl scale --selector + kubectl scale deploy --replicas=2 -l run=hello + kube::test::get_object_assert 'deploy scale-1' "{{.spec.replicas}}" '2' + kube::test::get_object_assert 'deploy scale-2' "{{.spec.replicas}}" '2' + kube::test::get_object_assert 'deploy scale-3' "{{.spec.replicas}}" '1' + # Test kubectl scale --all + kubectl scale deploy --replicas=3 --all + kube::test::get_object_assert 'deploy scale-1' "{{.spec.replicas}}" '3' + kube::test::get_object_assert 'deploy scale-2' "{{.spec.replicas}}" '3' + kube::test::get_object_assert 'deploy scale-3' "{{.spec.replicas}}" '3' # Clean-up kubectl delete rs frontend "${kube_flags[@]}" + kubectl delete deploy scale-1 scale-2 scale-3 "${kube_flags[@]}" ### Expose replica set as service kubectl create -f hack/testdata/frontend-replicaset.yaml "${kube_flags[@]}" diff --git a/hack/testdata/scale-deploy-1.yaml b/hack/testdata/scale-deploy-1.yaml new file mode 100644 index 00000000000..b1e6ee5a01e --- /dev/null +++ b/hack/testdata/scale-deploy-1.yaml @@ -0,0 +1,25 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + labels: + run: hello + name: scale-1 +spec: + replicas: 1 + selector: + matchLabels: + run: hello + strategy: + rollingUpdate: + maxSurge: 1 + maxUnavailable: 1 + type: RollingUpdate + template: + metadata: + labels: + run: hello + spec: + containers: + - image: aronchick/hello-node:2.0 + imagePullPolicy: IfNotPresent + name: hello diff --git a/hack/testdata/scale-deploy-2.yaml b/hack/testdata/scale-deploy-2.yaml new file mode 100644 index 00000000000..dc954b5b5cd --- /dev/null +++ b/hack/testdata/scale-deploy-2.yaml @@ -0,0 +1,25 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + labels: + run: hello + name: scale-2 +spec: + replicas: 1 + selector: + matchLabels: + run: hello + strategy: + rollingUpdate: + maxSurge: 1 + maxUnavailable: 1 + type: RollingUpdate + template: + metadata: + labels: + run: hello + spec: + containers: + - image: aronchick/hello-node:2.0 + imagePullPolicy: IfNotPresent + name: hello diff --git a/hack/testdata/scale-deploy-3.yaml b/hack/testdata/scale-deploy-3.yaml new file mode 100644 index 00000000000..63a7d759335 --- /dev/null +++ b/hack/testdata/scale-deploy-3.yaml @@ -0,0 +1,25 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + labels: + foo: boo + name: scale-3 +spec: + replicas: 1 + selector: + matchLabels: + run: hello + strategy: + rollingUpdate: + maxSurge: 1 + maxUnavailable: 1 + type: RollingUpdate + template: + metadata: + labels: + run: hello + spec: + containers: + - image: aronchick/hello-node:2.0 + imagePullPolicy: IfNotPresent + name: hello