Commit 6288c4e9 authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #44861 from sttts/sttts-dynamic-client-listoptions-fallback

Automatic merge from submit-queue apimachinery: NotRegisteredErr for known kinds not registered in target GV Fixes the fall back to core v1 for *Options in the parameter encoder of the dynamic client. The dynamic client uses NotRegisteredErr to fall back to core v1 if ListOptions is not known in the given GV. This commit fixes the case that ListOptions is known in some group, but not in the given one.
parents 33356a18 2ece9e4d
...@@ -24,16 +24,27 @@ import ( ...@@ -24,16 +24,27 @@ import (
) )
type notRegisteredErr struct { type notRegisteredErr struct {
gvk schema.GroupVersionKind gvk schema.GroupVersionKind
t reflect.Type target GroupVersioner
t reflect.Type
} }
// NewNotRegisteredErr is exposed for testing. func NewNotRegisteredErrForKind(gvk schema.GroupVersionKind) error {
func NewNotRegisteredErr(gvk schema.GroupVersionKind, t reflect.Type) error { return &notRegisteredErr{gvk: gvk}
return &notRegisteredErr{gvk: gvk, t: t} }
func NewNotRegisteredErrForType(t reflect.Type) error {
return &notRegisteredErr{t: t}
}
func NewNotRegisteredErrForTarget(t reflect.Type, target GroupVersioner) error {
return &notRegisteredErr{t: t, target: target}
} }
func (k *notRegisteredErr) Error() string { func (k *notRegisteredErr) Error() string {
if k.t != nil && k.target != nil {
return fmt.Sprintf("%v is not suitable for converting to %q", k.t, k.target)
}
if k.t != nil { if k.t != nil {
return fmt.Sprintf("no kind is registered for the type %v", k.t) return fmt.Sprintf("no kind is registered for the type %v", k.t)
} }
......
...@@ -243,7 +243,7 @@ func (s *Scheme) ObjectKinds(obj Object) ([]schema.GroupVersionKind, bool, error ...@@ -243,7 +243,7 @@ func (s *Scheme) ObjectKinds(obj Object) ([]schema.GroupVersionKind, bool, error
gvks, ok := s.typeToGVK[t] gvks, ok := s.typeToGVK[t]
if !ok { if !ok {
return nil, false, NewNotRegisteredErr(schema.GroupVersionKind{}, t) return nil, false, NewNotRegisteredErrForType(t)
} }
_, unversionedType := s.unversionedTypes[t] _, unversionedType := s.unversionedTypes[t]
...@@ -281,7 +281,7 @@ func (s *Scheme) New(kind schema.GroupVersionKind) (Object, error) { ...@@ -281,7 +281,7 @@ func (s *Scheme) New(kind schema.GroupVersionKind) (Object, error) {
if t, exists := s.unversionedKinds[kind.Kind]; exists { if t, exists := s.unversionedKinds[kind.Kind]; exists {
return reflect.New(t).Interface().(Object), nil return reflect.New(t).Interface().(Object), nil
} }
return nil, NewNotRegisteredErr(kind, nil) return nil, NewNotRegisteredErrForKind(kind)
} }
// AddGenericConversionFunc adds a function that accepts the ConversionFunc call pattern // AddGenericConversionFunc adds a function that accepts the ConversionFunc call pattern
...@@ -492,7 +492,7 @@ func (s *Scheme) convertToVersion(copy bool, in Object, target GroupVersioner) ( ...@@ -492,7 +492,7 @@ func (s *Scheme) convertToVersion(copy bool, in Object, target GroupVersioner) (
} }
kinds, ok := s.typeToGVK[t] kinds, ok := s.typeToGVK[t]
if !ok || len(kinds) == 0 { if !ok || len(kinds) == 0 {
return nil, NewNotRegisteredErr(schema.GroupVersionKind{}, t) return nil, NewNotRegisteredErrForType(t)
} }
gvk, ok := target.KindForGroupVersionKinds(kinds) gvk, ok := target.KindForGroupVersionKinds(kinds)
...@@ -506,8 +506,7 @@ func (s *Scheme) convertToVersion(copy bool, in Object, target GroupVersioner) ( ...@@ -506,8 +506,7 @@ func (s *Scheme) convertToVersion(copy bool, in Object, target GroupVersioner) (
return copyAndSetTargetKind(copy, s, in, unversionedKind) return copyAndSetTargetKind(copy, s, in, unversionedKind)
} }
// TODO: should this be a typed error? return nil, NewNotRegisteredErrForTarget(t, target)
return nil, fmt.Errorf("%v is not suitable for converting to %q", t, target)
} }
// target wants to use the existing type, set kind and return (no conversion necessary) // target wants to use the existing type, set kind and return (no conversion necessary)
......
...@@ -129,7 +129,7 @@ func TestDecode(t *testing.T) { ...@@ -129,7 +129,7 @@ func TestDecode(t *testing.T) {
{ {
data: []byte(`{"kind":"Test","apiVersion":"other/blah","value":1,"Other":"test"}`), data: []byte(`{"kind":"Test","apiVersion":"other/blah","value":1,"Other":"test"}`),
into: &testDecodable{}, into: &testDecodable{},
typer: &mockTyper{err: runtime.NewNotRegisteredErr(schema.GroupVersionKind{}, nil)}, typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind(schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}, expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
expectedObject: &testDecodable{ expectedObject: &testDecodable{
Other: "test", Other: "test",
......
...@@ -556,3 +556,11 @@ func TestPatch(t *testing.T) { ...@@ -556,3 +556,11 @@ func TestPatch(t *testing.T) {
} }
} }
} }
func TestVersionedParameterEncoderWithV1Fallback(t *testing.T) {
enc := VersionedParameterEncoderWithV1Fallback
_, err := enc.EncodeParameters(&metav1.ListOptions{}, schema.GroupVersion{Group: "foo.bar.com", Version: "v4"})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment