From 59d5298314276f7ffa1052832779c6f02f497ffb Mon Sep 17 00:00:00 2001 From: David Eads Date: Thu, 2 Aug 2018 08:36:22 -0400 Subject: [PATCH] switch expose to externals --- pkg/kubectl/cmd/expose.go | 10 +- pkg/kubectl/cmd/expose_test.go | 171 +++++++++++++++++---------------- 2 files changed, 89 insertions(+), 92 deletions(-) diff --git a/pkg/kubectl/cmd/expose.go b/pkg/kubectl/cmd/expose.go index f0e309eb74f..13d4655e7b1 100644 --- a/pkg/kubectl/cmd/expose.go +++ b/pkg/kubectl/cmd/expose.go @@ -29,7 +29,6 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/validation" "k8s.io/client-go/dynamic" - "k8s.io/kubernetes/pkg/api/legacyscheme" "k8s.io/kubernetes/pkg/kubectl" "k8s.io/kubernetes/pkg/kubectl/cmd/templates" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" @@ -92,7 +91,6 @@ type ExposeServiceOptions struct { Generators func(string) map[string]kubectl.Generator CanBeExposed polymorphichelpers.CanBeExposedFunc - ClientForMapping func(*meta.RESTMapping) (resource.RESTClient, error) MapBasedSelectorForObject func(runtime.Object) (string, error) PortsForObject polymorphichelpers.PortsForObjectFunc ProtocolsForObject func(runtime.Object) (map[string]string, error) @@ -191,7 +189,6 @@ func (o *ExposeServiceOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) e o.Generators = cmdutil.GeneratorFn o.Builder = f.NewBuilder() o.CanBeExposed = polymorphichelpers.CanBeExposedFn - o.ClientForMapping = f.ClientForMapping o.MapBasedSelectorForObject = polymorphichelpers.MapBasedSelectorForObjectFn o.ProtocolsForObject = polymorphichelpers.ProtocolsForObjectFn o.PortsForObject = polymorphichelpers.PortsForObjectFn @@ -211,7 +208,7 @@ func (o *ExposeServiceOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) e func (o *ExposeServiceOptions) RunExpose(cmd *cobra.Command, args []string) error { r := o.Builder. - WithScheme(legacyscheme.Scheme). + WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...). ContinueOnError(). NamespaceParam(o.Namespace).DefaultNamespace(). FilenameParam(o.EnforceNamespace, &o.FilenameOptions). @@ -314,8 +311,7 @@ func (o *ExposeServiceOptions) RunExpose(cmd *cobra.Command, args []string) erro } if inline := cmdutil.GetFlagString(cmd, "overrides"); len(inline) > 0 { - codec := runtime.NewCodec(cmdutil.InternalVersionJSONEncoder(), cmdutil.InternalVersionDecoder()) - object, err = cmdutil.Merge(codec, object, inline) + object, err = cmdutil.Merge(scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...), object, inline) if err != nil { return err } @@ -333,7 +329,7 @@ func (o *ExposeServiceOptions) RunExpose(cmd *cobra.Command, args []string) erro } asUnstructured := &unstructured.Unstructured{} - if err := legacyscheme.Scheme.Convert(object, asUnstructured, nil); err != nil { + if err := scheme.Scheme.Convert(object, asUnstructured, nil); err != nil { return err } gvks, _, err := unstructuredscheme.NewUnstructuredObjectTyper().ObjectKinds(asUnstructured) diff --git a/pkg/kubectl/cmd/expose_test.go b/pkg/kubectl/cmd/expose_test.go index 22c20c00142..be208a63f19 100644 --- a/pkg/kubectl/cmd/expose_test.go +++ b/pkg/kubectl/cmd/expose_test.go @@ -22,6 +22,7 @@ import ( "strings" "testing" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -60,19 +61,19 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/test/services/baz", "POST": "/namespaces/test/services", }, - input: &api.Service{ + input: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"}, - Spec: api.ServiceSpec{ + Spec: corev1.ServiceSpec{ Selector: map[string]string{"app": "go"}, }, }, flags: map[string]string{"protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { - Protocol: api.ProtocolUDP, + Protocol: corev1.ProtocolUDP, Port: 14, TargetPort: intstr.FromInt(14), }, @@ -91,19 +92,19 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/test/services/baz", "POST": "/namespaces/test/services", }, - input: &api.Service{ + input: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"}, - Spec: api.ServiceSpec{ + Spec: corev1.ServiceSpec{ Selector: map[string]string{"app": "go"}, }, }, flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { - Protocol: api.ProtocolUDP, + Protocol: corev1.ProtocolUDP, Port: 14, TargetPort: intstr.FromInt(14), }, @@ -122,20 +123,20 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/default/services/mayor", "POST": "/namespaces/default/services", }, - input: &api.Service{ + input: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "mayor", Namespace: "default", ResourceVersion: "12"}, - Spec: api.ServiceSpec{ + Spec: corev1.ServiceSpec{ Selector: map[string]string{"run": "this"}, }, }, // No --name flag specified below. Service will use the rc's name passed via the 'default-name' parameter flags: map[string]string{"selector": "run=this", "port": "80", "labels": "runas=amayor"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "mayor", Namespace: "", Labels: map[string]string{"runas": "amayor"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { - Protocol: api.ProtocolTCP, + Protocol: corev1.ProtocolTCP, Port: 80, TargetPort: intstr.FromInt(80), }, @@ -154,25 +155,25 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/test/services/baz", "POST": "/namespaces/test/services", }, - input: &api.Service{ + input: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"}, - Spec: api.ServiceSpec{ + Spec: corev1.ServiceSpec{ Selector: map[string]string{"app": "go"}, }, }, flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "type": "LoadBalancer", "dry-run": "true"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { - Protocol: api.ProtocolUDP, + Protocol: corev1.ProtocolUDP, Port: 14, TargetPort: intstr.FromInt(14), }, }, Selector: map[string]string{"func": "stream"}, - Type: api.ServiceTypeLoadBalancer, + Type: corev1.ServiceTypeLoadBalancer, }, }, status: 200, @@ -185,26 +186,26 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/test/services/baz", "POST": "/namespaces/test/services", }, - input: &api.Service{ + input: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"}, - Spec: api.ServiceSpec{ + Spec: corev1.ServiceSpec{ Selector: map[string]string{"app": "go"}, }, }, flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "type": "LoadBalancer", "session-affinity": "ClientIP", "dry-run": "true"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { - Protocol: api.ProtocolUDP, + Protocol: corev1.ProtocolUDP, Port: 14, TargetPort: intstr.FromInt(14), }, }, Selector: map[string]string{"func": "stream"}, - Type: api.ServiceTypeLoadBalancer, - SessionAffinity: api.ServiceAffinityClientIP, + Type: corev1.ServiceTypeLoadBalancer, + SessionAffinity: corev1.ServiceAffinityClientIP, }, }, status: 200, @@ -217,19 +218,19 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/test/services/baz", "POST": "/namespaces/test/services", }, - input: &api.Service{ + input: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"}, - Spec: api.ServiceSpec{ + Spec: corev1.ServiceSpec{ Selector: map[string]string{"app": "go"}, }, }, flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "cluster-ip": "10.10.10.10", "dry-run": "true"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { - Protocol: api.ProtocolUDP, + Protocol: corev1.ProtocolUDP, Port: 14, TargetPort: intstr.FromInt(14), }, @@ -249,25 +250,25 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/test/services/baz", "POST": "/namespaces/test/services", }, - input: &api.Service{ + input: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"}, - Spec: api.ServiceSpec{ + Spec: corev1.ServiceSpec{ Selector: map[string]string{"app": "go"}, }, }, flags: map[string]string{"selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "cluster-ip": "None", "dry-run": "true"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { - Protocol: api.ProtocolUDP, + Protocol: corev1.ProtocolUDP, Port: 14, TargetPort: intstr.FromInt(14), }, }, Selector: map[string]string{"func": "stream"}, - ClusterIP: api.ClusterIPNone, + ClusterIP: corev1.ClusterIPNone, }, }, expected: "service/foo exposed", @@ -281,19 +282,19 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/test/services/baz", "POST": "/namespaces/test/services", }, - input: &api.Service{ + input: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"}, - Spec: api.ServiceSpec{ + Spec: corev1.ServiceSpec{ Selector: map[string]string{"app": "go"}, }, }, flags: map[string]string{"selector": "func=stream", "name": "foo", "labels": "svc=test", "cluster-ip": "None", "dry-run": "true"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "test"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{}, + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{}, Selector: map[string]string{"func": "stream"}, - ClusterIP: api.ClusterIPNone, + ClusterIP: corev1.ClusterIPNone, }, }, expected: "service/foo exposed", @@ -307,19 +308,19 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/test/services/redis-master", "POST": "/namespaces/test/services", }, - input: &api.Service{ + input: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "redis-master", Namespace: "test", ResourceVersion: "12"}, - Spec: api.ServiceSpec{ + Spec: corev1.ServiceSpec{ Selector: map[string]string{"app": "go"}, }, }, flags: map[string]string{"filename": "../../../test/e2e/testing-manifests/guestbook/redis-master-service.yaml", "selector": "func=stream", "protocol": "UDP", "port": "14", "name": "foo", "labels": "svc=test", "dry-run": "true"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo", Labels: map[string]string{"svc": "test"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { - Protocol: api.ProtocolUDP, + Protocol: corev1.ProtocolUDP, Port: 14, TargetPort: intstr.FromInt(14), }, @@ -337,16 +338,16 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/test/pods/a-name-that-is-toooo-big-for-a-service-because-it-can-only-handle-63-characters", "POST": "/namespaces/test/services", }, - input: &api.Pod{ + input: &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test", ResourceVersion: "12"}, }, flags: map[string]string{"selector": "svc=frompod", "port": "90", "labels": "svc=frompod", "generator": "service/v2"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "a-name-that-is-toooo-big-for-a-service-because-it-can-only-handle-63-characters", Namespace: "", Labels: map[string]string{"svc": "frompod"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { - Protocol: api.ProtocolTCP, + Protocol: corev1.ProtocolTCP, Port: 90, TargetPort: intstr.FromInt(90), }, @@ -365,17 +366,17 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/test/services/foo", "POST": "/namespaces/test/services", }, - input: &api.Service{ + input: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "multiport"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { - Protocol: api.ProtocolTCP, + Protocol: corev1.ProtocolTCP, Port: 80, TargetPort: intstr.FromInt(80), }, { - Protocol: api.ProtocolTCP, + Protocol: corev1.ProtocolTCP, Port: 443, TargetPort: intstr.FromInt(443), }, @@ -383,19 +384,19 @@ func TestRunExposeService(t *testing.T) { }, }, flags: map[string]string{"selector": "svc=fromfoo", "generator": "service/v2", "name": "fromfoo", "dry-run": "true"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "fromfoo", Namespace: "", Labels: map[string]string{"svc": "multiport"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { Name: "port-1", - Protocol: api.ProtocolTCP, + Protocol: corev1.ProtocolTCP, Port: 80, TargetPort: intstr.FromInt(80), }, { Name: "port-2", - Protocol: api.ProtocolTCP, + Protocol: corev1.ProtocolTCP, Port: 443, TargetPort: intstr.FromInt(443), }, @@ -413,22 +414,22 @@ func TestRunExposeService(t *testing.T) { "GET": "/namespaces/test/services/foo", "POST": "/namespaces/test/services", }, - input: &api.Service{ + input: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "", Labels: map[string]string{"svc": "multiport"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { - Protocol: api.ProtocolTCP, + Protocol: corev1.ProtocolTCP, Port: 80, TargetPort: intstr.FromInt(80), }, { - Protocol: api.ProtocolUDP, + Protocol: corev1.ProtocolUDP, Port: 8080, TargetPort: intstr.FromInt(8080), }, { - Protocol: api.ProtocolUDP, + Protocol: corev1.ProtocolUDP, Port: 8081, TargetPort: intstr.FromInt(8081), }, @@ -436,25 +437,25 @@ func TestRunExposeService(t *testing.T) { }, }, flags: map[string]string{"selector": "svc=fromfoo", "generator": "service/v2", "name": "fromfoo", "dry-run": "true"}, - output: &api.Service{ + output: &corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: "fromfoo", Namespace: "", Labels: map[string]string{"svc": "multiport"}}, - Spec: api.ServiceSpec{ - Ports: []api.ServicePort{ + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ { Name: "port-1", - Protocol: api.ProtocolTCP, + Protocol: corev1.ProtocolTCP, Port: 80, TargetPort: intstr.FromInt(80), }, { Name: "port-2", - Protocol: api.ProtocolUDP, + Protocol: corev1.ProtocolUDP, Port: 8080, TargetPort: intstr.FromInt(8080), }, { Name: "port-3", - Protocol: api.ProtocolUDP, + Protocol: corev1.ProtocolUDP, Port: 8081, TargetPort: intstr.FromInt(8081), },