mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
Fix create deployment port not working
This commit is contained in:
parent
7c3f7065db
commit
24766024c1
@ -204,7 +204,7 @@ func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment {
|
|||||||
namespace = o.Namespace
|
namespace = o.Namespace
|
||||||
}
|
}
|
||||||
|
|
||||||
return &appsv1.Deployment{
|
deploy := &appsv1.Deployment{
|
||||||
TypeMeta: metav1.TypeMeta{APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment"},
|
TypeMeta: metav1.TypeMeta{APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment"},
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: o.Name,
|
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
|
// buildPodSpec parses the image strings and assemble them into the Containers
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@ -52,7 +53,7 @@ func TestCreateDeployment(t *testing.T) {
|
|||||||
|
|
||||||
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
||||||
cmd := NewCmdCreateDeployment(tf, ioStreams)
|
cmd := NewCmdCreateDeployment(tf, ioStreams)
|
||||||
cmd.Flags().Set("dry-run", "true")
|
cmd.Flags().Set("dry-run", "client")
|
||||||
cmd.Flags().Set("output", "name")
|
cmd.Flags().Set("output", "name")
|
||||||
cmd.Flags().Set("image", "hollywood/jonny.depp:v2")
|
cmd.Flags().Set("image", "hollywood/jonny.depp:v2")
|
||||||
cmd.Run(cmd, []string{depName})
|
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) {
|
func TestCreateDeploymentNoImage(t *testing.T) {
|
||||||
depName := "jonny-dep"
|
depName := "jonny-dep"
|
||||||
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
tf := cmdtesting.NewTestFactory().WithNamespace("test")
|
||||||
|
Loading…
Reference in New Issue
Block a user