Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apimachinery: NotRegisteredErr for known kinds not registered in target GV #44861

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions staging/src/k8s.io/apimachinery/pkg/runtime/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,27 @@ import (
)

type notRegisteredErr struct {
gvk schema.GroupVersionKind
t reflect.Type
gvk schema.GroupVersionKind
target GroupVersioner
t reflect.Type
}

// NewNotRegisteredErr is exposed for testing.
func NewNotRegisteredErr(gvk schema.GroupVersionKind, t reflect.Type) error {
return &notRegisteredErr{gvk: gvk, t: t}
func NewNotRegisteredErrForKind(gvk schema.GroupVersionKind) error {
return &notRegisteredErr{gvk: gvk}
}

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 {
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 {
return fmt.Sprintf("no kind is registered for the type %v", k.t)
}
Expand Down
9 changes: 4 additions & 5 deletions staging/src/k8s.io/apimachinery/pkg/runtime/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (s *Scheme) ObjectKinds(obj Object) ([]schema.GroupVersionKind, bool, error

gvks, ok := s.typeToGVK[t]
if !ok {
return nil, false, NewNotRegisteredErr(schema.GroupVersionKind{}, t)
return nil, false, NewNotRegisteredErrForType(t)
}
_, unversionedType := s.unversionedTypes[t]

Expand Down Expand Up @@ -281,7 +281,7 @@ func (s *Scheme) New(kind schema.GroupVersionKind) (Object, error) {
if t, exists := s.unversionedKinds[kind.Kind]; exists {
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
Expand Down Expand Up @@ -492,7 +492,7 @@ func (s *Scheme) convertToVersion(copy bool, in Object, target GroupVersioner) (
}
kinds, ok := s.typeToGVK[t]
if !ok || len(kinds) == 0 {
return nil, NewNotRegisteredErr(schema.GroupVersionKind{}, t)
return nil, NewNotRegisteredErrForType(t)
}

gvk, ok := target.KindForGroupVersionKinds(kinds)
Expand All @@ -506,8 +506,7 @@ func (s *Scheme) convertToVersion(copy bool, in Object, target GroupVersioner) (
return copyAndSetTargetKind(copy, s, in, unversionedKind)
}

// TODO: should this be a typed error?
return nil, fmt.Errorf("%v is not suitable for converting to %q", t, target)
return nil, NewNotRegisteredErrForTarget(t, target)
}

// target wants to use the existing type, set kind and return (no conversion necessary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestDecode(t *testing.T) {
{
data: []byte(`{"kind":"Test","apiVersion":"other/blah","value":1,"Other":"test"}`),
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"},
expectedObject: &testDecodable{
Other: "test",
Expand Down
8 changes: 8 additions & 0 deletions staging/src/k8s.io/client-go/dynamic/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}