move more CanBeExposed from factory_client_access
This commit is contained in:
@@ -27,7 +27,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructuredscheme"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructuredscheme"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
||||||
"k8s.io/apimachinery/pkg/util/validation"
|
"k8s.io/apimachinery/pkg/util/validation"
|
||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
||||||
@@ -92,7 +91,7 @@ type ExposeServiceOptions struct {
|
|||||||
EnforceNamespace bool
|
EnforceNamespace bool
|
||||||
|
|
||||||
Generators func(string) map[string]kubectl.Generator
|
Generators func(string) map[string]kubectl.Generator
|
||||||
CanBeExposed func(kind schema.GroupKind) error
|
CanBeExposed polymorphichelpers.CanBeExposedFunc
|
||||||
ClientForMapping func(*meta.RESTMapping) (resource.RESTClient, error)
|
ClientForMapping func(*meta.RESTMapping) (resource.RESTClient, error)
|
||||||
MapBasedSelectorForObject func(runtime.Object) (string, error)
|
MapBasedSelectorForObject func(runtime.Object) (string, error)
|
||||||
PortsForObject polymorphichelpers.PortsForObjectFunc
|
PortsForObject polymorphichelpers.PortsForObjectFunc
|
||||||
@@ -191,7 +190,7 @@ func (o *ExposeServiceOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) e
|
|||||||
|
|
||||||
o.Generators = f.Generators
|
o.Generators = f.Generators
|
||||||
o.Builder = f.NewBuilder()
|
o.Builder = f.NewBuilder()
|
||||||
o.CanBeExposed = f.CanBeExposed
|
o.CanBeExposed = polymorphichelpers.CanBeExposedFn
|
||||||
o.ClientForMapping = f.ClientForMapping
|
o.ClientForMapping = f.ClientForMapping
|
||||||
o.MapBasedSelectorForObject = f.MapBasedSelectorForObject
|
o.MapBasedSelectorForObject = f.MapBasedSelectorForObject
|
||||||
o.PortsForObject = polymorphichelpers.PortsForObjectFn
|
o.PortsForObject = polymorphichelpers.PortsForObjectFn
|
||||||
|
@@ -16,7 +16,6 @@ go_library(
|
|||||||
visibility = ["//build/visible_to:pkg_kubectl_cmd_util_CONSUMERS"],
|
visibility = ["//build/visible_to:pkg_kubectl_cmd_util_CONSUMERS"],
|
||||||
deps = [
|
deps = [
|
||||||
"//pkg/api/legacyscheme:go_default_library",
|
"//pkg/api/legacyscheme:go_default_library",
|
||||||
"//pkg/apis/apps:go_default_library",
|
|
||||||
"//pkg/apis/core:go_default_library",
|
"//pkg/apis/core:go_default_library",
|
||||||
"//pkg/apis/extensions:go_default_library",
|
"//pkg/apis/extensions:go_default_library",
|
||||||
"//pkg/client/clientset_generated/internalclientset:go_default_library",
|
"//pkg/client/clientset_generated/internalclientset:go_default_library",
|
||||||
|
@@ -107,8 +107,6 @@ type ClientAccessFactory interface {
|
|||||||
DefaultNamespace() (string, bool, error)
|
DefaultNamespace() (string, bool, error)
|
||||||
// Generators returns the generators for the provided command
|
// Generators returns the generators for the provided command
|
||||||
Generators(cmdName string) map[string]kubectl.Generator
|
Generators(cmdName string) map[string]kubectl.Generator
|
||||||
// Check whether the kind of resources could be exposed
|
|
||||||
CanBeExposed(kind schema.GroupKind) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ObjectMappingFactory holds the second level of factory methods. These functions depend upon ClientAccessFactory methods.
|
// ObjectMappingFactory holds the second level of factory methods. These functions depend upon ClientAccessFactory methods.
|
||||||
|
@@ -47,7 +47,6 @@ import (
|
|||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
||||||
"k8s.io/kubernetes/pkg/apis/apps"
|
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||||
@@ -459,17 +458,6 @@ func (f *ring0Factory) Generators(cmdName string) map[string]kubectl.Generator {
|
|||||||
return DefaultGenerators(cmdName)
|
return DefaultGenerators(cmdName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *ring0Factory) CanBeExposed(kind schema.GroupKind) error {
|
|
||||||
switch kind {
|
|
||||||
case api.Kind("ReplicationController"), api.Kind("Service"), api.Kind("Pod"),
|
|
||||||
extensions.Kind("Deployment"), apps.Kind("Deployment"), extensions.Kind("ReplicaSet"), apps.Kind("ReplicaSet"):
|
|
||||||
// nothing to do here
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("cannot expose a %s", kind)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// this method exists to help us find the points still relying on internal types.
|
// this method exists to help us find the points still relying on internal types.
|
||||||
func InternalVersionDecoder() runtime.Decoder {
|
func InternalVersionDecoder() runtime.Decoder {
|
||||||
return legacyscheme.Codecs.UniversalDecoder()
|
return legacyscheme.Codecs.UniversalDecoder()
|
||||||
|
@@ -21,7 +21,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
"k8s.io/kubernetes/pkg/kubectl"
|
"k8s.io/kubernetes/pkg/kubectl"
|
||||||
@@ -66,33 +65,6 @@ func TestProtocolsForObject(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCanBeExposed(t *testing.T) {
|
|
||||||
factory := NewFactory(genericclioptions.NewTestConfigFlags())
|
|
||||||
tests := []struct {
|
|
||||||
kind schema.GroupKind
|
|
||||||
expectErr bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
kind: api.Kind("ReplicationController"),
|
|
||||||
expectErr: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
kind: api.Kind("Node"),
|
|
||||||
expectErr: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
err := factory.CanBeExposed(test.kind)
|
|
||||||
if test.expectErr && err == nil {
|
|
||||||
t.Error("unexpected non-error")
|
|
||||||
}
|
|
||||||
if !test.expectErr && err != nil {
|
|
||||||
t.Errorf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMakePortsString(t *testing.T) {
|
func TestMakePortsString(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
ports []api.ServicePort
|
ports []api.ServicePort
|
||||||
|
@@ -5,6 +5,7 @@ go_library(
|
|||||||
srcs = [
|
srcs = [
|
||||||
"attachablepodforobject.go",
|
"attachablepodforobject.go",
|
||||||
"canbeautoscaled.go",
|
"canbeautoscaled.go",
|
||||||
|
"canbeexposed.go",
|
||||||
"helpers.go",
|
"helpers.go",
|
||||||
"historyviewer.go",
|
"historyviewer.go",
|
||||||
"interface.go",
|
"interface.go",
|
||||||
@@ -48,6 +49,7 @@ go_library(
|
|||||||
go_test(
|
go_test(
|
||||||
name = "go_default_test",
|
name = "go_default_test",
|
||||||
srcs = [
|
srcs = [
|
||||||
|
"canbeexposed_test.go",
|
||||||
"helpers_test.go",
|
"helpers_test.go",
|
||||||
"logsforobject_test.go",
|
"logsforobject_test.go",
|
||||||
"portsforobject_test.go",
|
"portsforobject_test.go",
|
||||||
|
38
pkg/kubectl/polymorphichelpers/canbeexposed.go
Normal file
38
pkg/kubectl/polymorphichelpers/canbeexposed.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2018 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package polymorphichelpers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/kubernetes/pkg/apis/apps"
|
||||||
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Check whether the kind of resources could be exposed
|
||||||
|
func canBeExposed(kind schema.GroupKind) error {
|
||||||
|
switch kind {
|
||||||
|
case api.Kind("ReplicationController"), api.Kind("Service"), api.Kind("Pod"),
|
||||||
|
extensions.Kind("Deployment"), apps.Kind("Deployment"), extensions.Kind("ReplicaSet"), apps.Kind("ReplicaSet"):
|
||||||
|
// nothing to do here
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("cannot expose a %s", kind)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
50
pkg/kubectl/polymorphichelpers/canbeexposed_test.go
Normal file
50
pkg/kubectl/polymorphichelpers/canbeexposed_test.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2018 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package polymorphichelpers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCanBeExposed(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
kind schema.GroupKind
|
||||||
|
expectErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
kind: api.Kind("ReplicationController"),
|
||||||
|
expectErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: api.Kind("Node"),
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
err := canBeExposed(test.kind)
|
||||||
|
if test.expectErr && err == nil {
|
||||||
|
t.Error("unexpected non-error")
|
||||||
|
}
|
||||||
|
if !test.expectErr && err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -71,3 +71,9 @@ type CanBeAutoscaledFunc func(kind schema.GroupKind) error
|
|||||||
|
|
||||||
// CanBeAutoscaledFn gives a way to easily override the function for unit testing if needed
|
// CanBeAutoscaledFn gives a way to easily override the function for unit testing if needed
|
||||||
var CanBeAutoscaledFn CanBeAutoscaledFunc = canBeAutoscaled
|
var CanBeAutoscaledFn CanBeAutoscaledFunc = canBeAutoscaled
|
||||||
|
|
||||||
|
// CanBeExposedFunc is a function type that can tell you whether a given GroupKind is capable of being exposed
|
||||||
|
type CanBeExposedFunc func(kind schema.GroupKind) error
|
||||||
|
|
||||||
|
// CanBeExposedFn gives a way to easily override the function for unit testing if needed
|
||||||
|
var CanBeExposedFn CanBeExposedFunc = canBeExposed
|
||||||
|
Reference in New Issue
Block a user