Fix create deployment port not working

This commit is contained in:
zhouya0 2020-05-27 15:56:35 +08:00
parent 7c3f7065db
commit 24766024c1
2 changed files with 39 additions and 2 deletions

View File

@ -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

View File

@ -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")