mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Storage: Add mode-specific DeleteCollection implementations (#86065)
* Add DeleteCollection methods to modes 1 and 4 * Add DeleteCollection implementations for modes 2 and 3
This commit is contained in:
@@ -69,6 +69,7 @@ type DualWriter struct {
|
|||||||
var errDualWriterCreaterMissing = errors.New("legacy storage rest.Creater is missing")
|
var errDualWriterCreaterMissing = errors.New("legacy storage rest.Creater is missing")
|
||||||
var errDualWriterListerMissing = errors.New("legacy storage rest.Lister is missing")
|
var errDualWriterListerMissing = errors.New("legacy storage rest.Lister is missing")
|
||||||
var errDualWriterDeleterMissing = errors.New("legacy storage rest.GracefulDeleter is missing")
|
var errDualWriterDeleterMissing = errors.New("legacy storage rest.GracefulDeleter is missing")
|
||||||
|
var errDualWriterCollectionDeleterMissing = errors.New("legacy storage rest.CollectionDeleter is missing")
|
||||||
|
|
||||||
type DualWriterMode int
|
type DualWriterMode int
|
||||||
|
|
||||||
|
|||||||
@@ -54,3 +54,13 @@ func (d *DualWriterMode1) Delete(ctx context.Context, name string, deleteValidat
|
|||||||
|
|
||||||
return legacy.Delete(ctx, name, deleteValidation, options)
|
return legacy.Delete(ctx, name, deleteValidation, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteCollection overrides the behavior of the generic DualWriter and deletes only from LegacyStorage.
|
||||||
|
func (d *DualWriterMode1) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
|
||||||
|
legacy, ok := d.Legacy.(rest.CollectionDeleter)
|
||||||
|
if !ok {
|
||||||
|
return nil, errDualWriterCollectionDeleterMissing
|
||||||
|
}
|
||||||
|
|
||||||
|
return legacy.DeleteCollection(ctx, deleteValidation, options, listOptions)
|
||||||
|
}
|
||||||
|
|||||||
@@ -44,4 +44,15 @@ func TestMode1(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Delete"))
|
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Delete"))
|
||||||
assert.Equal(t, 0, sSpy.Counts("Storage.Delete"))
|
assert.Equal(t, 0, sSpy.Counts("Storage.Delete"))
|
||||||
|
|
||||||
|
// DeleteCollection: it should use the Legacy DeleteCollection implementation
|
||||||
|
_, err = dw.DeleteCollection(
|
||||||
|
context.Background(),
|
||||||
|
func(context.Context, runtime.Object) error { return nil },
|
||||||
|
&metav1.DeleteOptions{},
|
||||||
|
&metainternalversion.ListOptions{},
|
||||||
|
)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.DeleteCollection"))
|
||||||
|
assert.Equal(t, 0, sSpy.Counts("Storage.DeleteCollection"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,25 +124,25 @@ func (d *DualWriterMode2) List(ctx context.Context, options *metainternalversion
|
|||||||
return ll, nil
|
return ll, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func enrichObject(orig, copy runtime.Object) (runtime.Object, error) {
|
// DeleteCollection overrides the behavior of the generic DualWriter and deletes from both LegacyStorage and Storage.
|
||||||
accessorC, err := meta.Accessor(copy)
|
func (d *DualWriterMode2) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
|
||||||
|
legacy, ok := d.Legacy.(rest.CollectionDeleter)
|
||||||
|
if !ok {
|
||||||
|
return nil, errDualWriterCollectionDeleterMissing
|
||||||
|
}
|
||||||
|
|
||||||
|
// #TODO: figure out how to handle partial deletions
|
||||||
|
deleted, err := legacy.DeleteCollection(ctx, deleteValidation, options, listOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
klog.FromContext(ctx).Error(err, "failed to delete collection successfully from legacy storage", "deletedObjects", deleted)
|
||||||
}
|
}
|
||||||
accessorO, err := meta.Accessor(orig)
|
|
||||||
|
res, err := d.Storage.DeleteCollection(ctx, deleteValidation, options, listOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
klog.FromContext(ctx).Error(err, "failed to delete collection successfully from Storage", "deletedObjects", deleted)
|
||||||
}
|
}
|
||||||
|
|
||||||
accessorC.SetLabels(accessorO.GetLabels())
|
return res, err
|
||||||
|
|
||||||
ac := accessorC.GetAnnotations()
|
|
||||||
for k, v := range accessorO.GetAnnotations() {
|
|
||||||
ac[k] = v
|
|
||||||
}
|
|
||||||
accessorC.SetAnnotations(ac)
|
|
||||||
|
|
||||||
return copy, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DualWriterMode2) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
|
func (d *DualWriterMode2) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
|
||||||
@@ -168,3 +168,24 @@ func (d *DualWriterMode2) Delete(ctx context.Context, name string, deleteValidat
|
|||||||
|
|
||||||
return deletedLS, async, err
|
return deletedLS, async, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func enrichObject(orig, copy runtime.Object) (runtime.Object, error) {
|
||||||
|
accessorC, err := meta.Accessor(copy)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
accessorO, err := meta.Accessor(orig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
accessorC.SetLabels(accessorO.GetLabels())
|
||||||
|
|
||||||
|
ac := accessorC.GetAnnotations()
|
||||||
|
for k, v := range accessorO.GetAnnotations() {
|
||||||
|
ac[k] = v
|
||||||
|
}
|
||||||
|
accessorC.SetAnnotations(ac)
|
||||||
|
|
||||||
|
return copy, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -68,4 +68,15 @@ func TestMode2(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Delete"))
|
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Delete"))
|
||||||
assert.Equal(t, 1, sSpy.Counts("Storage.Delete"))
|
assert.Equal(t, 1, sSpy.Counts("Storage.Delete"))
|
||||||
|
|
||||||
|
// DeleteCollection: it should delete from both LegacyStorage and Storage
|
||||||
|
_, err = dw.DeleteCollection(
|
||||||
|
context.Background(),
|
||||||
|
func(context.Context, runtime.Object) error { return nil },
|
||||||
|
&metav1.DeleteOptions{},
|
||||||
|
&metainternalversion.ListOptions{},
|
||||||
|
)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.DeleteCollection"))
|
||||||
|
assert.Equal(t, 1, sSpy.Counts("Storage.DeleteCollection"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apiserver/pkg/registry/rest"
|
"k8s.io/apiserver/pkg/registry/rest"
|
||||||
@@ -66,3 +67,23 @@ func (d *DualWriterMode3) Delete(ctx context.Context, name string, deleteValidat
|
|||||||
|
|
||||||
return deleted, async, err
|
return deleted, async, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteCollection overrides the behavior of the generic DualWriter and deletes from both LegacyStorage and Storage.
|
||||||
|
func (d *DualWriterMode3) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
|
||||||
|
legacy, ok := d.Legacy.(rest.CollectionDeleter)
|
||||||
|
if !ok {
|
||||||
|
return nil, errDualWriterCollectionDeleterMissing
|
||||||
|
}
|
||||||
|
|
||||||
|
// #TODO: figure out how to handle partial deletions
|
||||||
|
deleted, err := d.Storage.DeleteCollection(ctx, deleteValidation, options, listOptions)
|
||||||
|
if err != nil {
|
||||||
|
klog.FromContext(ctx).Error(err, "failed to delete collection successfully from Storage", "deletedObjects", deleted)
|
||||||
|
}
|
||||||
|
|
||||||
|
if deleted, err := legacy.DeleteCollection(ctx, deleteValidation, options, listOptions); err != nil {
|
||||||
|
klog.FromContext(ctx).Error(err, "failed to delete collection successfully from LegacyStorage", "deletedObjects", deleted)
|
||||||
|
}
|
||||||
|
|
||||||
|
return deleted, err
|
||||||
|
}
|
||||||
|
|||||||
@@ -42,4 +42,15 @@ func TestMode3(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Delete"))
|
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Delete"))
|
||||||
assert.Equal(t, 1, sSpy.Counts("Storage.Delete"))
|
assert.Equal(t, 1, sSpy.Counts("Storage.Delete"))
|
||||||
|
|
||||||
|
// DeleteCollection: it should delete from both LegacyStorage and Storage
|
||||||
|
_, err = dw.DeleteCollection(
|
||||||
|
context.Background(),
|
||||||
|
func(context.Context, runtime.Object) error { return nil },
|
||||||
|
&metav1.DeleteOptions{},
|
||||||
|
&metainternalversion.ListOptions{},
|
||||||
|
)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.DeleteCollection"))
|
||||||
|
assert.Equal(t, 1, sSpy.Counts("Storage.DeleteCollection"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package rest
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apiserver/pkg/registry/rest"
|
"k8s.io/apiserver/pkg/registry/rest"
|
||||||
@@ -33,3 +34,8 @@ func (d *DualWriterMode4) Get(ctx context.Context, name string, options *metav1.
|
|||||||
func (d *DualWriterMode4) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
|
func (d *DualWriterMode4) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
|
||||||
return d.Storage.Delete(ctx, name, deleteValidation, options)
|
return d.Storage.Delete(ctx, name, deleteValidation, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteCollection overrides the behavior of the generic DualWriter and deletes only from Storage.
|
||||||
|
func (d *DualWriterMode4) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
|
||||||
|
return d.Storage.DeleteCollection(ctx, deleteValidation, options, listOptions)
|
||||||
|
}
|
||||||
|
|||||||
@@ -42,4 +42,15 @@ func TestMode4(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Delete"))
|
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Delete"))
|
||||||
assert.Equal(t, 1, sSpy.Counts("Storage.Delete"))
|
assert.Equal(t, 1, sSpy.Counts("Storage.Delete"))
|
||||||
|
|
||||||
|
// DeleteCollection: it should use the Storage DeleteCollection implementation
|
||||||
|
_, err = dw.DeleteCollection(
|
||||||
|
context.Background(),
|
||||||
|
func(context.Context, runtime.Object) error { return nil },
|
||||||
|
&metav1.DeleteOptions{},
|
||||||
|
&metainternalversion.ListOptions{},
|
||||||
|
)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.DeleteCollection"))
|
||||||
|
assert.Equal(t, 1, sSpy.Counts("Storage.DeleteCollection"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,6 +101,12 @@ func (c *spyStorageClient) Delete(ctx context.Context, name string, deleteValida
|
|||||||
return nil, false, nil
|
return nil, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *spyStorageClient) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
|
||||||
|
c.spy.record("Storage.DeleteCollection")
|
||||||
|
klog.Info("method: Storage.DeleteCollection")
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
// LegacyStorage Spy
|
// LegacyStorage Spy
|
||||||
|
|
||||||
type LegacyStorageSpyClient interface {
|
type LegacyStorageSpyClient interface {
|
||||||
@@ -184,6 +190,12 @@ func (c *spyLegacyStorageClient) Delete(ctx context.Context, name string, delete
|
|||||||
return nil, false, nil
|
return nil, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *spyLegacyStorageClient) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
|
||||||
|
c.spy.record("LegacyStorage.DeleteCollection")
|
||||||
|
klog.Info("method: LegacyStorage.DeleteCollection")
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
type dummyList struct {
|
type dummyList struct {
|
||||||
metav1.TypeMeta `json:",inline"`
|
metav1.TypeMeta `json:",inline"`
|
||||||
metav1.ListMeta `json:"metadata,omitempty"`
|
metav1.ListMeta `json:"metadata,omitempty"`
|
||||||
|
|||||||
Reference in New Issue
Block a user