Skip to content

Commit

Permalink
Refactor NewDualWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
suntala committed May 13, 2024
1 parent 8654454 commit 2e73c4f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 47 deletions.
45 changes: 13 additions & 32 deletions pkg/apiserver/rest/dualwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rest
import (
"context"

"github.com/grafana/grafana/pkg/services/featuremgmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
Expand Down Expand Up @@ -82,19 +81,20 @@ const (
)

// NewDualWriter returns a new DualWriter.
func NewDualWriter(kind string, features featuremgmt.FeatureToggles, legacy LegacyStorage, storage Storage) DualWriter {
// #TODO replace string with Kind type
switch kind {
case "playlist":
if features.IsEnabledGlobally(featuremgmt.FlagDualWritePlaylistsMode3) {
return NewDualWriterMode3(legacy, storage)
}

if features.IsEnabledGlobally(featuremgmt.FlagDualWritePlaylistsMode2) {
return NewDualWriterMode2(legacy, storage)
}

func NewDualWriter(mode DualWriterMode, legacy LegacyStorage, storage Storage) DualWriter {
switch mode {
case Mode1:
// read and write only from legacy storage
return NewDualWriterMode1(legacy, storage)
case Mode2:
// write to both, read from storage but use legacy as backup
return NewDualWriterMode2(legacy, storage)
case Mode3:
// write to both, read from storage only
return NewDualWriterMode3(legacy, storage)
case Mode4:
// read and write only from storage
return NewDualWriterMode4(legacy, storage)
default:
return NewDualWriterMode1(legacy, storage)
}
Expand All @@ -117,22 +117,3 @@ func (u *updateWrapper) Preconditions() *metav1.Preconditions {
func (u *updateWrapper) UpdatedObject(ctx context.Context, oldObj runtime.Object) (newObj runtime.Object, err error) {
return u.updated, nil
}

func selectDualWriter(mode DualWriterMode, legacy LegacyStorage, storage Storage) DualWriter {
switch mode {
case Mode1:
// read and write only from legacy storage
return NewDualWriterMode1(legacy, storage)
case Mode2:
// write to both, read from storage but use legacy as backup
return NewDualWriterMode2(legacy, storage)
case Mode3:
// write to both, read from storage only
return NewDualWriterMode3(legacy, storage)
case Mode4:
// read and write only from storage
return NewDualWriterMode4(legacy, storage)
default:
return NewDualWriterMode1(legacy, storage)
}
}
12 changes: 6 additions & 6 deletions pkg/apiserver/rest/dualwriter_mode1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestMode1_Create(t *testing.T) {
tt.setupStorageFn(m, tt.input)
}

dw := selectDualWriter(Mode1, ls, us)
dw := NewDualWriter(Mode1, ls, us)

obj, err := dw.Create(context.Background(), tt.input, func(context.Context, runtime.Object) error { return nil }, &metav1.CreateOptions{})

Expand Down Expand Up @@ -126,7 +126,7 @@ func TestMode1_Get(t *testing.T) {
tt.setupStorageFn(m, tt.input)
}

dw := selectDualWriter(Mode1, ls, us)
dw := NewDualWriter(Mode1, ls, us)

obj, err := dw.Get(context.Background(), tt.input, &metav1.GetOptions{})

Expand Down Expand Up @@ -175,7 +175,7 @@ func TestMode1_List(t *testing.T) {
tt.setupStorageFn(m)
}

dw := selectDualWriter(Mode1, ls, us)
dw := NewDualWriter(Mode1, ls, us)

_, err := dw.List(context.Background(), &metainternalversion.ListOptions{})

Expand Down Expand Up @@ -228,7 +228,7 @@ func TestMode1_Delete(t *testing.T) {
tt.setupStorageFn(m, tt.input)
}

dw := selectDualWriter(Mode1, ls, us)
dw := NewDualWriter(Mode1, ls, us)

obj, _, err := dw.Delete(context.Background(), tt.input, func(ctx context.Context, obj runtime.Object) error { return nil }, &metav1.DeleteOptions{})

Expand Down Expand Up @@ -285,7 +285,7 @@ func TestMode1_DeleteCollection(t *testing.T) {
tt.setupStorageFn(m, tt.input)
}

dw := selectDualWriter(Mode1, ls, us)
dw := NewDualWriter(Mode1, ls, us)

obj, err := dw.DeleteCollection(context.Background(), func(ctx context.Context, obj runtime.Object) error { return nil }, tt.input, &metainternalversion.ListOptions{})

Expand Down Expand Up @@ -359,7 +359,7 @@ func TestMode1_Update(t *testing.T) {
tt.setupGetFn(m, tt.input)
}

dw := selectDualWriter(Mode1, ls, us)
dw := NewDualWriter(Mode1, ls, us)

obj, _, err := dw.Update(context.Background(), tt.input, UpdatedObjInfoObj{}, func(ctx context.Context, obj runtime.Object) error { return nil }, func(ctx context.Context, obj, old runtime.Object) error { return nil }, false, &metav1.UpdateOptions{})

Expand Down
12 changes: 6 additions & 6 deletions pkg/apiserver/rest/dualwriter_mode2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestMode2_Create(t *testing.T) {
tt.setupStorageFn(m, tt.input)
}

dw := selectDualWriter(Mode2, ls, us)
dw := NewDualWriter(Mode2, ls, us)

obj, err := dw.Create(context.Background(), tt.input, createFn, &metav1.CreateOptions{})

Expand Down Expand Up @@ -138,7 +138,7 @@ func TestMode2_Get(t *testing.T) {
tt.setupStorageFn(m, tt.input)
}

dw := selectDualWriter(Mode2, ls, us)
dw := NewDualWriter(Mode2, ls, us)

obj, err := dw.Get(context.Background(), tt.input, &metav1.GetOptions{})

Expand Down Expand Up @@ -189,7 +189,7 @@ func TestMode2_List(t *testing.T) {
tt.setupStorageFn(m)
}

dw := selectDualWriter(Mode2, ls, us)
dw := NewDualWriter(Mode2, ls, us)

obj, err := dw.List(context.Background(), &metainternalversion.ListOptions{})

Expand Down Expand Up @@ -281,7 +281,7 @@ func TestMode2_Delete(t *testing.T) {
tt.setupStorageFn(m, tt.input)
}

dw := selectDualWriter(Mode2, ls, us)
dw := NewDualWriter(Mode2, ls, us)

obj, _, err := dw.Delete(context.Background(), tt.input, func(context.Context, runtime.Object) error { return nil }, &metav1.DeleteOptions{})

Expand Down Expand Up @@ -351,7 +351,7 @@ func TestMode2_DeleteCollection(t *testing.T) {
tt.setupStorageFn(m)
}

dw := selectDualWriter(Mode2, ls, us)
dw := NewDualWriter(Mode2, ls, us)

obj, err := dw.DeleteCollection(context.Background(), func(ctx context.Context, obj runtime.Object) error { return nil }, &metav1.DeleteOptions{TypeMeta: metav1.TypeMeta{Kind: tt.input}}, &metainternalversion.ListOptions{})

Expand Down Expand Up @@ -458,7 +458,7 @@ func TestMode2_Update(t *testing.T) {
tt.setupStorageFn(m, tt.input)
}

dw := selectDualWriter(Mode2, ls, us)
dw := NewDualWriter(Mode2, ls, us)

obj, _, err := dw.Update(context.Background(), tt.input, UpdatedObjInfoObj{}, func(ctx context.Context, obj runtime.Object) error { return nil }, func(ctx context.Context, obj, old runtime.Object) error { return nil }, false, &metav1.UpdateOptions{})

Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/apis/dashboard/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (b *DashboardsAPIBuilder) GetAPIGroupInfo(
if err := store.CompleteWithOptions(options); err != nil {
return nil, err
}
storage[resourceInfo.StoragePath()] = grafanarest.NewDualWriter("dashboard", featuremgmt.WithFeatures(), legacyStore, store)
storage[resourceInfo.StoragePath()] = grafanarest.NewDualWriter(grafanarest.Mode1, legacyStore, store)
}

// Summary
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/apis/folders/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (b *FolderAPIBuilder) GetAPIGroupInfo(
if err != nil {
return nil, err
}
storage[resourceInfo.StoragePath()] = grafanarest.NewDualWriter("folder", featuremgmt.WithFeatures(), legacyStore, store)
storage[resourceInfo.StoragePath()] = grafanarest.NewDualWriter(grafanarest.Mode1, legacyStore, store)
}

apiGroupInfo.VersionedResourcesStorageMap[v0alpha1.VERSION] = storage
Expand Down
7 changes: 6 additions & 1 deletion pkg/registry/apis/playlist/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ func (b *PlaylistAPIBuilder) GetAPIGroupInfo(
if err != nil {
return nil, err
}
storage[resource.StoragePath()] = grafanarest.NewDualWriter("playlist", b.features, legacyStore, store)

mode := grafanarest.Mode1
if b.features.IsEnabledGlobally(featuremgmt.FlagDualWritePlaylistsMode2) {
mode = grafanarest.Mode2
}
storage[resource.StoragePath()] = grafanarest.NewDualWriter(mode, legacyStore, store)
}

apiGroupInfo.VersionedResourcesStorageMap[playlist.VERSION] = storage
Expand Down

0 comments on commit 2e73c4f

Please sign in to comment.