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:
Arati R
2024-04-16 17:05:31 +02:00
committed by GitHub
parent d3fee607e2
commit 79eab65f94
10 changed files with 129 additions and 14 deletions

View File

@@ -69,6 +69,7 @@ type DualWriter struct {
var errDualWriterCreaterMissing = errors.New("legacy storage rest.Creater is missing")
var errDualWriterListerMissing = errors.New("legacy storage rest.Lister 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

View File

@@ -54,3 +54,13 @@ func (d *DualWriterMode1) Delete(ctx context.Context, name string, deleteValidat
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)
}

View File

@@ -44,4 +44,15 @@ func TestMode1(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.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"))
}

View File

@@ -124,25 +124,25 @@ func (d *DualWriterMode2) List(ctx context.Context, options *metainternalversion
return ll, nil
}
func enrichObject(orig, copy runtime.Object) (runtime.Object, error) {
accessorC, err := meta.Accessor(copy)
// DeleteCollection overrides the behavior of the generic DualWriter and deletes from both LegacyStorage and Storage.
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 {
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 {
return nil, err
klog.FromContext(ctx).Error(err, "failed to delete collection successfully from Storage", "deletedObjects", deleted)
}
accessorC.SetLabels(accessorO.GetLabels())
ac := accessorC.GetAnnotations()
for k, v := range accessorO.GetAnnotations() {
ac[k] = v
}
accessorC.SetAnnotations(ac)
return copy, nil
return res, err
}
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
}
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
}

View File

@@ -68,4 +68,15 @@ func TestMode2(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.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"))
}

View File

@@ -4,6 +4,7 @@ import (
"context"
apierrors "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/apiserver/pkg/registry/rest"
@@ -66,3 +67,23 @@ func (d *DualWriterMode3) Delete(ctx context.Context, name string, deleteValidat
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
}

View File

@@ -42,4 +42,15 @@ func TestMode3(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.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"))
}

View File

@@ -3,6 +3,7 @@ package rest
import (
"context"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"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) {
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)
}

View File

@@ -42,4 +42,15 @@ func TestMode4(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.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"))
}

View File

@@ -101,6 +101,12 @@ func (c *spyStorageClient) Delete(ctx context.Context, name string, deleteValida
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
type LegacyStorageSpyClient interface {
@@ -184,6 +190,12 @@ func (c *spyLegacyStorageClient) Delete(ctx context.Context, name string, delete
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 {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`