Skip to content

Commit eada9e1

Browse files
committed
UPSTREAM: <carry>: do not set the generation for cached object
1 parent 858a219 commit eada9e1

File tree

2 files changed

+21
-1
lines changed
  • staging/src/k8s.io

2 files changed

+21
-1
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"k8s.io/apimachinery/pkg/runtime"
3434
"k8s.io/apimachinery/pkg/runtime/schema"
3535
"k8s.io/apimachinery/pkg/util/validation/field"
36+
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
3637
"k8s.io/apiserver/pkg/features"
3738
apiserverstorage "k8s.io/apiserver/pkg/storage"
3839
"k8s.io/apiserver/pkg/storage/names"
@@ -114,6 +115,15 @@ func (a customResourceStrategy) PrepareForCreate(ctx context.Context, obj runtim
114115
}
115116

116117
accessor, _ := meta.Accessor(obj)
118+
if _, found := accessor.GetAnnotations()[genericapirequest.AnnotationKey]; found {
119+
// in general the shard annotation is not attached to objects, instead, it is assigned by the storage layer on the fly
120+
// to avoid an additional UPDATE request (mismatch on the generation field) replicated objects have the shard annotation set
121+
// thus we need to remove the shard annotation and simply return early so that the generation is not reset to 1
122+
annotations := accessor.GetAnnotations()
123+
delete(annotations, genericapirequest.AnnotationKey)
124+
accessor.SetAnnotations(annotations)
125+
return
126+
}
117127
accessor.SetGeneration(1)
118128
}
119129

@@ -144,6 +154,11 @@ func (a customResourceStrategy) PrepareForUpdate(ctx context.Context, obj, old r
144154
if !apiequality.Semantic.DeepEqual(newCopyContent, oldCopyContent) {
145155
oldAccessor, _ := meta.Accessor(oldCustomResourceObject)
146156
newAccessor, _ := meta.Accessor(newCustomResourceObject)
157+
if _, found := oldAccessor.GetAnnotations()[genericapirequest.AnnotationKey]; found {
158+
// the presence of the annotation indicates the object is from the cache server.
159+
// since the objects from the cache should not be modified in any way, just return early.
160+
return
161+
}
147162
newAccessor.SetGeneration(oldAccessor.GetGeneration() + 1)
148163
}
149164
}

staging/src/k8s.io/apiserver/pkg/registry/rest/update.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,12 @@ func BeforeUpdate(strategy RESTUpdateStrategy, ctx context.Context, obj, old run
126126
if err != nil {
127127
return err
128128
}
129-
objectMeta.SetGeneration(oldMeta.GetGeneration())
129+
if len(oldMeta.GetAnnotations()[genericapirequest.AnnotationKey]) == 0 || objectMeta.GetGeneration() == 0 {
130+
// the absence of the annotation indicates the object is NOT from the cache server,
131+
// if the new object doesn't have its generation set, just rewrite it from the old object
132+
// otherwise we are dealing with an object from the cache server that wants its generation to be updated
133+
objectMeta.SetGeneration(oldMeta.GetGeneration())
134+
}
130135

131136
// Ensure managedFields state is removed unless ServerSideApply is enabled
132137
if !utilfeature.DefaultFeatureGate.Enabled(features.ServerSideApply) {

0 commit comments

Comments
 (0)