From ac652d16e63134ba59aa543642b8c1e391faa685 Mon Sep 17 00:00:00 2001 From: kargakis Date: Wed, 13 May 2015 10:52:25 +0200 Subject: [PATCH] expose: Re-use labels in case none was specified --- docs/kubectl_expose.md | 5 +-- docs/man/man1/kubectl-expose.1 | 3 +- pkg/kubectl/cmd/expose.go | 10 +++++- pkg/kubectl/cmd/expose_test.go | 6 ++-- pkg/kubectl/cmd/util/factory.go | 5 +++ pkg/kubectl/cmd/util/factory_test.go | 53 ++++++++++++++++++++++++++++ pkg/kubectl/service.go | 1 + 7 files changed, 77 insertions(+), 6 deletions(-) diff --git a/docs/kubectl_expose.md b/docs/kubectl_expose.md index 5afb28e7041..0bca5541a46 100644 --- a/docs/kubectl_expose.md +++ b/docs/kubectl_expose.md @@ -8,7 +8,8 @@ Take a replicated application and expose it as Kubernetes Service Take a replicated application and expose it as Kubernetes Service. Looks up a replication controller or service by name and uses the selector for that resource as the -selector for a new Service on the specified port. +selector for a new Service on the specified port. If no labels are specified, the new service will +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] @@ -81,4 +82,4 @@ $ 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-08 20:26:40.49295951 +0000 UTC +###### Auto generated by spf13/cobra at 2015-05-14 09:05:28.99900833 +0000 UTC diff --git a/docs/man/man1/kubectl-expose.1 b/docs/man/man1/kubectl-expose.1 index 3ea4b73ad90..c5d9f2c4c27 100644 --- a/docs/man/man1/kubectl-expose.1 +++ b/docs/man/man1/kubectl-expose.1 @@ -17,7 +17,8 @@ Take a replicated application and expose it as Kubernetes Service. .PP Looks up a replication controller or service by name and uses the selector for that resource as the -selector for a new Service on the specified port. +selector for a new Service on the specified port. If no labels are specified, the new service will +re\-use the labels from the resource it exposes. .SH OPTIONS diff --git a/pkg/kubectl/cmd/expose.go b/pkg/kubectl/cmd/expose.go index 1514b81d6ee..346e10d18ac 100644 --- a/pkg/kubectl/cmd/expose.go +++ b/pkg/kubectl/cmd/expose.go @@ -32,7 +32,8 @@ const ( expose_long = `Take a replicated application and expose it as Kubernetes Service. Looks up a replication controller or service by name and uses the selector for that resource as the -selector for a new Service on the specified port.` +selector for a new Service on the specified port. If no labels are specified, the new service will +re-use the labels from the resource it exposes.` expose_example = `// Creates a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000. $ kubectl expose rc nginx --port=80 --target-port=8000 @@ -151,6 +152,13 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str if cmdutil.GetFlagBool(cmd, "create-external-load-balancer") { params["create-external-load-balancer"] = "true" } + if len(params["labels"]) == 0 { + labels, err := f.LabelsForObject(inputObject) + if err != nil { + return err + } + params["labels"] = kubectl.MakeLabels(labels) + } err = kubectl.ValidateParams(names, params) if err != nil { return err diff --git a/pkg/kubectl/cmd/expose_test.go b/pkg/kubectl/cmd/expose_test.go index 703b42f7c04..5c1a5b60abb 100644 --- a/pkg/kubectl/cmd/expose_test.go +++ b/pkg/kubectl/cmd/expose_test.go @@ -41,13 +41,15 @@ func TestRunExposeService(t *testing.T) { args: []string{"service", "baz"}, input: &api.Service{ ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"}, + TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, Spec: api.ServiceSpec{ Selector: map[string]string{"app": "go"}, }, }, - flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "service-name": "foo"}, + flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "service-name": "foo", "labels": "svc=test"}, output: &api.Service{ - ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "12"}, + ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", ResourceVersion: "12", Labels: map[string]string{"svc": "test"}}, + TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, Spec: api.ServiceSpec{ Ports: []api.ServicePort{ { diff --git a/pkg/kubectl/cmd/util/factory.go b/pkg/kubectl/cmd/util/factory.go index 067f5825c3b..dc89d7625fc 100644 --- a/pkg/kubectl/cmd/util/factory.go +++ b/pkg/kubectl/cmd/util/factory.go @@ -73,6 +73,8 @@ type Factory struct { PodSelectorForObject func(object runtime.Object) (string, error) // PortsForObject returns the ports associated with the provided object PortsForObject func(object runtime.Object) ([]string, error) + // LabelsForObject returns the labels associated with the provided object + LabelsForObject func(object runtime.Object) (map[string]string, error) // Returns a schema that can validate objects stored on disk. Validator func() (validation.Schema, error) // Returns the default namespace to use in cases where no other namespace is specified @@ -182,6 +184,9 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory { return nil, fmt.Errorf("it is not possible to get ports from %s", kind) } }, + LabelsForObject: func(object runtime.Object) (map[string]string, error) { + return meta.NewAccessor().Labels(object) + }, Resizer: func(mapping *meta.RESTMapping) (kubectl.Resizer, error) { client, err := clients.ClientForVersion(mapping.APIVersion) if err != nil { diff --git a/pkg/kubectl/cmd/util/factory_test.go b/pkg/kubectl/cmd/util/factory_test.go index 808477be765..06569027fa9 100644 --- a/pkg/kubectl/cmd/util/factory_test.go +++ b/pkg/kubectl/cmd/util/factory_test.go @@ -23,6 +23,8 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd" clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api" + "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl" + "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" ) func TestNewFactoryDefaultFlagBindings(t *testing.T) { @@ -100,3 +102,54 @@ func TestPortsForObject(t *testing.T) { } } } + +func TestLabelsForObject(t *testing.T) { + f := NewFactory(nil) + + tests := []struct { + name string + object runtime.Object + expected string + err error + }{ + { + name: "successful re-use of labels", + object: &api.Service{ + ObjectMeta: api.ObjectMeta{Name: "baz", Namespace: "test", Labels: map[string]string{"svc": "test"}}, + TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, + }, + expected: "svc=test", + err: nil, + }, + { + name: "empty labels", + object: &api.Service{ + ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test", Labels: map[string]string{}}, + TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, + }, + expected: "", + err: nil, + }, + { + name: "nil labels", + object: &api.Service{ + ObjectMeta: api.ObjectMeta{Name: "zen", Namespace: "test", Labels: nil}, + TypeMeta: api.TypeMeta{Kind: "Service", APIVersion: "v1beta3"}, + }, + expected: "", + err: nil, + }, + } + + for _, test := range tests { + gotLabels, err := f.LabelsForObject(test.object) + if err != test.err { + t.Fatalf("%s: Error mismatch: Expected %v, got %v", test.name, test.err, err) + } + got := kubectl.MakeLabels(gotLabels) + if test.expected != got { + t.Fatalf("%s: Labels mismatch! Expected %s, got %s", test.name, test.expected, got) + } + + } +} diff --git a/pkg/kubectl/service.go b/pkg/kubectl/service.go index 43baf56cfad..c0a0ba59892 100644 --- a/pkg/kubectl/service.go +++ b/pkg/kubectl/service.go @@ -32,6 +32,7 @@ func (ServiceGenerator) ParamNames() []GeneratorParam { {"name", true}, {"selector", true}, {"port", true}, + {"labels", false}, {"public-ip", false}, {"create-external-load-balancer", false}, {"protocol", false},