Skip to content

Commit 5d3943e

Browse files
committed
Change ComputeRequestIdentifier parameters to clarify usage
Signed-off-by: Lennart Jern <[email protected]>
1 parent 2634ca3 commit 5d3943e

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

internal/controllers/topology/cluster/structuredmerge/dryrun.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func dryRunSSAPatch(ctx context.Context, dryRunCtx *dryRunSSAPatchInput) (bool,
5454
// The identifier consists of: gvk, namespace, name and resourceVersion of originalUnstructured
5555
// and a hash of modifiedUnstructured.
5656
// This ensures that we re-run the request as soon as either original or modified changes.
57-
requestIdentifier, err := ssa.ComputeRequestIdentifier(dryRunCtx.client.Scheme(), dryRunCtx.originalUnstructured, dryRunCtx.modifiedUnstructured)
57+
requestIdentifier, err := ssa.ComputeRequestIdentifier(dryRunCtx.client.Scheme(), dryRunCtx.originalUnstructured.GetResourceVersion(), dryRunCtx.modifiedUnstructured)
5858
if err != nil {
5959
return false, false, nil, err
6060
}

internal/util/ssa/cache.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,18 @@ func (r *ssaCache) Has(key, kind string) bool {
104104
// ComputeRequestIdentifier computes a request identifier for the cache.
105105
// The identifier is unique for a specific request to ensure we don't have to re-run the request
106106
// once we found out that it would not produce a diff.
107-
// The identifier consists of: gvk, namespace, name and resourceVersion of the original object and a hash of the modified
108-
// object. This ensures that we re-run the request as soon as either original or modified changes.
109-
func ComputeRequestIdentifier(scheme *runtime.Scheme, original, modified client.Object) (string, error) {
110-
modifiedObjectHash, err := hash.Compute(modified)
107+
// The identifier consists of: gvk, namespace, name and resourceVersion of the object and a hash of the modified
108+
// object. This ensures that we re-run the request as soon as anything changes.
109+
func ComputeRequestIdentifier(scheme *runtime.Scheme, resourceVersion string, obj client.Object) (string, error) {
110+
objHash, err := hash.Compute(obj)
111111
if err != nil {
112-
return "", errors.Wrapf(err, "failed to calculate request identifier: failed to compute hash for modified object")
112+
return "", errors.Wrapf(err, "failed to calculate request identifier: failed to compute hash for object")
113113
}
114114

115-
gvk, err := apiutil.GVKForObject(original, scheme)
115+
gvk, err := apiutil.GVKForObject(obj, scheme)
116116
if err != nil {
117-
return "", errors.Wrapf(err, "failed to calculate request identifier: failed to get GroupVersionKind of original object %s", klog.KObj(original))
117+
return "", errors.Wrapf(err, "failed to calculate request identifier: failed to get GroupVersionKind of object %s", klog.KObj(obj))
118118
}
119119

120-
return fmt.Sprintf("%s.%s.%s.%d", gvk.String(), klog.KObj(original), original.GetResourceVersion(), modifiedObjectHash), nil
120+
return fmt.Sprintf("%s.%s.%s.%d", gvk.String(), klog.KObj(obj), resourceVersion, objHash), nil
121121
}

internal/util/ssa/patch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func Patch(ctx context.Context, c client.Client, fieldManager string, modified c
9494
var requestIdentifier string
9595
if options.WithCachingProxy {
9696
// Check if the request is cached.
97-
requestIdentifier, err = ComputeRequestIdentifier(c.Scheme(), options.Original, modifiedUnstructured)
97+
requestIdentifier, err = ComputeRequestIdentifier(c.Scheme(), options.Original.GetResourceVersion(), modifiedUnstructured)
9898
if err != nil {
9999
return errors.Wrapf(err, "failed to apply object")
100100
}
@@ -135,9 +135,9 @@ func Patch(ctx context.Context, c client.Client, fieldManager string, modified c
135135
if options.WithCachingProxy && !options.WithDryRun {
136136
// If the object changed, we need to recompute the request identifier before caching.
137137
if options.Original.GetResourceVersion() != modifiedUnstructured.GetResourceVersion() {
138-
// NOTE: This takes the resourceVersion from modifiedUnstructured, and the hash from
139-
// modifiedUnstructuredBeforeApply, which is what we want.
140-
requestIdentifier, err = ComputeRequestIdentifier(c.Scheme(), modifiedUnstructured, modifiedUnstructuredBeforeApply)
138+
// NOTE: This uses the resourceVersion from modifiedUnstructured (after apply), and the hash from
139+
// modifiedUnstructuredBeforeApply (what we wanted to apply), which is what we want.
140+
requestIdentifier, err = ComputeRequestIdentifier(c.Scheme(), modifiedUnstructured.GetResourceVersion(), modifiedUnstructuredBeforeApply)
141141
if err != nil {
142142
return errors.Wrapf(err, "failed to compute request identifier after apply")
143143
}

internal/util/ssa/patch_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func TestPatch(t *testing.T) {
8787
// Compute request identifier before the update, so we can later verify that the update call was not cached with this identifier.
8888
modifiedUnstructured, err := prepareModified(env.Scheme(), modifiedObject)
8989
g.Expect(err).ToNot(HaveOccurred())
90-
oldRequestIdentifier, err := ComputeRequestIdentifier(env.GetScheme(), originalObject, modifiedUnstructured)
90+
oldRequestIdentifier, err := ComputeRequestIdentifier(env.GetScheme(), originalObject.GetResourceVersion(), modifiedUnstructured)
9191
g.Expect(err).ToNot(HaveOccurred())
9292
// Save a copy of modifiedUnstructured before apply to compute the new identifier later
9393
modifiedUnstructuredBeforeApply := modifiedUnstructured.DeepCopy()
@@ -101,7 +101,7 @@ func TestPatch(t *testing.T) {
101101
objectAfterApply := initialObject.DeepCopy()
102102
g.Expect(env.GetAPIReader().Get(ctx, client.ObjectKeyFromObject(objectAfterApply), objectAfterApply)).To(Succeed())
103103
// Compute the new request identifier (after apply)
104-
newRequestIdentifier, err := ComputeRequestIdentifier(env.GetScheme(), objectAfterApply, modifiedUnstructuredBeforeApply)
104+
newRequestIdentifier, err := ComputeRequestIdentifier(env.GetScheme(), objectAfterApply.GetResourceVersion(), modifiedUnstructuredBeforeApply)
105105
g.Expect(err).ToNot(HaveOccurred())
106106
// Verify that request was cached with the new identifier (after apply)
107107
g.Expect(ssaCache.Has(newRequestIdentifier, initialObject.GetKind())).To(BeTrue())
@@ -116,7 +116,7 @@ func TestPatch(t *testing.T) {
116116
// Compute request identifier, so we can later verify that the update call was cached.
117117
modifiedUnstructured, err = prepareModified(env.Scheme(), modifiedObject)
118118
g.Expect(err).ToNot(HaveOccurred())
119-
requestIdentifierNoOp, err := ComputeRequestIdentifier(env.GetScheme(), originalObject, modifiedUnstructured)
119+
requestIdentifierNoOp, err := ComputeRequestIdentifier(env.GetScheme(), originalObject.GetResourceVersion(), modifiedUnstructured)
120120
g.Expect(err).ToNot(HaveOccurred())
121121
// Update the object
122122
applyCallCount = 0
@@ -189,7 +189,7 @@ func TestPatch(t *testing.T) {
189189
// Compute request identifier before the update, so we can later verify that the update call was not cached with this identifier.
190190
modifiedUnstructured, err := prepareModified(env.Scheme(), modifiedObject)
191191
g.Expect(err).ToNot(HaveOccurred())
192-
oldRequestIdentifier, err := ComputeRequestIdentifier(env.GetScheme(), originalObject, modifiedUnstructured)
192+
oldRequestIdentifier, err := ComputeRequestIdentifier(env.GetScheme(), originalObject.GetResourceVersion(), modifiedUnstructured)
193193
g.Expect(err).ToNot(HaveOccurred())
194194
// Save a copy of modifiedUnstructured before apply to compute the new identifier later
195195
modifiedUnstructuredBeforeApply := modifiedUnstructured.DeepCopy()
@@ -204,12 +204,8 @@ func TestPatch(t *testing.T) {
204204
// Get the actual object from server after apply to compute the new request identifier
205205
objectAfterApply := initialObject.DeepCopy()
206206
g.Expect(env.GetAPIReader().Get(ctx, client.ObjectKeyFromObject(objectAfterApply), objectAfterApply)).To(Succeed())
207-
// Convert to unstructured WITHOUT filtering to preserve server fields (like resourceVersion)
208-
objectAfterApplyUnstructured := &unstructured.Unstructured{}
209-
err = env.GetScheme().Convert(objectAfterApply, objectAfterApplyUnstructured, nil)
210-
g.Expect(err).ToNot(HaveOccurred())
211207
// Compute the new request identifier (after apply)
212-
newRequestIdentifier, err := ComputeRequestIdentifier(env.GetScheme(), objectAfterApplyUnstructured, modifiedUnstructuredBeforeApply)
208+
newRequestIdentifier, err := ComputeRequestIdentifier(env.GetScheme(), objectAfterApply.GetResourceVersion(), modifiedUnstructuredBeforeApply)
213209
g.Expect(err).ToNot(HaveOccurred())
214210
// Verify that request was cached with the new identifier (after apply)
215211
g.Expect(ssaCache.Has(newRequestIdentifier, initialObject.GetObjectKind().GroupVersionKind().Kind)).To(BeTrue())
@@ -232,7 +228,7 @@ func TestPatch(t *testing.T) {
232228
// Compute request identifier, so we can later verify that the update call was cached.
233229
modifiedUnstructured, err = prepareModified(env.Scheme(), modifiedObject)
234230
g.Expect(err).ToNot(HaveOccurred())
235-
requestIdentifierNoOp, err := ComputeRequestIdentifier(env.GetScheme(), originalObject, modifiedUnstructured)
231+
requestIdentifierNoOp, err := ComputeRequestIdentifier(env.GetScheme(), originalObject.GetResourceVersion(), modifiedUnstructured)
236232
g.Expect(err).ToNot(HaveOccurred())
237233
// Update the object
238234
applyCallCount = 0

0 commit comments

Comments
 (0)