mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-06 07:57:35 +00:00
expose: Re-use labels in case none was specified
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user