expose: Re-use labels in case none was specified
This commit is contained in:
parent
5010b2dde0
commit
ac652d16e6
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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{
|
||||
{
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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},
|
||||
|
Loading…
Reference in New Issue
Block a user