From b6213ed931930eba27b4a53db54b4b4d224a1c59 Mon Sep 17 00:00:00 2001 From: zhouya0 Date: Fri, 29 May 2020 14:52:07 +0800 Subject: [PATCH] Support kubectl create deployment with replicas --- .../pkg/cmd/create/create_deployment.go | 9 ++++-- .../pkg/cmd/create/create_deployment_test.go | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment.go index 2a64e289f04..b579298ceb7 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment.go @@ -48,6 +48,9 @@ var ( # Create a deployment with command kubectl create deployment my-dep --image=busybox -- date + # Create a deployment named my-dep that runs the nginx image with 3 replicas. + kubectl create deployment my-dep --image=nginx --replicas=3 + # Create a deployment named my-dep that runs the busybox image and expose port 5701. kubectl create deployment my-dep --image=busybox --port=5701`)) ) @@ -61,6 +64,7 @@ type CreateDeploymentOptions struct { Name string Images []string Port int32 + Replicas int32 Command []string Namespace string EnforceNamespace bool @@ -76,6 +80,7 @@ type CreateDeploymentOptions struct { func NewCreateCreateDeploymentOptions(ioStreams genericclioptions.IOStreams) *CreateDeploymentOptions { return &CreateDeploymentOptions{ Port: -1, + Replicas: 1, PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme), IOStreams: ioStreams, } @@ -107,6 +112,7 @@ func NewCmdCreateDeployment(f cmdutil.Factory, ioStreams genericclioptions.IOStr cmd.Flags().StringSliceVar(&o.Images, "image", o.Images, "Image names to run.") cmd.MarkFlagRequired("image") cmd.Flags().Int32Var(&o.Port, "port", o.Port, "The port that this container exposes.") + cmd.Flags().Int32VarP(&o.Replicas, "replicas", "r", o.Replicas, "Number of replicas to create. Default is 1.") cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-create") return cmd @@ -196,7 +202,6 @@ func (o *CreateDeploymentOptions) Run() error { } func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment { - one := int32(1) labels := map[string]string{"app": o.Name} selector := metav1.LabelSelector{MatchLabels: labels} namespace := "" @@ -212,7 +217,7 @@ func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment { Namespace: namespace, }, Spec: appsv1.DeploymentSpec{ - Replicas: &one, + Replicas: &o.Replicas, Selector: &selector, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment_test.go index 73719b0f837..f20af00844f 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment_test.go @@ -94,6 +94,37 @@ func TestCreateDeploymentWithPort(t *testing.T) { } } +func TestCreateDeploymentWithReplicas(t *testing.T) { + depName := "jonny-dep" + replicas := "3" + tf := cmdtesting.NewTestFactory().WithNamespace("test") + defer tf.Cleanup() + + ns := scheme.Codecs.WithoutConversion() + fakeDiscovery := "{\"kind\":\"APIResourceList\",\"apiVersion\":\"v1\",\"groupVersion\":\"apps/v1\",\"resources\":[{\"name\":\"deployments\",\"singularName\":\"\",\"namespaced\":true,\"kind\":\"Deployment\",\"verbs\":[\"create\",\"delete\",\"deletecollection\",\"get\",\"list\",\"patch\",\"update\",\"watch\"],\"shortNames\":[\"deploy\"],\"categories\":[\"all\"]}]}" + tf.Client = &fake.RESTClient{ + NegotiatedSerializer: ns, + Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) { + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))), + }, nil + }), + } + tf.ClientConfigVal = &restclient.Config{} + + ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams() + cmd := NewCmdCreateDeployment(tf, ioStreams) + cmd.Flags().Set("dry-run", "client") + cmd.Flags().Set("output", "jsonpath={.spec.replicas}") + cmd.Flags().Set("replicas", replicas) + cmd.Flags().Set("image", "hollywood/jonny.depp:v2") + cmd.Run(cmd, []string{depName}) + if buf.String() != replicas { + t.Errorf("expected output: %s, but got: %s", replicas, buf.String()) + } +} + func TestCreateDeploymentNoImage(t *testing.T) { depName := "jonny-dep" tf := cmdtesting.NewTestFactory().WithNamespace("test")