Skip to content

Commit 2476602

Browse files
committed
Fix create deployment port not working
1 parent 7c3f706 commit 2476602

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment {
204204
namespace = o.Namespace
205205
}
206206

207-
return &appsv1.Deployment{
207+
deploy := &appsv1.Deployment{
208208
TypeMeta: metav1.TypeMeta{APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment"},
209209
ObjectMeta: metav1.ObjectMeta{
210210
Name: o.Name,
@@ -222,6 +222,11 @@ func (o *CreateDeploymentOptions) createDeployment() *appsv1.Deployment {
222222
},
223223
},
224224
}
225+
226+
if o.Port >= 0 && len(deploy.Spec.Template.Spec.Containers) > 0 {
227+
deploy.Spec.Template.Spec.Containers[0].Ports = []corev1.ContainerPort{{ContainerPort: o.Port}}
228+
}
229+
return deploy
225230
}
226231

227232
// buildPodSpec parses the image strings and assemble them into the Containers

staging/src/k8s.io/kubectl/pkg/cmd/create/create_deployment_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"io/ioutil"
2222
"net/http"
23+
"strings"
2324
"testing"
2425

2526
"github.com/stretchr/testify/assert"
@@ -52,7 +53,7 @@ func TestCreateDeployment(t *testing.T) {
5253

5354
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
5455
cmd := NewCmdCreateDeployment(tf, ioStreams)
55-
cmd.Flags().Set("dry-run", "true")
56+
cmd.Flags().Set("dry-run", "client")
5657
cmd.Flags().Set("output", "name")
5758
cmd.Flags().Set("image", "hollywood/jonny.depp:v2")
5859
cmd.Run(cmd, []string{depName})
@@ -62,6 +63,37 @@ func TestCreateDeployment(t *testing.T) {
6263
}
6364
}
6465

66+
func TestCreateDeploymentWithPort(t *testing.T) {
67+
depName := "jonny-dep"
68+
port := "5701"
69+
tf := cmdtesting.NewTestFactory().WithNamespace("test")
70+
defer tf.Cleanup()
71+
72+
ns := scheme.Codecs.WithoutConversion()
73+
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\"]}]}"
74+
tf.Client = &fake.RESTClient{
75+
NegotiatedSerializer: ns,
76+
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
77+
return &http.Response{
78+
StatusCode: http.StatusOK,
79+
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))),
80+
}, nil
81+
}),
82+
}
83+
tf.ClientConfigVal = &restclient.Config{}
84+
85+
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
86+
cmd := NewCmdCreateDeployment(tf, ioStreams)
87+
cmd.Flags().Set("dry-run", "client")
88+
cmd.Flags().Set("output", "yaml")
89+
cmd.Flags().Set("port", port)
90+
cmd.Flags().Set("image", "hollywood/jonny.depp:v2")
91+
cmd.Run(cmd, []string{depName})
92+
if !strings.Contains(buf.String(), port) {
93+
t.Errorf("unexpected output: %s\nexpected to contain: %s", buf.String(), port)
94+
}
95+
}
96+
6597
func TestCreateDeploymentNoImage(t *testing.T) {
6698
depName := "jonny-dep"
6799
tf := cmdtesting.NewTestFactory().WithNamespace("test")

0 commit comments

Comments
 (0)