Skip to content

Commit 502bd56

Browse files
Storage: Extract returned object hydration into function (grafana#88012)
* Extract returned object hydration into function * Finish writing tests for utils func * Lint * Update pkg/apiserver/rest/dualwriter_mode2.go Co-authored-by: Arati R. <[email protected]> * Better var naming * Remove duplicated logic * Lint * Fix test * Lint * Make type private * Fix one more test * Fix test --------- Co-authored-by: Arati R. <[email protected]>
1 parent 4f3ab51 commit 502bd56

8 files changed

+578
-596
lines changed

pkg/apiserver/rest/dualwriter_mode1.go

+13-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"time"
77

8-
"k8s.io/apimachinery/pkg/api/meta"
98
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
109
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1110
"k8s.io/apimachinery/pkg/runtime"
@@ -16,8 +15,8 @@ import (
1615
type DualWriterMode1 struct {
1716
Legacy LegacyStorage
1817
Storage Storage
19-
Log klog.Logger
2018
*dualWriterMetrics
19+
Log klog.Logger
2120
}
2221

2322
const (
@@ -38,40 +37,34 @@ func (d *DualWriterMode1) Mode() DualWriterMode {
3837
}
3938

4039
// Create overrides the behavior of the generic DualWriter and writes only to LegacyStorage.
41-
func (d *DualWriterMode1) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
40+
func (d *DualWriterMode1) Create(ctx context.Context, original runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
4241
log := d.Log.WithValues("kind", options.Kind)
4342
ctx = klog.NewContext(ctx, log)
4443
var method = "create"
4544

4645
startLegacy := time.Now()
47-
res, err := d.Legacy.Create(ctx, obj, createValidation, options)
46+
created, err := d.Legacy.Create(ctx, original, createValidation, options)
4847
if err != nil {
4948
log.Error(err, "unable to create object in legacy storage")
5049
d.recordLegacyDuration(true, mode1Str, options.Kind, method, startLegacy)
51-
return res, err
50+
return created, err
5251
}
5352
d.recordLegacyDuration(false, mode1Str, options.Kind, method, startLegacy)
5453

5554
go func() {
56-
accessorCreated, err := meta.Accessor(res)
57-
if err != nil {
58-
log.Error(err, "unable to get accessor for created object")
59-
}
60-
61-
accessorOld, err := meta.Accessor(obj)
55+
ctx, cancel := context.WithTimeoutCause(ctx, time.Second*10, errors.New("storage create timeout"))
56+
createdLegacy, err := enrichLegacyObject(original, created, true)
6257
if err != nil {
63-
log.Error(err, "unable to get accessor for old object")
58+
cancel()
6459
}
6560

66-
enrichObject(accessorOld, accessorCreated)
6761
startStorage := time.Now()
68-
ctx, cancel := context.WithTimeoutCause(ctx, time.Second*10, errors.New("storage create timeout"))
6962
defer cancel()
70-
_, errObjectSt := d.Storage.Create(ctx, obj, createValidation, options)
63+
_, errObjectSt := d.Storage.Create(ctx, createdLegacy, createValidation, options)
7164
d.recordStorageDuration(errObjectSt != nil, mode1Str, options.Kind, method, startStorage)
7265
}()
7366

74-
return res, nil
67+
return created, nil
7568
}
7669

7770
// Get overrides the behavior of the generic DualWriter and reads only from LegacyStorage.
@@ -188,6 +181,7 @@ func (d *DualWriterMode1) Update(ctx context.Context, name string, objInfo rest.
188181
d.recordLegacyDuration(false, mode1Str, options.Kind, method, startLegacy)
189182

190183
go func() {
184+
ctx, cancel := context.WithTimeoutCause(ctx, time.Second*10, errors.New("storage update timeout"))
191185
updated, err := objInfo.UpdatedObject(ctx, res)
192186
if err != nil {
193187
log.WithValues("object", updated).Error(err, "could not update or create object")
@@ -201,27 +195,17 @@ func (d *DualWriterMode1) Update(ctx context.Context, name string, objInfo rest.
201195

202196
// if the object is found, create a new updateWrapper with the object found
203197
if foundObj != nil {
204-
accessorOld, err := meta.Accessor(foundObj)
205-
if err != nil {
206-
log.Error(err, "unable to get accessor for original updated object")
207-
}
208-
209-
accessor, err := meta.Accessor(res)
198+
res, err := enrichLegacyObject(foundObj, res, false)
210199
if err != nil {
211-
log.Error(err, "unable to get accessor for updated object")
200+
log.Error(err, "could not enrich object")
201+
cancel()
212202
}
213-
214-
accessor.SetResourceVersion(accessorOld.GetResourceVersion())
215-
accessor.SetUID(accessorOld.GetUID())
216-
217-
enrichObject(accessorOld, accessor)
218203
objInfo = &updateWrapper{
219204
upstream: objInfo,
220205
updated: res,
221206
}
222207
}
223208
startStorage := time.Now()
224-
ctx, cancel := context.WithTimeoutCause(ctx, time.Second*10, errors.New("storage update timeout"))
225209
defer cancel()
226210
_, _, errObjectSt := d.Storage.Update(ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)
227211
d.recordStorageDuration(errObjectSt != nil, mode1Str, options.Kind, method, startStorage)

0 commit comments

Comments
 (0)