Merge pull request #7536 from rjnagal/node

Add --hostport to run-container.
This commit is contained in:
Victor Marmol
2015-05-04 12:55:12 -07:00
7 changed files with 85 additions and 10 deletions

View File

@@ -61,6 +61,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().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("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.")
return cmd
}

View File

@@ -17,6 +17,7 @@ limitations under the License.
package kubectl
import (
"fmt"
"strconv"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -32,6 +33,7 @@ func (BasicReplicationController) ParamNames() []GeneratorParam {
{"replicas", true},
{"image", true},
{"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 {
port, err := strconv.Atoi(params["port"])
port, err = strconv.Atoi(params["port"])
if err != nil {
return nil, err
}
}
// 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 len(params["hostport"]) > 0 {
hostPort, err = strconv.Atoi(params["hostport"])
if err != nil {
return nil, err
}
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

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{
"name": "foo",
@@ -134,6 +182,9 @@ func TestGenerate(t *testing.T) {
if !test.expectErr && err != nil {
t.Errorf("unexpected error: %v", err)
}
if test.expectErr && err != nil {
continue
}
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)
}