5
5
"errors"
6
6
"time"
7
7
8
- "k8s.io/apimachinery/pkg/api/meta"
9
8
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
10
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
10
"k8s.io/apimachinery/pkg/runtime"
@@ -16,8 +15,8 @@ import (
16
15
type DualWriterMode1 struct {
17
16
Legacy LegacyStorage
18
17
Storage Storage
19
- Log klog.Logger
20
18
* dualWriterMetrics
19
+ Log klog.Logger
21
20
}
22
21
23
22
const (
@@ -38,40 +37,34 @@ func (d *DualWriterMode1) Mode() DualWriterMode {
38
37
}
39
38
40
39
// 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 ) {
42
41
log := d .Log .WithValues ("kind" , options .Kind )
43
42
ctx = klog .NewContext (ctx , log )
44
43
var method = "create"
45
44
46
45
startLegacy := time .Now ()
47
- res , err := d .Legacy .Create (ctx , obj , createValidation , options )
46
+ created , err := d .Legacy .Create (ctx , original , createValidation , options )
48
47
if err != nil {
49
48
log .Error (err , "unable to create object in legacy storage" )
50
49
d .recordLegacyDuration (true , mode1Str , options .Kind , method , startLegacy )
51
- return res , err
50
+ return created , err
52
51
}
53
52
d .recordLegacyDuration (false , mode1Str , options .Kind , method , startLegacy )
54
53
55
54
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 )
62
57
if err != nil {
63
- log . Error ( err , "unable to get accessor for old object" )
58
+ cancel ( )
64
59
}
65
60
66
- enrichObject (accessorOld , accessorCreated )
67
61
startStorage := time .Now ()
68
- ctx , cancel := context .WithTimeoutCause (ctx , time .Second * 10 , errors .New ("storage create timeout" ))
69
62
defer cancel ()
70
- _ , errObjectSt := d .Storage .Create (ctx , obj , createValidation , options )
63
+ _ , errObjectSt := d .Storage .Create (ctx , createdLegacy , createValidation , options )
71
64
d .recordStorageDuration (errObjectSt != nil , mode1Str , options .Kind , method , startStorage )
72
65
}()
73
66
74
- return res , nil
67
+ return created , nil
75
68
}
76
69
77
70
// 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.
188
181
d .recordLegacyDuration (false , mode1Str , options .Kind , method , startLegacy )
189
182
190
183
go func () {
184
+ ctx , cancel := context .WithTimeoutCause (ctx , time .Second * 10 , errors .New ("storage update timeout" ))
191
185
updated , err := objInfo .UpdatedObject (ctx , res )
192
186
if err != nil {
193
187
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.
201
195
202
196
// if the object is found, create a new updateWrapper with the object found
203
197
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 )
210
199
if err != nil {
211
- log .Error (err , "unable to get accessor for updated object" )
200
+ log .Error (err , "could not enrich object" )
201
+ cancel ()
212
202
}
213
-
214
- accessor .SetResourceVersion (accessorOld .GetResourceVersion ())
215
- accessor .SetUID (accessorOld .GetUID ())
216
-
217
- enrichObject (accessorOld , accessor )
218
203
objInfo = & updateWrapper {
219
204
upstream : objInfo ,
220
205
updated : res ,
221
206
}
222
207
}
223
208
startStorage := time .Now ()
224
- ctx , cancel := context .WithTimeoutCause (ctx , time .Second * 10 , errors .New ("storage update timeout" ))
225
209
defer cancel ()
226
210
_ , _ , errObjectSt := d .Storage .Update (ctx , name , objInfo , createValidation , updateValidation , forceAllowCreate , options )
227
211
d .recordStorageDuration (errObjectSt != nil , mode1Str , options .Kind , method , startStorage )
0 commit comments