Implement Destroy() method for all registries

This commit is contained in:
Wojciech Tyczyński
2022-04-05 12:26:22 +02:00
parent 0527a0dd45
commit 80060a502c
47 changed files with 356 additions and 5 deletions

View File

@@ -195,6 +195,12 @@ func (r *StatusREST) New() runtime.Object {
return &apiextensions.CustomResourceDefinition{}
}
// Destroy cleans up resources on shutdown.
func (r *StatusREST) Destroy() {
// Given that underlying store is shared with REST,
// we don't destroy it here explicitly.
}
// Get retrieves the object from the storage. It is required to support Patch.
func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
return r.store.Get(ctx, name, options)

View File

@@ -462,6 +462,9 @@ func (storage *SimpleRESTStorage) NewList() runtime.Object {
return &genericapitesting.SimpleList{}
}
func (storage *SimpleRESTStorage) Destroy() {
}
func (storage *SimpleRESTStorage) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
storage.checkContext(ctx)
storage.created = obj.(*genericapitesting.Simple)
@@ -547,6 +550,9 @@ func (s *ConnecterRESTStorage) New() runtime.Object {
return &genericapitesting.Simple{}
}
func (s *ConnecterRESTStorage) Destroy() {
}
func (s *ConnecterRESTStorage) Connect(ctx context.Context, id string, options runtime.Object, responder rest.Responder) (http.Handler, error) {
s.receivedConnectOptions = options
s.receivedID = id
@@ -668,6 +674,9 @@ func (storage *SimpleTypedStorage) New() runtime.Object {
return storage.baseType
}
func (storage *SimpleTypedStorage) Destroy() {
}
func (storage *SimpleTypedStorage) Get(ctx context.Context, id string, options *metav1.GetOptions) (runtime.Object, error) {
storage.checkContext(ctx)
return storage.item.DeepCopyObject(), storage.errors["get"]
@@ -810,6 +819,9 @@ func (UnimplementedRESTStorage) New() runtime.Object {
return &genericapitesting.Simple{}
}
func (UnimplementedRESTStorage) Destroy() {
}
// TestUnimplementedRESTStorage ensures that if a rest.Storage does not implement a given
// method, that it is literally not registered with the server. In the past,
// we registered everything, and returned method not supported if it didn't support
@@ -4322,6 +4334,9 @@ func (storage *SimpleXGSubresourceRESTStorage) New() runtime.Object {
return &genericapitesting.SimpleXGSubresource{}
}
func (storage *SimpleXGSubresourceRESTStorage) Destroy() {
}
func (storage *SimpleXGSubresourceRESTStorage) Get(ctx context.Context, id string, options *metav1.GetOptions) (runtime.Object, error) {
return storage.item.DeepCopyObject(), nil
}

View File

@@ -56,6 +56,11 @@ type Storage interface {
// New returns an empty object that can be used with Create and Update after request data has been put into it.
// This object must be a pointer type for use with Codec.DecodeInto([]byte, runtime.Object)
New() runtime.Object
// Destroy cleans up its resources on shutdown.
// Destroy has to be implemented in thread-safe way and be prepared
// for being called more than once.
Destroy()
}
// Scoper indicates what scope the resource is at. It must be specified.

View File

@@ -110,6 +110,9 @@ func (r removedInStorage) New() runtime.Object {
return removedInObj{major: r.major, minor: r.minor}
}
func (r removedInStorage) Destroy() {
}
type neverRemovedObj struct {
}

View File

@@ -92,9 +92,7 @@ type APIGroupInfo struct {
func (a *APIGroupInfo) destroyStorage() {
for _, stores := range a.VersionedResourcesStorageMap {
for _, store := range stores {
// TODO(wojtek-t): Uncomment once all storage support it.
klog.Errorf("Destroying storage: %v", store)
// store.Destroy()
store.Destroy()
}
}
}

View File

@@ -544,6 +544,9 @@ func (p *testGetterStorage) New() runtime.Object {
}
}
func (p *testGetterStorage) Destroy() {
}
func (p *testGetterStorage) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
return nil, nil
}
@@ -565,6 +568,9 @@ func (p *testNoVerbsStorage) New() runtime.Object {
}
}
func (p *testNoVerbsStorage) Destroy() {
}
func fakeVersion() version.Info {
return version.Info{
Major: "42",

View File

@@ -147,6 +147,12 @@ func (r *StatusREST) New() runtime.Object {
return &apiregistration.APIService{}
}
// Destroy cleans up resources on shutdown.
func (r *StatusREST) Destroy() {
// Given that underlying store is shared with REST,
// we don't destroy it here explicitly.
}
// Get retrieves the object from the storage. It is required to support Patch.
func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
return r.store.Get(ctx, name, options)

View File

@@ -20,7 +20,6 @@ import (
"fmt"
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/apiserver/pkg/registry/rest"
)
// REST implements a RESTStorage for API services against etcd
@@ -31,7 +30,7 @@ type REST struct {
// RESTInPeace is just a simple function that panics on error.
// Otherwise returns the given storage object. It is meant to be
// a wrapper for wardle registries.
func RESTInPeace(storage rest.StandardStorage, err error) rest.StandardStorage {
func RESTInPeace(storage *REST, err error) *REST {
if err != nil {
err = fmt.Errorf("unable to create REST storage for a resource due to %v, will die", err)
panic(err)