Skip to content

Commit 34de08a

Browse files
committed
feat(scale): update CR in UpdatedObjectInfo impl
1 parent 35cfd32 commit 34de08a

File tree

1 file changed

+62
-40
lines changed
  • staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource

1 file changed

+62
-40
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/etcd.go

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -237,48 +237,17 @@ func (r *ScaleREST) Get(ctx context.Context, name string, options *metav1.GetOpt
237237
}
238238

239239
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,
270245
}
271246

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(
279248
ctx,
280-
cr.GetName(),
281-
rest.DefaultUpdatedObjectInfo(cr),
249+
name,
250+
scaleObjInfo,
282251
toScaleCreateValidation(createValidation, r.specReplicasPath, r.statusReplicasPath, r.labelSelectorPath),
283252
toScaleUpdateValidation(updateValidation, r.specReplicasPath, r.statusReplicasPath, r.labelSelectorPath),
284253
false,
@@ -287,12 +256,13 @@ func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.Update
287256
if err != nil {
288257
return nil, false, err
289258
}
290-
cr = obj.(*unstructured.Unstructured)
259+
cr := obj.(*unstructured.Unstructured)
291260

292261
newScale, _, err := scaleFromCustomResource(cr, r.specReplicasPath, r.statusReplicasPath, r.labelSelectorPath)
293262
if err != nil {
294263
return nil, false, apierrors.NewBadRequest(err.Error())
295264
}
265+
296266
return newScale, false, err
297267
}
298268

@@ -372,3 +342,55 @@ func scaleFromCustomResource(cr *unstructured.Unstructured, specReplicasPath, st
372342

373343
return scale, foundSpecReplicas, nil
374344
}
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

Comments
 (0)