Skip to content

Commit 4a58167

Browse files
authored
fix: use the current component version for the default resource version (#684)
<!-- markdownlint-disable MD041 --> #### What this PR does / why we need it Fixes #629 #### 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 3929cc1 commit 4a58167

File tree

2 files changed

+98
-6
lines changed

2 files changed

+98
-6
lines changed

pkg/ocm/ocm.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,6 @@ func (c *Client) GetResource(
172172
cv *v1alpha1.ComponentVersion,
173173
resource *v1alpha1.ResourceReference,
174174
) (io.ReadCloser, string, int64, error) {
175-
// we default to the latest component version if we don't have any versions for the resource.
176-
version := cv.Status.ReconciledVersion
177-
if resource.ElementMeta.Version != "" {
178-
version = resource.ElementMeta.Version
179-
}
180-
181175
cd, err := component.GetComponentDescriptor(ctx, c.client, resource.ReferencePath, cv.Status.ComponentDescriptor)
182176
if err != nil {
183177
return nil, "", -1, fmt.Errorf("failed to find component descriptor for reference: %w", err)
@@ -190,6 +184,12 @@ func (c *Client) GetResource(
190184
)
191185
}
192186

187+
// we default to the latest component version that this resource belongs to if we don't have any versions for the resource.
188+
version := cd.Spec.Version
189+
if resource.ElementMeta.Version != "" {
190+
version = resource.ElementMeta.Version
191+
}
192+
193193
identity := ocmmetav1.Identity{
194194
v1alpha1.ComponentNameKey: cd.Name,
195195
v1alpha1.ComponentVersionKey: cd.Spec.Version,

pkg/ocm/ocm_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,98 @@ func TestClient_VerifyComponentDifferentPublicKey(t *testing.T) {
10371037
assert.False(t, verified, "verified should have been false, but it did not")
10381038
}
10391039

1040+
func TestClient_GetResourceUsesComponentDescriptorVersionAsDefault(t *testing.T) {
1041+
component := "ocm.software/ocm-demo-index"
1042+
resource := "remote-controller-demo"
1043+
componentVersion := "v1.2.3"
1044+
reconciledVersion := "v0.9.9"
1045+
data := "testdata"
1046+
1047+
octx := fakeocm.NewFakeOCMContext()
1048+
1049+
// Create component with actual component version, not reconciled version
1050+
comp := &fakeocm.Component{
1051+
Name: component,
1052+
Version: reconciledVersion,
1053+
}
1054+
res := &fakeocm.Resource[*ocm.ResourceMeta]{
1055+
Name: resource,
1056+
Version: componentVersion,
1057+
Data: []byte(data),
1058+
Component: comp,
1059+
Kind: "localBlob",
1060+
Type: "ociBlob",
1061+
}
1062+
comp.Resources = append(comp.Resources, res)
1063+
1064+
_ = octx.AddComponent(comp)
1065+
1066+
cd := &v1alpha1.ComponentDescriptor{
1067+
ObjectMeta: metav1.ObjectMeta{
1068+
Namespace: "default",
1069+
Name: "github.com-open-component-model-ocm-demo-index-v1.2.3-12345",
1070+
},
1071+
Spec: v1alpha1.ComponentDescriptorSpec{
1072+
Version: componentVersion,
1073+
},
1074+
}
1075+
1076+
fakeKubeClient := env.FakeKubeClient(WithObjects(cd))
1077+
cache := &fakes.FakeCache{}
1078+
cache.IsCachedReturns(false, nil)
1079+
cache.FetchDataByDigestReturns(io.NopCloser(strings.NewReader("mockdata")), nil)
1080+
cache.PushDataReturns("sha256:8fa155245ea8d3f2ea3add7d090d42dfb0e22799018fded6aae24f0c1a1c3f38", nil)
1081+
1082+
ocmClient := NewClient(fakeKubeClient, cache)
1083+
1084+
cv := &v1alpha1.ComponentVersion{
1085+
ObjectMeta: metav1.ObjectMeta{
1086+
Name: "test-name",
1087+
Namespace: "default",
1088+
},
1089+
Spec: v1alpha1.ComponentVersionSpec{
1090+
Component: component,
1091+
Version: v1alpha1.Version{
1092+
Semver: componentVersion,
1093+
},
1094+
Repository: v1alpha1.Repository{
1095+
URL: "localhost",
1096+
},
1097+
},
1098+
Status: v1alpha1.ComponentVersionStatus{
1099+
ReconciledVersion: reconciledVersion,
1100+
ComponentDescriptor: v1alpha1.Reference{
1101+
Name: component,
1102+
Version: componentVersion,
1103+
ComponentDescriptorRef: meta.NamespacedObjectReference{
1104+
Name: "github.com-open-component-model-ocm-demo-index-v1.2.3-12345",
1105+
Namespace: "default",
1106+
},
1107+
},
1108+
},
1109+
}
1110+
1111+
// Test resource reference without explicit version - should use cd.Spec.Version as default
1112+
resourceRef := &v1alpha1.ResourceReference{
1113+
ElementMeta: v1alpha1.ElementMeta{
1114+
Name: "remote-controller-demo",
1115+
},
1116+
}
1117+
1118+
reader, digest, _, err := ocmClient.GetResource(context.Background(), octx, cv, resourceRef)
1119+
assert.NoError(t, err)
1120+
content, err := io.ReadAll(reader)
1121+
require.NoError(t, err)
1122+
assert.Equal(t, "mockdata", string(content))
1123+
assert.Equal(t, "sha256:8fa155245ea8d3f2ea3add7d090d42dfb0e22799018fded6aae24f0c1a1c3f38", digest)
1124+
1125+
// This assertion verifies that the resource version used is the component descriptor version
1126+
// (v1.2.3) rather than the reconciled version (v0.9.9)
1127+
args := cache.PushDataCallingArgumentsOnCall(0)
1128+
assert.Equal(t, data, args.Content)
1129+
assert.Equal(t, componentVersion, args.Version, "resource version should default to component descriptor version (v1.2.3), not reconciled version (v0.9.9)")
1130+
}
1131+
10401132
func TestResourceLookupWithVersionAndExtraIdentity(t *testing.T) {
10411133
// Create Two resources, identical in name but differs in extraIdentity.
10421134
// It should find the right resource.

0 commit comments

Comments
 (0)