Add --hostport to run-container.

This helps as a starting point to show a single-machine container.
Its easier to use this as an example to show where host port mapping breaks and move on to
services.
pull/6/head
Rohit Jnagal 2015-04-29 23:54:23 +00:00
parent a100e976ec
commit 9cbfb0c3f9
7 changed files with 85 additions and 10 deletions

View File

@ -530,6 +530,7 @@ _kubectl_run-container()
flags+=("--generator=") flags+=("--generator=")
flags+=("--help") flags+=("--help")
flags+=("-h") flags+=("-h")
flags+=("--hostport=")
flags+=("--image=") flags+=("--image=")
flags+=("--labels=") flags+=("--labels=")
two_word_flags+=("-l") two_word_flags+=("-l")

View File

@ -66,4 +66,4 @@ kubectl
* [kubectl update](kubectl_update.md) - Update a resource by filename or stdin. * [kubectl update](kubectl_update.md) - Update a resource by filename or stdin.
* [kubectl version](kubectl_version.md) - Print the client and server version information. * [kubectl version](kubectl_version.md) - Print the client and server version information.
###### Auto generated by spf13/cobra at 2015-04-30 16:43:53.288271038 +0000 UTC ###### Auto generated by spf13/cobra at 2015-05-01 20:16:42.546735249 +0000 UTC

View File

@ -34,6 +34,7 @@ $ kubectl run-container nginx --image=nginx --overrides='{ "apiVersion": "v1beta
--dry-run=false: If true, only print the object that would be sent, without sending it. --dry-run=false: If true, only print the object that would be sent, without sending it.
--generator="run-container/v1": The name of the API generator to use. Default is 'run-container-controller/v1'. --generator="run-container/v1": The name of the API generator to use. Default is 'run-container-controller/v1'.
-h, --help=false: help for run-container -h, --help=false: help for run-container
--hostport=-1: The host port mapping for the container port. To demonstrate a single-machine container.
--image="": The image for the container to run. --image="": The image for the container to run.
-l, --labels="": Labels to apply to the pod(s) created by this call to run-container. -l, --labels="": Labels to apply to the pod(s) created by this call to run-container.
--no-headers=false: When using the default output, don't print headers. --no-headers=false: When using the default output, don't print headers.
@ -78,4 +79,4 @@ $ kubectl run-container nginx --image=nginx --overrides='{ "apiVersion": "v1beta
### SEE ALSO ### SEE ALSO
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
###### Auto generated by spf13/cobra at 2015-04-29 15:25:11.0330734 +0000 UTC ###### Auto generated by spf13/cobra at 2015-04-29 23:46:39.503475144 +0000 UTC

View File

@ -30,6 +30,10 @@ Creates a replication controller to manage the created container(s).
\fB\-h\fP, \fB\-\-help\fP=false \fB\-h\fP, \fB\-\-help\fP=false
help for run\-container help for run\-container
.PP
\fB\-\-hostport\fP=\-1
The host port mapping for the container port. To demonstrate a single\-machine container.
.PP .PP
\fB\-\-image\fP="" \fB\-\-image\fP=""
The image for the container to run. The image for the container to run.

View File

@ -60,6 +60,7 @@ func NewCmdRunContainer(f *cmdutil.Factory, out io.Writer) *cobra.Command {
cmd.Flags().Bool("dry-run", false, "If true, only print the object that would be sent, without sending it.") cmd.Flags().Bool("dry-run", false, "If true, only print the object that would be sent, without sending it.")
cmd.Flags().String("overrides", "", "An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field.") cmd.Flags().String("overrides", "", "An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field.")
cmd.Flags().Int("port", -1, "The port that this container exposes.") cmd.Flags().Int("port", -1, "The port that this container exposes.")
cmd.Flags().Int("hostport", -1, "The host port mapping for the container port. To demonstrate a single-machine container.")
cmd.Flags().StringP("labels", "l", "", "Labels to apply to the pod(s) created by this call to run-container.") cmd.Flags().StringP("labels", "l", "", "Labels to apply to the pod(s) created by this call to run-container.")
return cmd return cmd
} }

View File

@ -17,6 +17,7 @@ limitations under the License.
package kubectl package kubectl
import ( import (
"fmt"
"strconv" "strconv"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@ -32,6 +33,7 @@ func (BasicReplicationController) ParamNames() []GeneratorParam {
{"replicas", true}, {"replicas", true},
{"image", true}, {"image", true},
{"port", false}, {"port", false},
{"hostport", false},
} }
} }
@ -78,19 +80,34 @@ func (BasicReplicationController) Generate(params map[string]string) (runtime.Ob
}, },
} }
port := -1
hostPort := -1
if len(params["port"]) > 0 { if len(params["port"]) > 0 {
port, err := strconv.Atoi(params["port"]) port, err = strconv.Atoi(params["port"])
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
// Don't include the port if it was not specified. if len(params["hostport"]) > 0 {
if port > 0 { hostPort, err = strconv.Atoi(params["hostport"])
controller.Spec.Template.Spec.Containers[0].Ports = []api.ContainerPort{ if err != nil {
{ return nil, err
ContainerPort: port, }
}, if hostPort > 0 && port < 0 {
} return nil, fmt.Errorf("--hostport requires --port to be specified")
}
}
// Don't include the port if it was not specified.
if port > 0 {
controller.Spec.Template.Spec.Containers[0].Ports = []api.ContainerPort{
{
ContainerPort: port,
},
}
if hostPort > 0 {
controller.Spec.Template.Spec.Containers[0].Ports[0].HostPort = hostPort
} }
} }
return &controller, nil return &controller, nil

View File

@ -96,6 +96,54 @@ func TestGenerate(t *testing.T) {
}, },
}, },
}, },
{
params: map[string]string{
"name": "foo",
"image": "someimage",
"replicas": "1",
"port": "80",
"hostport": "80",
},
expected: &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: "foo",
Labels: map[string]string{"run-container": "foo"},
},
Spec: api.ReplicationControllerSpec{
Replicas: 1,
Selector: map[string]string{"run-container": "foo"},
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{"run-container": "foo"},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "foo",
Image: "someimage",
Ports: []api.ContainerPort{
{
ContainerPort: 80,
HostPort: 80,
},
},
},
},
},
},
},
},
},
{
params: map[string]string{
"name": "foo",
"image": "someimage",
"replicas": "1",
"hostport": "80",
},
expected: nil,
expectErr: true,
},
{ {
params: map[string]string{ params: map[string]string{
"name": "foo", "name": "foo",
@ -134,6 +182,9 @@ func TestGenerate(t *testing.T) {
if !test.expectErr && err != nil { if !test.expectErr && err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
if test.expectErr && err != nil {
continue
}
if !reflect.DeepEqual(obj.(*api.ReplicationController).Spec.Template, test.expected.Spec.Template) { if !reflect.DeepEqual(obj.(*api.ReplicationController).Spec.Template, test.expected.Spec.Template) {
t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected.Spec.Template, obj.(*api.ReplicationController).Spec.Template) t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected.Spec.Template, obj.(*api.ReplicationController).Spec.Template)
} }