add port validate when --port is set or --expose=true

This commit is contained in:
lojies 2016-07-26 17:12:10 +08:00
parent 956501b1f0
commit ef87972835
3 changed files with 10 additions and 10 deletions

View File

@ -112,7 +112,7 @@ func addRunFlags(cmd *cobra.Command) {
cmd.Flags().Bool("rm", false, "If true, delete resources created in this command for attached containers.") cmd.Flags().Bool("rm", false, "If true, delete resources created in this command for attached containers.")
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().StringSlice("env", []string{}, "Environment variables to set in the container") cmd.Flags().StringSlice("env", []string{}, "Environment variables to set in the container")
cmd.Flags().Int("port", -1, "The port that this container exposes. If --expose is true, this is also the port used by the service that is created.") cmd.Flags().String("port", "", "The port that this container exposes. If --expose is true, this is also the port used by the service that is created.")
cmd.Flags().Int("hostport", -1, "The host port mapping for the container port. To demonstrate a single-machine container.") 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).") cmd.Flags().StringP("labels", "l", "", "Labels to apply to the pod(s).")
cmd.Flags().BoolP("stdin", "i", false, "Keep stdin open on the container(s) in the pod, even if nothing is attached.") cmd.Flags().BoolP("stdin", "i", false, "Keep stdin open on the container(s) in the pod, even if nothing is attached.")
@ -536,9 +536,9 @@ func generateService(f *cmdutil.Factory, cmd *cobra.Command, args []string, serv
} }
names := generator.ParamNames() names := generator.ParamNames()
port := cmdutil.GetFlagInt(cmd, "port") port := cmdutil.GetFlagString(cmd, "port")
if port < 1 { if len(port) == 0 {
return fmt.Errorf("--port must be a positive integer when exposing a service") return fmt.Errorf("--port must be set when exposing a service")
} }
params := map[string]interface{}{} params := map[string]interface{}{}

View File

@ -796,7 +796,7 @@ func updatePodPorts(params map[string]string, podSpec *api.PodSpec) (err error)
} }
// Don't include the port if it was not specified. // Don't include the port if it was not specified.
if port > 0 { if len(params["port"]) > 0 {
podSpec.Containers[0].Ports = []api.ContainerPort{ podSpec.Containers[0].Ports = []api.ContainerPort{
{ {
ContainerPort: int32(port), ContainerPort: int32(port),

View File

@ -39,7 +39,7 @@ func TestGenerate(t *testing.T) {
"image": "someimage", "image": "someimage",
"image-pull-policy": "Always", "image-pull-policy": "Always",
"replicas": "1", "replicas": "1",
"port": "-1", "port": "",
}, },
expected: &api.ReplicationController{ expected: &api.ReplicationController{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
@ -72,7 +72,7 @@ func TestGenerate(t *testing.T) {
"name": "foo", "name": "foo",
"image": "someimage", "image": "someimage",
"replicas": "1", "replicas": "1",
"port": "-1", "port": "",
"env": []string{"a=b", "c=d"}, "env": []string{"a=b", "c=d"},
}, },
expected: &api.ReplicationController{ expected: &api.ReplicationController{
@ -116,7 +116,7 @@ func TestGenerate(t *testing.T) {
"image": "someimage", "image": "someimage",
"image-pull-policy": "Never", "image-pull-policy": "Never",
"replicas": "1", "replicas": "1",
"port": "-1", "port": "",
"args": []string{"bar", "baz", "blah"}, "args": []string{"bar", "baz", "blah"},
}, },
expected: &api.ReplicationController{ expected: &api.ReplicationController{
@ -150,7 +150,7 @@ func TestGenerate(t *testing.T) {
"name": "foo", "name": "foo",
"image": "someimage", "image": "someimage",
"replicas": "1", "replicas": "1",
"port": "-1", "port": "",
"args": []string{"bar", "baz", "blah"}, "args": []string{"bar", "baz", "blah"},
"command": "true", "command": "true",
}, },
@ -410,7 +410,7 @@ func TestGeneratePod(t *testing.T) {
params: map[string]interface{}{ params: map[string]interface{}{
"name": "foo", "name": "foo",
"image": "someimage", "image": "someimage",
"port": "-1", "port": "",
}, },
expected: &api.Pod{ expected: &api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{