Merge pull request #54024 from pwittrock/resource-validation-deps

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

kubectl resource builder to use versioned list

Switch to using a versioned listed when return an object from the resource builder.

This is necessary to allow cli's built outside the kubernetes/kubernetes repo to vendor the resource builder logic without vendoring all of Kubernetes.

The following commands call the modified function.  (identified by changing the function name and recompiling)

- [x] `attach`
  - pkg/kubectl/cmd/attach.go:155: builder.Do().Object
  - passes to `AttachablePodForObject` which does not support Lists of any kind
- [x] `get`
  - pkg/kubectl/cmd/get.go:251: r.Object
  - [x] check if isList with `IsListType` -> `GetItemsPtr` -> check for presence of `Items` field
  - [x] pass to `ResourceVersion` -> `CommonAccessor` -> checks for `metav1.ListInterface` (which this PR introduces a compile time check to ensure v1.List implements this)
  - [x] pass to `ExtractList` -> checks if items implement `runtime.RawExtension`
- [x] `rolling_update`
  - pkg/kubectl/cmd/rollingupdate.go:207: request.Object
  - only accepts lists of length 1.  updated in PR to support both api.List and v1.List
- [x] `rollout_status`
  - pkg/kubectl/cmd/rollout/rollout_status.go:107: r.Object
  - passes to `ResourceVersion` -> `CommonAccessor` -> checks for `metav1.ListInterface`

```release-note
NONE
```

Closes kubernetes/kubectl#81
This commit is contained in:
Kubernetes Submit Queue 2017-10-20 21:31:10 -07:00 committed by GitHub
commit 9403fcc9c7
4 changed files with 30 additions and 15 deletions

View File

@ -212,14 +212,14 @@ func RunRollingUpdate(f cmdutil.Factory, out io.Writer, cmd *cobra.Command, args
var ok bool
// Handle filename input from stdin. The resource builder always returns an api.List
// when creating resource(s) from a stream.
if list, ok := obj.(*api.List); ok {
if list, ok := obj.(*v1.List); ok {
if len(list.Items) > 1 {
return cmdutil.UsageErrorf(cmd, "%s specifies multiple items", filename)
}
if len(list.Items) == 0 {
return cmdutil.UsageErrorf(cmd, "please make sure %s exists and is not empty", filename)
}
obj = list.Items[0]
obj = list.Items[0].Object
}
newRc, ok = obj.(*api.ReplicationController)
if !ok {

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",

View File

@ -900,11 +900,11 @@ func TestMultipleObject(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
expected := &api.List{
Items: []runtime.Object{
&pods.Items[0],
&pods.Items[1],
&svc.Items[0],
expected := &v1.List{
Items: []runtime.RawExtension{
{Object: &pods.Items[0]},
{Object: &pods.Items[1]},
{Object: &svc.Items[0]},
},
}
if !apiequality.Semantic.DeepDerivative(expected, obj) {
@ -1023,7 +1023,7 @@ func TestListObject(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
list, ok := obj.(*api.List)
list, ok := obj.(*v1.List)
if !ok {
t.Fatalf("unexpected object: %#v", obj)
}
@ -1057,7 +1057,7 @@ func TestListObjectWithDifferentVersions(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
list, ok := obj.(*api.List)
list, ok := obj.(*v1.List)
if !ok {
t.Fatalf("unexpected object: %#v", obj)
}

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.
@ -129,7 +129,7 @@ func (r *Result) Infos() ([]*Info, error) {
// 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) {
infos, err := r.Infos()
@ -160,12 +160,27 @@ func (r *Result) Object() (runtime.Object, error) {
if len(versions) == 1 {
version = versions.List()[0]
}
return &api.List{
return toV1List(objects, version), err
}
// 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 in the Items field
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: objects,
}, err
Items: raw,
}
}
// ResourceMapping returns a single meta.RESTMapping representing the