Skip to content

Commit

Permalink
Merge pull request kubernetes#11415 from nikhiljindal/exposeError
Browse files Browse the repository at this point in the history
Fixing the error message when kubectl expose is used without port
  • Loading branch information
bgrant0607 committed Jul 17, 2015
2 parents 5f34630 + c849730 commit b2dafda
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/man/man1/kubectl-expose.1
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ re\-use the labels from the resource it exposes.

.PP
\fB\-\-port\fP=\-1
The port that the service should serve on. Required.
The port that the service should serve on. Copied from the resource being exposed, if unspecified

.PP
\fB\-\-protocol\fP="TCP"
Expand Down
4 changes: 2 additions & 2 deletions docs/user-guide/kubectl/kubectl_expose.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream
-o, --output="": Output format. One of: json|yaml|template|templatefile|wide.
--output-version="": Output the formatted object with the given version (default api-version).
--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.
--port=-1: The port that the service should serve on. Required.
--port=-1: The port that the service should serve on. Copied from the resource being exposed, if unspecified
--protocol="TCP": The network protocol for the service to be created. Default is 'tcp'.
--public-ip="": Name of a public IP address to set for the service. The service will be assigned this IP in addition to its generated service IP.
--selector="": A label selector to use for this service. If empty (the default) infer the selector from the replication controller.
Expand Down Expand Up @@ -105,7 +105,7 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream
### SEE ALSO
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager

###### Auto generated by spf13/cobra at 2015-07-16 21:22:36.08263026 +0000 UTC
###### Auto generated by spf13/cobra at 2015-07-17 01:17:57.020108348 +0000 UTC


<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
Expand Down
10 changes: 5 additions & 5 deletions pkg/kubectl/cmd/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewCmdExposeService(f *cmdutil.Factory, out io.Writer) *cobra.Command {
cmdutil.AddPrinterFlags(cmd)
cmd.Flags().String("generator", "service/v2", "The name of the API generator to use. There are 2 generators: 'service/v1' and 'service/v2'. The only difference between them is that service port in v1 is named 'default', while it is left unnamed in v2. Default is 'service/v2'.")
cmd.Flags().String("protocol", "TCP", "The network protocol for the service to be created. Default is 'tcp'.")
cmd.Flags().Int("port", -1, "The port that the service should serve on. Required.")
cmd.Flags().Int("port", -1, "The port that the service should serve on. Copied from the resource being exposed, if unspecified")
cmd.MarkFlagRequired("port")
cmd.Flags().String("type", "", "Type for this service: ClusterIP, NodePort, or LoadBalancer. Default is 'ClusterIP' unless --create-external-load-balancer is specified.")
cmd.Flags().Bool("create-external-load-balancer", false, "If true, create an external load balancer for this service (trumped by --type). Implementation is cloud provider dependent. Default is 'false'.")
Expand Down Expand Up @@ -126,7 +126,7 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
if len(s) == 0 {
s, err := f.PodSelectorForObject(inputObject)
if err != nil {
return err
return cmdutil.UsageError(cmd, fmt.Sprintf("couldn't find selectors via --selector flag or introspection: %s", err))
}
params["selector"] = s
}
Expand All @@ -140,15 +140,15 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
if cmdutil.GetFlagInt(cmd, "port") < 0 && !noPorts {
ports, err := f.PortsForObject(inputObject)
if err != nil {
return err
return cmdutil.UsageError(cmd, fmt.Sprintf("couldn't find port via --port flag or introspection: %s", err))
}
switch len(ports) {
case 0:
return cmdutil.UsageError(cmd, "couldn't find a suitable port via --port flag or introspection")
return cmdutil.UsageError(cmd, "couldn't find port via --port flag or introspection")
case 1:
params["port"] = ports[0]
default:
return cmdutil.UsageError(cmd, "more than one port to choose from, please explicitly specify a port using the --port flag.")
return cmdutil.UsageError(cmd, fmt.Sprintf("multiple ports to choose from: %v, please explicitly specify a port using the --port flag.", ports))
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/kubectl/cmd/util/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
}
return kubectl.MakeLabels(t.Spec.Selector), nil
default:
kind, err := meta.NewAccessor().Kind(object)
_, kind, err := api.Scheme.ObjectVersionAndKind(object)
if err != nil {
return "", err
}
return "", fmt.Errorf("it is not possible to get a pod selector from %s", kind)
return "", fmt.Errorf("cannot extract pod selector from %s", kind)
}
},
PortsForObject: func(object runtime.Object) ([]string, error) {
Expand All @@ -178,11 +178,13 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
case *api.Pod:
return getPorts(t.Spec), nil
default:
kind, err := meta.NewAccessor().Kind(object)
// TODO: support extracting ports from service:
// https://github.com/GoogleCloudPlatform/kubernetes/issues/11392
_, kind, err := api.Scheme.ObjectVersionAndKind(object)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("it is not possible to get ports from %s", kind)
return nil, fmt.Errorf("cannot extract ports from %s", kind)
}
},
LabelsForObject: func(object runtime.Object) (map[string]string, error) {
Expand Down

0 comments on commit b2dafda

Please sign in to comment.