From 24766024c17e6090b694b0f57d0b051884a2dac1 Mon Sep 17 00:00:00 2001 From: zhouya0 Date: Wed, 27 May 2020 15:56:35 +0800 Subject: [PATCH] Fix create deployment port not working --- .../pkg/cmd/create/create_deployment.go | 7 +++- .../pkg/cmd/create/create_deployment_test.go | 34 ++++++++++++++++++- 2 files changed, 39 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 7da3a6f9ec0..2a64e289f04 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 @@ -204,7 +204,7 @@ func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment { namespace = o.Namespace } - return &appsv1.Deployment{ + deploy := &appsv1.Deployment{ TypeMeta: metav1.TypeMeta{APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment"}, ObjectMeta: metav1.ObjectMeta{ Name: o.Name, @@ -222,6 +222,11 @@ func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment { }, }, } + + if o.Port >= 0 && len(deploy.Spec.Template.Spec.Containers) > 0 { + deploy.Spec.Template.Spec.Containers[0].Ports = []corev1.ContainerPort{{ContainerPort: o.Port}} + } + return deploy } // buildPodSpec parses the image strings and assemble them into the Containers 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 878f2a3c42f..73719b0f837 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 @@ -20,6 +20,7 @@ import ( "bytes" "io/ioutil" "net/http" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -52,7 +53,7 @@ func TestCreateDeployment(t *testing.T) { ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams() cmd := NewCmdCreateDeployment(tf, ioStreams) - cmd.Flags().Set("dry-run", "true") + cmd.Flags().Set("dry-run", "client") cmd.Flags().Set("output", "name") cmd.Flags().Set("image", "hollywood/jonny.depp:v2") cmd.Run(cmd, []string{depName}) @@ -62,6 +63,37 @@ func TestCreateDeployment(t *testing.T) { } } +func TestCreateDeploymentWithPort(t *testing.T) { + depName := "jonny-dep" + port := "5701" + 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", "yaml") + cmd.Flags().Set("port", port) + cmd.Flags().Set("image", "hollywood/jonny.depp:v2") + cmd.Run(cmd, []string{depName}) + if !strings.Contains(buf.String(), port) { + t.Errorf("unexpected output: %s\nexpected to contain: %s", buf.String(), port) + } +} + func TestCreateDeploymentNoImage(t *testing.T) { depName := "jonny-dep" tf := cmdtesting.NewTestFactory().WithNamespace("test")