From 30958f892de50494af354afccc37f913e4fa2135 Mon Sep 17 00:00:00 2001 From: kargakis Date: Wed, 13 May 2015 14:14:44 +0200 Subject: [PATCH] expose: Use resource builder Plus: * Rename flag --service-name to --name * Some refactoring --- contrib/completions/bash/kubectl | 2 +- docs/kubectl_expose.md | 10 +++--- docs/man/man1/kubectl-expose.1 | 12 +++---- hack/test-cmd.sh | 4 +-- pkg/kubectl/cmd/expose.go | 54 +++++++++++++++++--------------- pkg/kubectl/cmd/expose_test.go | 2 +- 6 files changed, 43 insertions(+), 41 deletions(-) diff --git a/contrib/completions/bash/kubectl b/contrib/completions/bash/kubectl index 6cd0c9ea4c9..7afc86b9a9d 100644 --- a/contrib/completions/bash/kubectl +++ b/contrib/completions/bash/kubectl @@ -615,6 +615,7 @@ _kubectl_expose() flags+=("-h") flags+=("--labels=") two_word_flags+=("-l") + flags+=("--name=") flags+=("--no-headers") flags+=("--output=") two_word_flags+=("-o") @@ -624,7 +625,6 @@ _kubectl_expose() flags+=("--protocol=") flags+=("--public-ip=") flags+=("--selector=") - flags+=("--service-name=") flags+=("--target-port=") flags+=("--template=") two_word_flags+=("-t") diff --git a/docs/kubectl_expose.md b/docs/kubectl_expose.md index a880ff27a3b..82c0a4d426f 100644 --- a/docs/kubectl_expose.md +++ b/docs/kubectl_expose.md @@ -12,7 +12,7 @@ selector for a new Service on the specified port. If no labels are specified, th re-use the labels from the resource it exposes. ``` -kubectl expose RESOURCE NAME --port=port [--protocol=TCP|UDP] [--target-port=number-or-name] [--service-name=name] [--public-ip=ip] [--create-external-load-balancer=bool] +kubectl expose RESOURCE NAME --port=port [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [--public-ip=ip] [--create-external-load-balancer=bool] ``` ### Examples @@ -22,10 +22,10 @@ kubectl expose RESOURCE NAME --port=port [--protocol=TCP|UDP] [--target-port=num $ kubectl expose rc nginx --port=80 --target-port=8000 // Creates a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx-https" -$ kubectl expose service nginx --port=443 --target-port=8443 --service-name=nginx-https +$ kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https // Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'. -$ kubectl expose rc streamer --port=4100 --protocol=udp --service-name=video-stream +$ kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream ``` ### Options @@ -37,6 +37,7 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --service-name=video-str --generator="service/v1": The name of the API generator to use. Default is 'service/v1'. -h, --help=false: help for expose -l, --labels="": Labels to apply to the service created by this call. + --name="": The name for the newly created service. --no-headers=false: When using the default output, don't print headers. -o, --output="": Output format. One of: json|yaml|template|templatefile. --output-version="": Output the formatted object with the given version (default api-version). @@ -45,7 +46,6 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --service-name=video-str --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. - --service-name="": The name for the newly created service. --target-port="": Name or number for the port on the container that the service should direct traffic to. Optional. -t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview] ``` @@ -82,6 +82,6 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --service-name=video-str ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-05-15 00:05:04.553214179 +0000 UTC +###### Auto generated by spf13/cobra at 2015-05-19 14:12:47.467953048 +0000 UTC [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/kubectl_expose.md?pixel)]() diff --git a/docs/man/man1/kubectl-expose.1 b/docs/man/man1/kubectl-expose.1 index c5d9f2c4c27..a24642dd8cb 100644 --- a/docs/man/man1/kubectl-expose.1 +++ b/docs/man/man1/kubectl-expose.1 @@ -46,6 +46,10 @@ re\-use the labels from the resource it exposes. \fB\-l\fP, \fB\-\-labels\fP="" Labels to apply to the service created by this call. +.PP +\fB\-\-name\fP="" + The name for the newly created service. + .PP \fB\-\-no\-headers\fP=false When using the default output, don't print headers. @@ -78,10 +82,6 @@ re\-use the labels from the resource it exposes. \fB\-\-selector\fP="" A label selector to use for this service. If empty (the default) infer the selector from the replication controller. -.PP -\fB\-\-service\-name\fP="" - The name for the newly created service. - .PP \fB\-\-target\-port\fP="" Name or number for the port on the container that the service should direct traffic to. Optional. @@ -199,10 +199,10 @@ re\-use the labels from the resource it exposes. $ kubectl expose rc nginx \-\-port=80 \-\-target\-port=8000 // Creates a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx\-https" -$ kubectl expose service nginx \-\-port=443 \-\-target\-port=8443 \-\-service\-name=nginx\-https +$ kubectl expose service nginx \-\-port=443 \-\-target\-port=8443 \-\-name=nginx\-https // Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video\-stream'. -$ kubectl expose rc streamer \-\-port=4100 \-\-protocol=udp \-\-service\-name=video\-stream +$ kubectl expose rc streamer \-\-port=4100 \-\-protocol=udp \-\-name=video\-stream .fi .RE diff --git a/hack/test-cmd.sh b/hack/test-cmd.sh index 65a6b931acb..e3ff86968c0 100755 --- a/hack/test-cmd.sh +++ b/hack/test-cmd.sh @@ -531,12 +531,12 @@ __EOF__ # Post-condition: service exists kube::test::get_object_assert 'service frontend' "{{$port_field}}" '80' # Command - kubectl expose service frontend --port=443 --service-name=frontend-2 "${kube_flags[@]}" + kubectl expose service frontend --port=443 --name=frontend-2 "${kube_flags[@]}" # Post-condition: service exists kube::test::get_object_assert 'service frontend-2' "{{$port_field}}" '443' # Command kubectl create -f examples/limitrange/valid-pod.json "${kube_flags[@]}" - kubectl expose pod valid-pod --port=444 --service-name=frontend-3 "${kube_flags[@]}" + kubectl expose pod valid-pod --port=444 --name=frontend-3 "${kube_flags[@]}" # Post-condition: service exists kube::test::get_object_assert 'service frontend-3' "{{$port_field}}" '444' # Cleanup services diff --git a/pkg/kubectl/cmd/expose.go b/pkg/kubectl/cmd/expose.go index 346e10d18ac..5da1604b913 100644 --- a/pkg/kubectl/cmd/expose.go +++ b/pkg/kubectl/cmd/expose.go @@ -20,7 +20,6 @@ import ( "fmt" "io" - "github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource" @@ -39,15 +38,15 @@ re-use the labels from the resource it exposes.` $ kubectl expose rc nginx --port=80 --target-port=8000 // Creates a second service based on the above service, exposing the container port 8443 as port 443 with the name "nginx-https" -$ kubectl expose service nginx --port=443 --target-port=8443 --service-name=nginx-https +$ kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https // Create a service for a replicated streaming application on port 4100 balancing UDP traffic and named 'video-stream'. -$ kubectl expose rc streamer --port=4100 --protocol=udp --service-name=video-stream` +$ kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream` ) func NewCmdExposeService(f *cmdutil.Factory, out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "expose RESOURCE NAME --port=port [--protocol=TCP|UDP] [--target-port=number-or-name] [--service-name=name] [--public-ip=ip] [--create-external-load-balancer=bool]", + Use: "expose RESOURCE NAME --port=port [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [--public-ip=ip] [--create-external-load-balancer=bool]", Short: "Take a replicated application and expose it as Kubernetes Service", Long: expose_long, Example: expose_example, @@ -69,40 +68,43 @@ func NewCmdExposeService(f *cmdutil.Factory, out io.Writer) *cobra.Command { cmd.Flags().String("target-port", "", "Name or number for the port on the container that the service should direct traffic to. Optional.") cmd.Flags().String("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.") 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("service-name", "", "The name for the newly created service.") + cmd.Flags().String("name", "", "The name for the newly created service.") return cmd } func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error { - var name, res string - switch l := len(args); { - case l == 2: - res, name = args[0], args[1] - default: - return cmdutil.UsageError(cmd, "the type and name of a resource to expose are required arguments") - } - namespace, err := f.DefaultNamespace() if err != nil { return err } - // Get the input object - var mapping *meta.RESTMapping mapper, typer := f.Object() - v, k, err := mapper.VersionAndKindForResource(res) + r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()). + ContinueOnError(). + NamespaceParam(namespace).DefaultNamespace(). + ResourceTypeOrNameArgs(false, args...). + Flatten(). + Do() + err = r.Err() if err != nil { return err } - mapping, err = mapper.RESTMapping(k, v) + mapping, err := r.ResourceMapping() if err != nil { return err } + infos, err := r.Infos() + if err != nil { + return err + } + info := infos[0] + + // Get the input object client, err := f.RESTClient(mapping) if err != nil { return err } - inputObject, err := resource.NewHelper(client, mapping).Get(namespace, name) + inputObject, err := resource.NewHelper(client, mapping).Get(info.Namespace, info.Name) if err != nil { return err } @@ -115,10 +117,9 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str } names := generator.ParamNames() params := kubectl.MakeParams(cmd, names) - if len(cmdutil.GetFlagString(cmd, "service-name")) == 0 { - params["name"] = name - } else { - params["name"] = cmdutil.GetFlagString(cmd, "service-name") + params["name"] = cmdutil.GetFlagString(cmd, "name") + if len(params["name"]) == 0 { + params["name"] = info.Name } if s, found := params["selector"]; !found || len(s) == 0 || cmdutil.GetFlagInt(cmd, "port") < 1 { if len(s) == 0 { @@ -140,13 +141,14 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str if err != nil { return err } - if len(ports) == 0 { + switch len(ports) { + case 0: return cmdutil.UsageError(cmd, "couldn't find a suitable port via --port flag or introspection") - } - if len(ports) > 1 { + 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.") } - params["port"] = ports[0] } } if cmdutil.GetFlagBool(cmd, "create-external-load-balancer") { diff --git a/pkg/kubectl/cmd/expose_test.go b/pkg/kubectl/cmd/expose_test.go index 5c1a5b60abb..94669880769 100644 --- a/pkg/kubectl/cmd/expose_test.go +++ b/pkg/kubectl/cmd/expose_test.go @@ -46,7 +46,7 @@ func TestRunExposeService(t *testing.T) { Selector: map[string]string{"app": "go"}, }, }, - flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "service-name": "foo", "labels": "svc=test"}, + flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test"}, output: &api.Service{ ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "12", Labels: map[string]string{"svc": "test"}}, TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"},