Remove dependency on internal types from pkg/kubectl/resource.

Updates the Result.Object function to take an argument.
This commit is contained in:
Phillip Wittrock
2017-10-17 18:46:38 -07:00
parent e3e2e24cc5
commit 72d672c3d5
12 changed files with 113 additions and 19 deletions

View File

@@ -22,10 +22,10 @@ go_library(
"//build/visible_to:pkg_kubectl_resource_CONSUMERS",
],
deps = [
"//pkg/api:go_default_library",
"//pkg/kubectl/validation:go_default_library",
"//vendor/golang.org/x/text/encoding/unicode:go_default_library",
"//vendor/golang.org/x/text/transform:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
@@ -62,6 +62,7 @@ go_test(
"//pkg/api:go_default_library",
"//pkg/api/testapi:go_default_library",
"//pkg/api/testing:go_default_library",
"//pkg/kubectl/internaldeps:go_default_library",
"//vendor/github.com/ghodss/yaml:go_default_library",
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",

View File

@@ -46,6 +46,7 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/testapi"
apitesting "k8s.io/kubernetes/pkg/api/testing"
"k8s.io/kubernetes/pkg/kubectl/internaldeps"
)
func stringBody(body string) io.ReadCloser {
@@ -893,7 +894,7 @@ func TestMultipleObject(t *testing.T) {
r, pods, svc := streamTestData()
obj, err := NewBuilder(testapi.Default.RESTMapper(), LegacyCategoryExpander, api.Scheme, fakeClient(), testapi.Default.Codec()).
NamespaceParam("test").Stream(r, "STDIN").Flatten().
Do().Object()
Do().Object(internaldeps.ToInternalList)
if err != nil {
t.Fatalf("unexpected error: %v", err)
@@ -946,7 +947,7 @@ func TestSingleItemImpliedObject(t *testing.T) {
NamespaceParam("test").DefaultNamespace().
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{"../../../examples/guestbook/legacy/redis-master-controller.yaml"}}).
Flatten().
Do().Object()
Do().Object(internaldeps.ToInternalList)
if err != nil {
t.Fatalf("unexpected error: %v", err)
@@ -966,7 +967,7 @@ func TestSingleItemImpliedObjectNoExtension(t *testing.T) {
NamespaceParam("test").DefaultNamespace().
FilenameParam(false, &FilenameOptions{Recursive: false, Filenames: []string{"../../../examples/pod"}}).
Flatten().
Do().Object()
Do().Object(internaldeps.ToInternalList)
if err != nil {
t.Fatalf("unexpected error: %v", err)
@@ -1017,7 +1018,7 @@ func TestListObject(t *testing.T) {
ResourceTypeOrNameArgs(true, "pods").
Flatten()
obj, err := b.Do().Object()
obj, err := b.Do().Object(internaldeps.ToInternalList)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -1050,7 +1051,7 @@ func TestListObjectWithDifferentVersions(t *testing.T) {
NamespaceParam("test").
ResourceTypeOrNameArgs(true, "pods,services").
Flatten().
Do().Object()
Do().Object(internaldeps.ToInternalList)
if err != nil {
t.Fatalf("unexpected error: %v", err)

View File

@@ -20,13 +20,13 @@ import (
"fmt"
"reflect"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/kubernetes/pkg/api"
)
// ErrMatchFunc can be used to filter errors that may not be true failures.
@@ -125,13 +125,37 @@ func (r *Result) Infos() ([]*Info, error) {
return infos, err
}
// ToList is a function that converts a slice of Objects into a single
// List Object. It allows callers to control whether an internal
// or external List is returned.
type ToList func([]runtime.Object, string) runtime.Object
// Compile time check to enforce that list implements the necessary interface
var _ metav1.ListInterface = &v1.List{}
var _ metav1.ListMetaAccessor = &v1.List{}
// ToV1List takes a slice of Objects + their version, and returns
// a v1.List Object containing the objects as Items
func ToV1List(objects []runtime.Object, version string) runtime.Object {
raw := []runtime.RawExtension{}
for _, o := range objects {
raw = append(raw, runtime.RawExtension{Object: o})
}
return &v1.List{
ListMeta: metav1.ListMeta{
ResourceVersion: version,
},
Items: raw,
}
}
// Object returns a single object representing the output of a single visit to all
// found resources. If the Builder was a singular context (expected to return a
// single resource by user input) and only a single resource was found, the resource
// will be returned as is. Otherwise, the returned resources will be part of an
// api.List. The ResourceVersion of the api.List will be set only if it is identical
// v1.List. The ResourceVersion of the v1.List will be set only if it is identical
// across all infos returned.
func (r *Result) Object() (runtime.Object, error) {
func (r *Result) Object(toList ToList) (runtime.Object, error) {
infos, err := r.Infos()
if err != nil {
return nil, err
@@ -160,12 +184,11 @@ func (r *Result) Object() (runtime.Object, error) {
if len(versions) == 1 {
version = versions.List()[0]
}
return &api.List{
ListMeta: metav1.ListMeta{
ResourceVersion: version,
},
Items: objects,
}, err
if toList == nil {
toList = ToV1List
}
return toList(objects, version), err
}
// ResourceMapping returns a single meta.RESTMapping representing the