Skip to content

Commit 415ee58

Browse files
authored
fix: adjust the resource controller to use the descriptor version for defaulting instead of the component version (#690)
<!-- markdownlint-disable MD041 --> #### What this PR does / why we need it #### Which issue(s) this PR fixes <!-- Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`. --> Signed-off-by: Gergely Brautigam <[email protected]>
1 parent d5f2657 commit 415ee58

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

controllers/resource_controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,6 @@ func (r *ResourceReconciler) reconcile(
205205
// The reader is unused here, but we should still close it, so it's not left over.
206206
defer reader.Close()
207207

208-
version := componentVersion.Status.ReconciledVersion
209-
// GetVersion returns resourceRef.Version
210-
if obj.Spec.SourceRef.GetVersion() != "" {
211-
version = obj.Spec.SourceRef.GetVersion()
212-
}
213-
214208
// This is important because THIS is the actual component for our resource. If we used ComponentVersion in the
215209
// below identity, that would be the top-level component instead of the component that this resource belongs to.
216210
componentDescriptor, err := component.GetComponentDescriptor(ctx, r.Client, obj.GetReferencePath(), componentVersion.Status.ComponentDescriptor)
@@ -231,6 +225,12 @@ func (r *ResourceReconciler) reconcile(
231225
return ctrl.Result{}, err
232226
}
233227

228+
version := componentDescriptor.Spec.Version
229+
// GetVersion returns resourceRef.Version
230+
if obj.Spec.SourceRef.GetVersion() != "" {
231+
version = obj.Spec.SourceRef.GetVersion()
232+
}
233+
234234
rreconcile.ProgressiveStatus(false, obj, meta.ProgressingReason, "resource retrieve, constructing snapshot with name %s", obj.GetSnapshotName())
235235

236236
identity := r.constructIdentity(componentDescriptor, obj, version)
@@ -265,7 +265,7 @@ func (r *ResourceReconciler) reconcile(
265265
return ctrl.Result{}, err
266266
}
267267

268-
obj.Status.LastAppliedResourceVersion = obj.Spec.SourceRef.GetVersion()
268+
obj.Status.LastAppliedResourceVersion = version
269269
obj.Status.LastAppliedComponentVersion = componentVersion.Status.ReconciledVersion
270270

271271
metrics.SnapshotNumberOfBytesReconciled.WithLabelValues(obj.GetSnapshotName(), digest, componentVersion.Name).Set(float64(size))

controllers/resource_controller_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,83 @@ func XTestResourceReconcilerFailed(t *testing.T) {
261261
assert.True(t, conditions.IsFalse(resource, meta.ReadyCondition))
262262
}
263263

264+
func TestResourceReconcilerVersionDefaulting(t *testing.T) {
265+
t.Log("setting up resource object without explicit version")
266+
resource := DefaultResource.DeepCopy()
267+
resource.Spec.SourceRef.ResourceRef.ReferencePath = nil
268+
resource.Spec.SourceRef.ResourceRef.Version = ""
269+
resource.Status.SnapshotName = "test-resource-version-default"
270+
271+
t.Log("setting up component version with different reconciled version")
272+
cv := DefaultComponent.DeepCopy()
273+
cv.Status.ReconciledVersion = "v0.9.9"
274+
275+
t.Log("setting up component descriptor with different version")
276+
cd := DefaultComponentDescriptor.DeepCopy()
277+
cd.Spec.Version = "v1.2.3"
278+
279+
cv.Status.ComponentDescriptor = v1alpha1.Reference{
280+
Name: resource.Spec.SourceRef.Name,
281+
Version: resource.Spec.SourceRef.GetVersion(),
282+
ComponentDescriptorRef: meta.NamespacedObjectReference{
283+
Name: cd.Name,
284+
Namespace: cd.Namespace,
285+
},
286+
}
287+
conditions.MarkTrue(cv,
288+
meta.ReadyCondition,
289+
meta.SucceededReason,
290+
"Applied version: v0.9.9")
291+
292+
client := env.FakeKubeClient(WithObjects(cv, resource, cd))
293+
t.Log("priming fake cache")
294+
cache := &cachefakes.FakeCache{}
295+
cache.PushDataReturns("digest", nil)
296+
297+
t.Log("priming fake ocm client")
298+
ocmClient := &fakes.MockFetcher{}
299+
ocmClient.GetResourceReturns(io.NopCloser(bytes.NewBuffer([]byte("content"))), "digest", nil)
300+
recorder := record.NewFakeRecorder(32)
301+
302+
rr := ResourceReconciler{
303+
Scheme: env.scheme,
304+
Client: client,
305+
OCMClient: ocmClient,
306+
EventRecorder: recorder,
307+
Cache: cache,
308+
}
309+
310+
t.Log("calling reconcile on resource controller")
311+
_, err := rr.Reconcile(context.Background(), ctrl.Request{
312+
NamespacedName: types.NamespacedName{
313+
Namespace: resource.Namespace,
314+
Name: resource.Name,
315+
},
316+
})
317+
require.NoError(t, err)
318+
319+
t.Log("verifying generated snapshot uses component descriptor version")
320+
snapshot := &v1alpha1.Snapshot{}
321+
err = client.Get(context.Background(), types.NamespacedName{
322+
Name: resource.Status.SnapshotName,
323+
Namespace: resource.Namespace,
324+
}, snapshot)
325+
326+
require.NoError(t, err)
327+
assert.Equal(t, "digest", snapshot.Spec.Digest)
328+
assert.Equal(t, "v1.2.3", snapshot.Spec.Tag)
329+
330+
t.Log("verifying resource status uses component descriptor version")
331+
err = client.Get(context.Background(), types.NamespacedName{
332+
Name: resource.Name,
333+
Namespace: resource.Namespace,
334+
}, resource)
335+
336+
require.NoError(t, err)
337+
assert.Equal(t, "v1.2.3", resource.Status.LastAppliedResourceVersion)
338+
assert.True(t, conditions.IsTrue(resource, meta.ReadyCondition))
339+
}
340+
264341
// TODO: rewrite these so that they test the predicate functions.
265342
func XTestResourceShouldReconcile(t *testing.T) {
266343
testcase := []struct {

0 commit comments

Comments
 (0)