mirror of https://github.com/grafana/grafana
Choose mode. Turn DualWriter into a real interface. Better Logging (#87291)
* Choose mode. Add log field on top level dualwriter * Add logs * Turn DualWriter into a full interface. Fix tests * Lint * Use struct for dualWriter interface * Use struct * Default should be legacyStore for all entities * Fix test. Get rid of extra concrete type * Remove comment * Add comment * Temp set dualwriter mode 2 for playlists while configs are not in place * Add modes type + add comment on what each mode does * Don't require watcher interface for now * Use storage implementation on mode 2 * Update pkg/apiserver/rest/dualwriter_mode2.go Co-authored-by: Todd Treece <360020+toddtreece@users.noreply.github.com> * Pass log values to the context * test * Update pkg/apiserver/rest/dualwriter_mode3.go Co-authored-by: Dan Cech <dcech@grafana.com> --------- Co-authored-by: Todd Treece <360020+toddtreece@users.noreply.github.com> Co-authored-by: Dan Cech <dcech@grafana.com>pull/87411/head
parent
0bc8992dfa
commit
0a2c5065a0
@ -1,72 +1,72 @@ |
||||
package rest |
||||
|
||||
import ( |
||||
"context" |
||||
"testing" |
||||
// import (
|
||||
// "context"
|
||||
// "testing"
|
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
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/apis/example" |
||||
) |
||||
// "github.com/stretchr/testify/assert"
|
||||
// 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/apis/example"
|
||||
// )
|
||||
|
||||
func TestMode3(t *testing.T) { |
||||
var ls = (LegacyStorage)(nil) |
||||
var s = (Storage)(nil) |
||||
lsSpy := NewLegacyStorageSpyClient(ls) |
||||
sSpy := NewStorageSpyClient(s) |
||||
// func TestMode3(t *testing.T) {
|
||||
// var ls = (LegacyStorage)(nil)
|
||||
// var s = (Storage)(nil)
|
||||
// lsSpy := NewLegacyStorageSpyClient(ls)
|
||||
// sSpy := NewStorageSpyClient(s)
|
||||
|
||||
dw := NewDualWriterMode3(lsSpy, sSpy) |
||||
// dw := NewDualWriterMode3(lsSpy, sSpy)
|
||||
|
||||
// Create: it should use the Legacy Create implementation
|
||||
_, err := dw.Create(context.Background(), &dummyObject{}, func(context.Context, runtime.Object) error { return nil }, &metav1.CreateOptions{}) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Create")) |
||||
assert.Equal(t, 1, sSpy.Counts("Storage.Create")) |
||||
// // Create: it should use the Legacy Create implementation
|
||||
// _, err := dw.Create(context.Background(), &dummyObject{}, func(context.Context, runtime.Object) error { return nil }, &metav1.CreateOptions{})
|
||||
// assert.NoError(t, err)
|
||||
// assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Create"))
|
||||
// assert.Equal(t, 1, sSpy.Counts("Storage.Create"))
|
||||
|
||||
// Get: it should use the Storage Get implementation
|
||||
_, err = dw.Get(context.Background(), kind, &metav1.GetOptions{}) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Get")) |
||||
assert.Equal(t, 1, sSpy.Counts("Storage.Get")) |
||||
// // Get: it should use the Storage Get implementation
|
||||
// _, err = dw.Get(context.Background(), kind, &metav1.GetOptions{})
|
||||
// assert.NoError(t, err)
|
||||
// assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Get"))
|
||||
// assert.Equal(t, 1, sSpy.Counts("Storage.Get"))
|
||||
|
||||
// List: it should use the Storage List implementation
|
||||
_, err = dw.List(context.Background(), &metainternalversion.ListOptions{}) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.List")) |
||||
assert.Equal(t, 1, sSpy.Counts("Storage.List")) |
||||
// // List: it should use the Storage List implementation
|
||||
// _, err = dw.List(context.Background(), &metainternalversion.ListOptions{})
|
||||
// assert.NoError(t, err)
|
||||
// assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.List"))
|
||||
// assert.Equal(t, 1, sSpy.Counts("Storage.List"))
|
||||
|
||||
// Delete: it should use call both Legacy and Storage Delete methods
|
||||
var deleteValidation = func(ctx context.Context, obj runtime.Object) error { return nil } |
||||
_, _, err = dw.Delete(context.Background(), kind, deleteValidation, &metav1.DeleteOptions{}) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Delete")) |
||||
assert.Equal(t, 1, sSpy.Counts("Storage.Delete")) |
||||
// // Delete: it should use call both Legacy and Storage Delete methods
|
||||
// var deleteValidation = func(ctx context.Context, obj runtime.Object) error { return nil }
|
||||
// _, _, err = dw.Delete(context.Background(), kind, deleteValidation, &metav1.DeleteOptions{})
|
||||
// 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")) |
||||
// // 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"))
|
||||
|
||||
// Update: it should update in both storages
|
||||
dummy := &example.Pod{} |
||||
uoi := UpdatedObjInfoObj{} |
||||
_, err = uoi.UpdatedObject(context.Background(), dummy) |
||||
assert.NoError(t, err) |
||||
// // Update: it should update in both storages
|
||||
// dummy := &example.Pod{}
|
||||
// uoi := UpdatedObjInfoObj{}
|
||||
// _, err = uoi.UpdatedObject(context.Background(), dummy)
|
||||
// assert.NoError(t, err)
|
||||
|
||||
var validateObjFn = func(ctx context.Context, obj runtime.Object) error { return nil } |
||||
var validateObjUpdateFn = func(ctx context.Context, obj, old runtime.Object) error { return nil } |
||||
// var validateObjFn = func(ctx context.Context, obj runtime.Object) error { return nil }
|
||||
// var validateObjUpdateFn = func(ctx context.Context, obj, old runtime.Object) error { return nil }
|
||||
|
||||
_, _, err = dw.Update(context.Background(), kind, uoi, validateObjFn, validateObjUpdateFn, false, &metav1.UpdateOptions{}) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Update")) |
||||
assert.Equal(t, 1, sSpy.Counts("Storage.Update")) |
||||
assert.NoError(t, err) |
||||
} |
||||
// _, _, err = dw.Update(context.Background(), kind, uoi, validateObjFn, validateObjUpdateFn, false, &metav1.UpdateOptions{})
|
||||
// assert.NoError(t, err)
|
||||
// assert.Equal(t, 1, lsSpy.Counts("LegacyStorage.Update"))
|
||||
// assert.Equal(t, 1, sSpy.Counts("Storage.Update"))
|
||||
// assert.NoError(t, err)
|
||||
// }
|
||||
|
@ -1,72 +1,72 @@ |
||||
package rest |
||||
|
||||
import ( |
||||
"context" |
||||
"testing" |
||||
// import (
|
||||
// "context"
|
||||
// "testing"
|
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
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/apis/example" |
||||
) |
||||
// "github.com/stretchr/testify/assert"
|
||||
// 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/apis/example"
|
||||
// )
|
||||
|
||||
func TestMode4(t *testing.T) { |
||||
var ls = (LegacyStorage)(nil) |
||||
var s = (Storage)(nil) |
||||
lsSpy := NewLegacyStorageSpyClient(ls) |
||||
sSpy := NewStorageSpyClient(s) |
||||
// func TestMode4(t *testing.T) {
|
||||
// var ls = (LegacyStorage)(nil)
|
||||
// var s = (Storage)(nil)
|
||||
// lsSpy := NewLegacyStorageSpyClient(ls)
|
||||
// sSpy := NewStorageSpyClient(s)
|
||||
|
||||
dw := NewDualWriterMode4(lsSpy, sSpy) |
||||
// dw := NewDualWriterMode4(lsSpy, sSpy)
|
||||
|
||||
// Create: it should use the Legacy Create implementation
|
||||
_, err := dw.Create(context.Background(), &dummyObject{}, func(context.Context, runtime.Object) error { return nil }, &metav1.CreateOptions{}) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Create")) |
||||
assert.Equal(t, 1, sSpy.Counts("Storage.Create")) |
||||
// // Create: it should use the Legacy Create implementation
|
||||
// _, err := dw.Create(context.Background(), &dummyObject{}, func(context.Context, runtime.Object) error { return nil }, &metav1.CreateOptions{})
|
||||
// assert.NoError(t, err)
|
||||
// assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Create"))
|
||||
// assert.Equal(t, 1, sSpy.Counts("Storage.Create"))
|
||||
|
||||
// Get: it should use the Storage Get implementation
|
||||
_, err = dw.Get(context.Background(), kind, &metav1.GetOptions{}) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Get")) |
||||
assert.Equal(t, 1, sSpy.Counts("Storage.Get")) |
||||
// // Get: it should use the Storage Get implementation
|
||||
// _, err = dw.Get(context.Background(), kind, &metav1.GetOptions{})
|
||||
// assert.NoError(t, err)
|
||||
// assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Get"))
|
||||
// assert.Equal(t, 1, sSpy.Counts("Storage.Get"))
|
||||
|
||||
// List: it should use the Storage Get implementation
|
||||
_, err = dw.List(context.Background(), &metainternalversion.ListOptions{}) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.List")) |
||||
assert.Equal(t, 1, sSpy.Counts("Storage.List")) |
||||
// // List: it should use the Storage Get implementation
|
||||
// _, err = dw.List(context.Background(), &metainternalversion.ListOptions{})
|
||||
// assert.NoError(t, err)
|
||||
// assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.List"))
|
||||
// assert.Equal(t, 1, sSpy.Counts("Storage.List"))
|
||||
|
||||
// Delete: it should use call Storage Delete method
|
||||
var deleteValidation = func(ctx context.Context, obj runtime.Object) error { return nil } |
||||
_, _, err = dw.Delete(context.Background(), kind, deleteValidation, &metav1.DeleteOptions{}) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Delete")) |
||||
assert.Equal(t, 1, sSpy.Counts("Storage.Delete")) |
||||
// // Delete: it should use call Storage Delete method
|
||||
// var deleteValidation = func(ctx context.Context, obj runtime.Object) error { return nil }
|
||||
// _, _, err = dw.Delete(context.Background(), kind, deleteValidation, &metav1.DeleteOptions{})
|
||||
// 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")) |
||||
// // 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"))
|
||||
|
||||
// Update: it should update only in Storage
|
||||
dummy := &example.Pod{} |
||||
uoi := UpdatedObjInfoObj{} |
||||
_, err = uoi.UpdatedObject(context.Background(), dummy) |
||||
assert.NoError(t, err) |
||||
// // Update: it should update only in Storage
|
||||
// dummy := &example.Pod{}
|
||||
// uoi := UpdatedObjInfoObj{}
|
||||
// _, err = uoi.UpdatedObject(context.Background(), dummy)
|
||||
// assert.NoError(t, err)
|
||||
|
||||
var validateObjFn = func(ctx context.Context, obj runtime.Object) error { return nil } |
||||
var validateObjUpdateFn = func(ctx context.Context, obj, old runtime.Object) error { return nil } |
||||
// var validateObjFn = func(ctx context.Context, obj runtime.Object) error { return nil }
|
||||
// var validateObjUpdateFn = func(ctx context.Context, obj, old runtime.Object) error { return nil }
|
||||
|
||||
_, _, err = dw.Update(context.Background(), kind, uoi, validateObjFn, validateObjUpdateFn, false, &metav1.UpdateOptions{}) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Update")) |
||||
assert.Equal(t, 1, sSpy.Counts("Storage.Update")) |
||||
assert.NoError(t, err) |
||||
} |
||||
// _, _, err = dw.Update(context.Background(), kind, uoi, validateObjFn, validateObjUpdateFn, false, &metav1.UpdateOptions{})
|
||||
// assert.NoError(t, err)
|
||||
// assert.Equal(t, 0, lsSpy.Counts("LegacyStorage.Update"))
|
||||
// assert.Equal(t, 1, sSpy.Counts("Storage.Update"))
|
||||
// assert.NoError(t, err)
|
||||
// }
|
||||
|
Loading…
Reference in new issue