Skip to content

Commit

Permalink
#47568 - Added delegateError + Improve delegate Test Coverage (#318)
Browse files Browse the repository at this point in the history
* added delegate error / improve delegate coverage / add new mocks

* fixing ci lint

* addressing comments from @ericpromislow

* addressing comments from @tomleb

* fixing apistatus type casting assert
  • Loading branch information
gehrkefc authored Nov 14, 2024
1 parent 6ee8201 commit ea17719
Show file tree
Hide file tree
Showing 6 changed files with 1,426 additions and 15 deletions.
30 changes: 16 additions & 14 deletions pkg/ext/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,23 +239,25 @@ func InstallStore[T runtime.Object, TList runtime.Object](
apiGroup.VersionedResourcesStorageMap[gvk.Version] = make(map[string]rest.Storage)
}

delegate := &delegate[T, TList]{
scheme: s.scheme,

t: t,
tList: tList,
singularName: singularName,
gvk: gvk,
gvr: schema.GroupVersionResource{
Group: gvk.Group,
Version: gvk.Version,
Resource: resourceName,
del := &delegateError[T, TList]{
inner: &delegate[T, TList]{
scheme: s.scheme,

t: t,
tList: tList,
singularName: singularName,
gvk: gvk,
gvr: schema.GroupVersionResource{
Group: gvk.Group,
Version: gvk.Version,
Resource: resourceName,
},
authorizer: s.authorizer,
store: store,
},
authorizer: s.authorizer,
store: store,
}

apiGroup.VersionedResourcesStorageMap[gvk.Version][resourceName] = delegate
apiGroup.VersionedResourcesStorageMap[gvk.Version][resourceName] = del
s.apiGroups[gvk.Group] = apiGroup
return nil
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/ext/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ext

import (
"context"
"errors"
"fmt"
"sync"

Expand All @@ -16,6 +17,10 @@ import (
"k8s.io/apiserver/pkg/registry/rest"
)

var (
errMissingUserInfo = errors.New("missing user info")
)

// delegate is the bridge between k8s.io/apiserver's [rest.Storage] interface and
// our own Store interface we want developers to use
//
Expand Down Expand Up @@ -328,7 +333,7 @@ func (s *delegate[T, TList]) GetSingularName() string {
func (s *delegate[T, TList]) makeContext(parentCtx context.Context) (Context, error) {
userInfo, ok := request.UserFrom(parentCtx)
if !ok {
return Context{}, fmt.Errorf("missing user info")
return Context{}, errMissingUserInfo
}

ctx := Context{
Expand Down
110 changes: 110 additions & 0 deletions pkg/ext/delegate_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package ext

import (
"context"

"k8s.io/apimachinery/pkg/api/errors"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/apiserver/pkg/registry/rest"
)

// delegateError wraps an inner delegate and converts unknown errors.
type delegateError[T runtime.Object, TList runtime.Object] struct {
inner *delegate[T, TList]
}

func (d *delegateError[T, TList]) New() runtime.Object {
return d.inner.New()
}

func (d *delegateError[T, TList]) Destroy() {
d.inner.Destroy()
}

func (d *delegateError[T, TList]) NewList() runtime.Object {
return d.inner.NewList()
}

func (d *delegateError[T, TList]) List(parentCtx context.Context, internaloptions *metainternalversion.ListOptions) (runtime.Object, error) {
result, err := d.inner.List(parentCtx, internaloptions)
if err != nil {
return nil, convertError(err)
}
return result, nil
}

func (d *delegateError[T, TList]) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
result, err := d.inner.ConvertToTable(ctx, object, tableOptions)
if err != nil {
return nil, convertError(err)
}
return result, nil
}

func (d *delegateError[T, TList]) Get(parentCtx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
result, err := d.inner.Get(parentCtx, name, options)
if err != nil {
return nil, convertError(err)
}
return result, nil
}

func (d *delegateError[T, TList]) Delete(parentCtx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
result, completed, err := d.inner.Delete(parentCtx, name, deleteValidation, options)
if err != nil {
return nil, false, convertError(err)
}
return result, completed, nil
}

func (d *delegateError[T, TList]) Create(parentCtx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
result, err := d.inner.Create(parentCtx, obj, createValidation, options)
if err != nil {
return nil, convertError(err)
}
return result, nil
}

func (d *delegateError[T, TList]) Update(parentCtx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
result, created, err := d.inner.Update(parentCtx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)
if err != nil {
return nil, false, convertError(err)
}
return result, created, nil
}

func (d *delegateError[T, TList]) Watch(parentCtx context.Context, internaloptions *metainternalversion.ListOptions) (watch.Interface, error) {
result, err := d.inner.Watch(parentCtx, internaloptions)
if err != nil {
return nil, convertError(err)
}
return result, nil
}

func (d *delegateError[T, TList]) GroupVersionKind(groupVersion schema.GroupVersion) schema.GroupVersionKind {
return d.inner.GroupVersionKind(groupVersion)
}

func (d *delegateError[T, TList]) NamespaceScoped() bool {
return d.inner.NamespaceScoped()
}

func (d *delegateError[T, TList]) Kind() string {
return d.inner.Kind()
}

func (d *delegateError[T, TList]) GetSingularName() string {
return d.inner.GetSingularName()
}

func convertError(err error) error {
if _, ok := err.(errors.APIStatus); ok {
return err
}

return errors.NewInternalError(err)
}
Loading

0 comments on commit ea17719

Please sign in to comment.