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

#47568 - Added delegateError + Improve delegate Test Coverage #318

Merged
merged 5 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions pkg/ext/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,22 @@ 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,
delegate := &delegateError[T, TList]{
gehrkefc marked this conversation as resolved.
Show resolved Hide resolved
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
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]) convertError(err error) error {
if _, ok := err.(errors.APIStatus); ok {
return err
}

return errors.NewInternalError(err)
}

gehrkefc marked this conversation as resolved.
Show resolved Hide resolved
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, d.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, d.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, d.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, d.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, d.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, d.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, d.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()
}
60 changes: 60 additions & 0 deletions pkg/ext/delegate_error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ext

import (
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestDelegateError_convertError(t *testing.T) {
tests := []struct {
name string
input error
output error
}{
{
name: "api status error",
input: &apierrors.StatusError{
ErrStatus: metav1.Status{
Code: http.StatusNotFound,
Reason: metav1.StatusReasonNotFound,
},
},
output: &apierrors.StatusError{
ErrStatus: metav1.Status{
Code: http.StatusNotFound,
Reason: metav1.StatusReasonNotFound,
},
},
},
{
name: "generic error",
input: assert.AnError,
output: &apierrors.StatusError{ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusInternalServerError,
Reason: metav1.StatusReasonInternalError,
Details: &metav1.StatusDetails{
Causes: []metav1.StatusCause{{Message: assert.AnError.Error()}},
},
Message: fmt.Sprintf("Internal error occurred: %v", assert.AnError),
}},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
delegateError := delegateError[*TestType, *TestTypeList]{
inner: &delegate[*TestType, *TestTypeList]{},
}

output := delegateError.convertError(tt.input)
assert.Equal(t, tt.output, output)
})
}

}
Loading