mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Unified Storage: Always populate RV and UID on create (#89554)
This commit is contained in:
@@ -54,7 +54,7 @@ func (d *DualWriterMode1) Create(ctx context.Context, original runtime.Object, c
|
||||
ctx, cancel := context.WithTimeoutCause(ctx, time.Second*10, errors.New("storage create timeout"))
|
||||
defer cancel()
|
||||
|
||||
if err := enrichLegacyObject(original, createdCopy, true); err != nil {
|
||||
if err := enrichLegacyObject(original, createdCopy); err != nil {
|
||||
cancel()
|
||||
}
|
||||
|
||||
@@ -201,7 +201,7 @@ func (d *DualWriterMode1) Update(ctx context.Context, name string, objInfo rest.
|
||||
|
||||
// if the object is found, create a new updateWrapper with the object found
|
||||
if foundObj != nil {
|
||||
if err := enrichLegacyObject(foundObj, resCopy, false); err != nil {
|
||||
if err := enrichLegacyObject(foundObj, resCopy); err != nil {
|
||||
log.Error(err, "could not enrich object")
|
||||
cancel()
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func (d *DualWriterMode2) Create(ctx context.Context, original runtime.Object, c
|
||||
}
|
||||
d.recordLegacyDuration(false, mode2Str, options.Kind, method, startLegacy)
|
||||
|
||||
if err := enrichLegacyObject(original, created, true); err != nil {
|
||||
if err := enrichLegacyObject(original, created); err != nil {
|
||||
return created, err
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ func (d *DualWriterMode2) Update(ctx context.Context, name string, objInfo rest.
|
||||
|
||||
// if the object is found, create a new updateWrapper with the object found
|
||||
if foundObj != nil {
|
||||
err = enrichLegacyObject(foundObj, obj, false)
|
||||
err = enrichLegacyObject(foundObj, obj)
|
||||
if err != nil {
|
||||
return obj, false, err
|
||||
}
|
||||
@@ -328,7 +328,7 @@ func parseList(legacyList []runtime.Object) (metainternalversion.ListOptions, ma
|
||||
return options, indexMap, nil
|
||||
}
|
||||
|
||||
func enrichLegacyObject(originalObj, returnedObj runtime.Object, created bool) error {
|
||||
func enrichLegacyObject(originalObj, returnedObj runtime.Object) error {
|
||||
accessorReturned, err := meta.Accessor(returnedObj)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -350,13 +350,6 @@ func enrichLegacyObject(originalObj, returnedObj runtime.Object, created bool) e
|
||||
}
|
||||
accessorReturned.SetAnnotations(ac)
|
||||
|
||||
// if the object is created, we need to reset the resource version and UID
|
||||
// create method expects an empty resource version
|
||||
if created {
|
||||
accessorReturned.SetResourceVersion("")
|
||||
accessorReturned.SetUID("")
|
||||
return nil
|
||||
}
|
||||
// otherwise, we propagate the original RV and UID
|
||||
accessorReturned.SetResourceVersion(accessorOriginal.GetResourceVersion())
|
||||
accessorReturned.SetUID(accessorOriginal.GetUID())
|
||||
|
||||
@@ -79,7 +79,7 @@ func TestMode2_Create(t *testing.T) {
|
||||
assert.Equal(t, exampleObj, obj)
|
||||
accessor, err := meta.Accessor(obj)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, accessor.GetResourceVersion(), "")
|
||||
assert.Equal(t, accessor.GetResourceVersion(), "1")
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -493,83 +493,7 @@ func TestEnrichReturnedObject(t *testing.T) {
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "create: original object does not have labels and annotations",
|
||||
inputOriginal: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "1", UID: types.UID("5")},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
inputReturned: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "2", UID: types.UID("6"), Labels: map[string]string{"label1": "1"}, Annotations: map[string]string{"annotation1": "1"}},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
isCreated: true,
|
||||
expectedObject: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "", UID: types.UID("")},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "create: returned object does not have labels and annotations",
|
||||
inputOriginal: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "1", UID: types.UID("5"), Labels: map[string]string{"label1": "1"}, Annotations: map[string]string{"annotation1": "1"}},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
inputReturned: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "2", UID: types.UID("6")},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
isCreated: true,
|
||||
expectedObject: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "", UID: types.UID(""), Labels: map[string]string{"label1": "1"}, Annotations: map[string]string{"annotation1": "1"}},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "create: both objects have labels and annotations",
|
||||
inputOriginal: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "1", UID: types.UID("5"), Labels: map[string]string{"label1": "1"}, Annotations: map[string]string{"annotation1": "1"}},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
inputReturned: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "2", UID: types.UID("6"), Labels: map[string]string{"label2": "2"}, Annotations: map[string]string{"annotation2": "2"}},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
isCreated: true,
|
||||
expectedObject: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "", UID: types.UID(""), Labels: map[string]string{"label1": "1"}, Annotations: map[string]string{"annotation1": "1"}},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "create: both objects have labels and annotations with duplicated keys",
|
||||
inputOriginal: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "1", UID: types.UID("5"), Labels: map[string]string{"label1": "1"}, Annotations: map[string]string{"annotation1": "1"}},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
inputReturned: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "2", UID: types.UID("6"), Labels: map[string]string{"label1": "11"}, Annotations: map[string]string{"annotation1": "11"}},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
isCreated: true,
|
||||
expectedObject: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "", UID: types.UID(""), Labels: map[string]string{"label1": "1"}, Annotations: map[string]string{"annotation1": "1"}},
|
||||
Spec: example.PodSpec{}, Status: example.PodStatus{},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update: original object does not have labels and annotations",
|
||||
name: "original object does not have labels and annotations",
|
||||
inputOriginal: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "1", UID: types.UID("5")},
|
||||
@@ -587,7 +511,7 @@ func TestEnrichReturnedObject(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update: returned object does not have labels and annotations",
|
||||
name: "returned object does not have labels and annotations",
|
||||
inputOriginal: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "1", UID: types.UID("5"), Labels: map[string]string{"label1": "1"}, Annotations: map[string]string{"annotation1": "1"}},
|
||||
@@ -605,7 +529,7 @@ func TestEnrichReturnedObject(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update: both objects have labels and annotations",
|
||||
name: "both objects have labels and annotations",
|
||||
inputOriginal: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "1", UID: types.UID("5"), Labels: map[string]string{"label1": "1"}, Annotations: map[string]string{"annotation1": "1"}},
|
||||
@@ -623,7 +547,7 @@ func TestEnrichReturnedObject(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "update: both objects have labels and annotations with duplicated keys",
|
||||
name: "both objects have labels and annotations with duplicated keys",
|
||||
inputOriginal: &example.Pod{
|
||||
TypeMeta: metav1.TypeMeta{Kind: "foo"},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "foo", ResourceVersion: "1", UID: types.UID("5"), Labels: map[string]string{"label1": "1"}, Annotations: map[string]string{"annotation1": "1"}},
|
||||
@@ -658,7 +582,7 @@ func TestEnrichReturnedObject(t *testing.T) {
|
||||
|
||||
for _, tt := range testCase {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := enrichLegacyObject(tt.inputOriginal, tt.inputReturned, tt.isCreated)
|
||||
err := enrichLegacyObject(tt.inputOriginal, tt.inputReturned)
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user