@@ -237,48 +237,17 @@ func (r *ScaleREST) Get(ctx context.Context, name string, options *metav1.GetOpt
237
237
}
238
238
239
239
func (r * ScaleREST ) Update (ctx context.Context , name string , objInfo rest.UpdatedObjectInfo , createValidation rest.ValidateObjectFunc , updateValidation rest.ValidateObjectUpdateFunc , forceAllowCreate bool , options * metav1.UpdateOptions ) (runtime.Object , bool , error ) {
240
- obj , err := r .store .Get (ctx , name , & metav1.GetOptions {})
241
- if err != nil {
242
- return nil , false , err
243
- }
244
- cr := obj .(* unstructured.Unstructured )
245
-
246
- const invalidSpecReplicas = - 2147483648 // smallest int32
247
- oldScale , replicasFound , err := scaleFromCustomResource (cr , r .specReplicasPath , r .statusReplicasPath , r .labelSelectorPath )
248
- if err != nil {
249
- return nil , false , err
250
- }
251
- if ! replicasFound {
252
- oldScale .Spec .Replicas = invalidSpecReplicas // signal that this was not set before
253
- }
254
-
255
- obj , err = objInfo .UpdatedObject (ctx , oldScale )
256
- if err != nil {
257
- return nil , false , err
258
- }
259
- if obj == nil {
260
- return nil , false , apierrors .NewBadRequest (fmt .Sprintf ("nil update passed to Scale" ))
261
- }
262
-
263
- scale , ok := obj .(* autoscalingv1.Scale )
264
- if ! ok {
265
- return nil , false , apierrors .NewBadRequest (fmt .Sprintf ("wrong object passed to Scale update: %v" , obj ))
266
- }
267
-
268
- if scale .Spec .Replicas == invalidSpecReplicas {
269
- return nil , false , apierrors .NewBadRequest (fmt .Sprintf ("the spec replicas field %q cannot be empty" , r .specReplicasPath ))
240
+ scaleObjInfo := & scaleUpdatedObjectInfo {
241
+ reqObjInfo : objInfo ,
242
+ specReplicasPath : r .specReplicasPath ,
243
+ labelSelectorPath : r .labelSelectorPath ,
244
+ statusReplicasPath : r .statusReplicasPath ,
270
245
}
271
246
272
- specReplicasPath := strings .TrimPrefix (r .specReplicasPath , "." ) // ignore leading period
273
- if err = unstructured .SetNestedField (cr .Object , int64 (scale .Spec .Replicas ), strings .Split (specReplicasPath , "." )... ); err != nil {
274
- return nil , false , err
275
- }
276
- cr .SetResourceVersion (scale .ResourceVersion )
277
-
278
- obj , _ , err = r .store .Update (
247
+ obj , _ , err := r .store .Update (
279
248
ctx ,
280
- cr . GetName () ,
281
- rest . DefaultUpdatedObjectInfo ( cr ) ,
249
+ name ,
250
+ scaleObjInfo ,
282
251
toScaleCreateValidation (createValidation , r .specReplicasPath , r .statusReplicasPath , r .labelSelectorPath ),
283
252
toScaleUpdateValidation (updateValidation , r .specReplicasPath , r .statusReplicasPath , r .labelSelectorPath ),
284
253
false ,
@@ -287,12 +256,13 @@ func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.Update
287
256
if err != nil {
288
257
return nil , false , err
289
258
}
290
- cr = obj .(* unstructured.Unstructured )
259
+ cr : = obj .(* unstructured.Unstructured )
291
260
292
261
newScale , _ , err := scaleFromCustomResource (cr , r .specReplicasPath , r .statusReplicasPath , r .labelSelectorPath )
293
262
if err != nil {
294
263
return nil , false , apierrors .NewBadRequest (err .Error ())
295
264
}
265
+
296
266
return newScale , false , err
297
267
}
298
268
@@ -372,3 +342,55 @@ func scaleFromCustomResource(cr *unstructured.Unstructured, specReplicasPath, st
372
342
373
343
return scale , foundSpecReplicas , nil
374
344
}
345
+
346
+ type scaleUpdatedObjectInfo struct {
347
+ reqObjInfo rest.UpdatedObjectInfo
348
+ specReplicasPath string
349
+ statusReplicasPath string
350
+ labelSelectorPath string
351
+ }
352
+
353
+ func (i * scaleUpdatedObjectInfo ) Preconditions () * metav1.Preconditions {
354
+ return i .reqObjInfo .Preconditions ()
355
+ }
356
+
357
+ func (i * scaleUpdatedObjectInfo ) UpdatedObject (ctx context.Context , oldObj runtime.Object ) (runtime.Object , error ) {
358
+ cr := oldObj .DeepCopyObject ().(* unstructured.Unstructured )
359
+ const invalidSpecReplicas = - 2147483648 // smallest int32
360
+ oldScale , replicasFound , err := scaleFromCustomResource (cr , i .specReplicasPath , i .statusReplicasPath , i .labelSelectorPath )
361
+ if err != nil {
362
+ return nil , err
363
+ }
364
+ if ! replicasFound {
365
+ oldScale .Spec .Replicas = invalidSpecReplicas // signal that this was not set before
366
+ }
367
+
368
+ obj , err := i .reqObjInfo .UpdatedObject (ctx , oldScale )
369
+ if err != nil {
370
+ return nil , err
371
+ }
372
+ if obj == nil {
373
+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("nil update passed to Scale" ))
374
+ }
375
+
376
+ scale , ok := obj .(* autoscalingv1.Scale )
377
+ if ! ok {
378
+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("wrong object passed to Scale update: %v" , obj ))
379
+ }
380
+
381
+ if scale .Spec .Replicas == invalidSpecReplicas {
382
+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("the spec replicas field %q cannot be empty" , i .specReplicasPath ))
383
+ }
384
+
385
+ specReplicasPath := strings .TrimPrefix (i .specReplicasPath , "." ) // ignore leading period
386
+
387
+ if err := unstructured .SetNestedField (cr .Object , int64 (scale .Spec .Replicas ), strings .Split (specReplicasPath , "." )... ); err != nil {
388
+ return nil , err
389
+ }
390
+ if len (scale .ResourceVersion ) != 0 {
391
+ // The client provided a resourceVersion precondition.
392
+ // Set that precondition and return any conflict errors to the client.
393
+ cr .SetResourceVersion (scale .ResourceVersion )
394
+ }
395
+ return cr , nil
396
+ }
0 commit comments