Skip to content

Commit 7580862

Browse files
committed
fix: consider version when getting CRDs for validating descriptors
An additional condition is included for matching `apiVersion` of example CRs with CRD `version` when searching for the CRD in the CSV. This ensures the correct CRD version is selected for validations. Closes #6781 Signed-off-by: Thuan Vo <thuan.votann@gmail.com>
1 parent 11ff3c8 commit 7580862

File tree

3 files changed

+115
-68
lines changed

3 files changed

+115
-68
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# entries is a list of entries to include in
2+
# release notes and/or the migration guide
3+
entries:
4+
- description: >
5+
An additional condition is included for matching `apiVersion` of example CRs with CRD `version` when searching for the CRD in the CSV.
6+
Previously, The `olm-spec-descriptors` scorecard test failed when multiple versions of CRD is included in the CSV.
7+
The CR specified in `alm-examples` annotations are validated only against the first matched CRD (by name), which is incorrect.
8+
This ensures the correct CRD version is selected for validations.
9+
10+
# kind is one of:
11+
# - addition
12+
# - change
13+
# - deprecation
14+
# - removal
15+
# - bugfix
16+
kind: "bugfix"
17+
18+
# Is this a breaking change?
19+
breaking: false
20+
21+
# NOTE: ONLY USE `pull_request_override` WHEN ADDING THIS
22+
# FILE FOR A PREVIOUSLY MERGED PULL_REQUEST!
23+
#
24+
# The generator auto-detects the PR number from the commit
25+
# message in which this file was originally added.
26+
#
27+
# What is the pull request number (without the "#")?
28+
# pull_request_override: 0

internal/scorecard/tests/bundle_test.go

+85-66
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,25 @@ var _ = Describe("Basic and OLM tests", func() {
127127
Describe("OLM Tests", func() {
128128

129129
Describe("Test Status and Spec Descriptors", func() {
130-
var (
131-
cr unstructured.Unstructured
132-
csv operatorsv1alpha1.ClusterServiceVersion
133-
)
134-
135-
csv = operatorsv1alpha1.ClusterServiceVersion{
130+
csv := operatorsv1alpha1.ClusterServiceVersion{
136131
Spec: operatorsv1alpha1.ClusterServiceVersionSpec{
137132
CustomResourceDefinitions: operatorsv1alpha1.CustomResourceDefinitions{
138133
Owned: []operatorsv1alpha1.CRDDescription{
134+
operatorsv1alpha1.CRDDescription{
135+
Name: "Test",
136+
Version: "v2",
137+
Kind: "TestKind",
138+
StatusDescriptors: []operatorsv1alpha1.StatusDescriptor{
139+
operatorsv1alpha1.StatusDescriptor{
140+
Path: "newStatus",
141+
},
142+
},
143+
SpecDescriptors: []operatorsv1alpha1.SpecDescriptor{
144+
operatorsv1alpha1.SpecDescriptor{
145+
Path: "newSpec",
146+
},
147+
},
148+
},
139149
operatorsv1alpha1.CRDDescription{
140150
Name: "Test",
141151
Version: "v1",
@@ -156,147 +166,156 @@ var _ = Describe("Basic and OLM tests", func() {
156166
},
157167
}
158168

159-
It("should pass when csv with owned cr and required fields is present", func() {
160-
cr = unstructured.Unstructured{
161-
Object: map[string]interface{}{
162-
"status": map[string]interface{}{
163-
"status": "val",
164-
},
165-
"spec": map[string]interface{}{
166-
"spec": "val",
167-
},
168-
},
169+
It("should pass when CR Object Descriptor is nil", func() {
170+
cr := unstructured.Unstructured{
171+
Object: nil,
169172
}
170173
cr.SetGroupVersionKind(schema.GroupVersionKind{
171-
Kind: "TestKind",
172-
Group: "test.example.com",
174+
Kind: "TestKind",
175+
Group: "test.example.com",
176+
Version: "v1",
173177
})
174178

175179
result = checkOwnedCSVStatusDescriptor(cr, &csv, result)
176180
Expect(result.State).To(Equal(scapiv1alpha3.PassState))
177181
})
178182

179-
It("should return warning when no spec status are defined for CRD", func() {
180-
cr = unstructured.Unstructured{
183+
It("should pass when status descriptor field is present in CR", func() {
184+
cr := unstructured.Unstructured{
181185
Object: map[string]interface{}{
186+
"status": map[string]interface{}{
187+
"status": "val",
188+
},
182189
"spec": map[string]interface{}{
183190
"spec": "val",
184191
},
185192
},
186193
}
187194
cr.SetGroupVersionKind(schema.GroupVersionKind{
188-
Kind: "TestKind",
189-
Group: "test.example.com",
195+
Kind: "TestKind",
196+
Group: "test.example.com",
197+
Version: "v1",
190198
})
191199

192200
result = checkOwnedCSVStatusDescriptor(cr, &csv, result)
193-
Expect(result.Suggestions).To(HaveLen(1))
194201
Expect(result.State).To(Equal(scapiv1alpha3.PassState))
195202
})
196203

197-
It("should pass when CR Object Descriptor is nil", func() {
204+
It("should pass when required spec descriptor field is present in CR", func() {
198205
cr := unstructured.Unstructured{
199-
Object: nil,
206+
Object: map[string]interface{}{
207+
"status": map[string]interface{}{
208+
"status": "val",
209+
},
210+
"spec": map[string]interface{}{
211+
"spec": "val",
212+
},
213+
},
200214
}
201215
cr.SetGroupVersionKind(schema.GroupVersionKind{
202-
Kind: "TestKind",
203-
Group: "test.example.com",
216+
Kind: "TestKind",
217+
Group: "test.example.com",
218+
Version: "v1",
204219
})
205220

206-
result = checkOwnedCSVStatusDescriptor(cr, &csv, result)
221+
result = checkOwnedCSVSpecDescriptors(cr, &csv, result)
207222
Expect(result.State).To(Equal(scapiv1alpha3.PassState))
208223
})
209224

210-
It("should fail when CR Object Descriptor is nil and CRD with given GVK cannot be found", func() {
225+
It("should pass with warning when no status descriptor field is present in CR", func() {
211226
cr := unstructured.Unstructured{
212-
Object: nil,
227+
Object: map[string]interface{}{
228+
"spec": map[string]interface{}{
229+
"spec": "val",
230+
},
231+
},
213232
}
214233
cr.SetGroupVersionKind(schema.GroupVersionKind{
215-
Kind: "TestKindNotPresent",
216-
Group: "testnotpresent.example.com",
234+
Kind: "TestKind",
235+
Group: "test.example.com",
236+
Version: "v1",
217237
})
218238

219239
result = checkOwnedCSVStatusDescriptor(cr, &csv, result)
220-
Expect(result.State).To(Equal(scapiv1alpha3.FailState))
240+
Expect(result.Suggestions).To(HaveLen(1))
241+
Expect(result.State).To(Equal(scapiv1alpha3.PassState))
221242
})
222243

223-
It("should fail when owned CRD for CR does not have GVK set", func() {
244+
It("should fail CRD with given GVK cannot be found", func() {
224245
cr := unstructured.Unstructured{
225246
Object: map[string]interface{}{
226247
"status": map[string]interface{}{
227248
"status": "val",
228249
},
250+
"spec": map[string]interface{}{
251+
"spec": "val",
252+
},
229253
},
230254
}
255+
cr.SetGroupVersionKind(schema.GroupVersionKind{
256+
Kind: "TestKindNotPresent",
257+
Group: "testnotpresent.example.com",
258+
Version: "unknown",
259+
})
231260

232261
result = checkOwnedCSVStatusDescriptor(cr, &csv, result)
233262
Expect(result.State).To(Equal(scapiv1alpha3.FailState))
234263
})
235264

236-
It("should fail when required descriptor field is not present in CR", func() {
265+
It("should fail when CR does not have GVK set", func() {
237266
cr := unstructured.Unstructured{
238267
Object: map[string]interface{}{
239-
"node": map[string]interface{}{
240-
"node": "val",
268+
"status": map[string]interface{}{
269+
"status": "val",
241270
},
242271
},
243272
}
244273

245274
result = checkOwnedCSVStatusDescriptor(cr, &csv, result)
246275
Expect(result.State).To(Equal(scapiv1alpha3.FailState))
247276
})
248-
It("should pass when required descriptor field is present in CR", func() {
277+
278+
It("should fail when required spec descriptor field is not present in CR", func() {
249279
cr := unstructured.Unstructured{
250280
Object: map[string]interface{}{
251-
"status": map[string]interface{}{
252-
"status": "val",
253-
},
254281
"spec": map[string]interface{}{
255-
"spec": "val",
282+
"node": "val",
256283
},
257284
},
258285
}
259286
cr.SetGroupVersionKind(schema.GroupVersionKind{
260-
Kind: "TestKind",
261-
Group: "test.example.com",
287+
Kind: "TestKind",
288+
Group: "test.example.com",
289+
Version: "v1",
262290
})
263291

264-
result = checkOwnedCSVSpecDescriptors(cr, &csv, result)
265-
Expect(result.State).To(Equal(scapiv1alpha3.PassState))
266-
})
267-
It("should fail when required spec descriptor field is not present in CR", func() {
268-
cr := unstructured.Unstructured{
269-
Object: map[string]interface{}{
270-
"status": map[string]interface{}{
271-
"status": "val",
272-
},
273-
},
274-
}
275-
276292
result = checkOwnedCSVSpecDescriptors(cr, &csv, result)
277293
Expect(result.State).To(Equal(scapiv1alpha3.FailState))
278294
})
279-
It("should fail when CRs do not have spec field specified", func() {
295+
296+
It("should pass when CRs have spec field specified", func() {
280297
cr := []unstructured.Unstructured{
281-
unstructured.Unstructured{
282-
Object: map[string]interface{}{},
298+
{
299+
Object: map[string]interface{}{
300+
"spec": map[string]interface{}{
301+
"spec": "val",
302+
},
303+
},
283304
},
284305
}
285306
result = checkSpec(cr, result)
286307
Expect(result.State).To(Equal(scapiv1alpha3.PassState))
287308
})
288-
It("should pass when CRs do have spec field specified", func() {
309+
310+
It("should pass with warning when CRs do not have spec field specified", func() {
289311
cr := []unstructured.Unstructured{
290-
unstructured.Unstructured{
291-
Object: map[string]interface{}{
292-
"spec": map[string]interface{}{
293-
"spec": "val",
294-
},
295-
},
312+
{
313+
Object: map[string]interface{}{},
296314
},
297315
}
298316
result = checkSpec(cr, result)
299317
Expect(result.State).To(Equal(scapiv1alpha3.PassState))
318+
Expect(result.Suggestions).To(HaveLen(1))
300319
})
301320

302321
})

internal/scorecard/tests/olm.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func checkOwnedCSVStatusDescriptor(cr unstructured.Unstructured, csv *operatorsv
237237
var crdDescription *operatorsv1alpha1.CRDDescription
238238

239239
for _, owned := range csv.Spec.CustomResourceDefinitions.Owned {
240-
if owned.Kind == cr.GetKind() {
240+
if owned.Kind == cr.GetKind() && owned.Version == cr.GroupVersionKind().Version {
241241
crdDescription = &owned
242242
break
243243
}
@@ -288,7 +288,7 @@ func checkOwnedCSVSpecDescriptors(cr unstructured.Unstructured, csv *operatorsv1
288288

289289
var crd *operatorsv1alpha1.CRDDescription
290290
for _, owned := range csv.Spec.CustomResourceDefinitions.Owned {
291-
if owned.Kind == cr.GetKind() {
291+
if owned.Kind == cr.GetKind() && owned.Version == cr.GroupVersionKind().Version {
292292
crd = &owned
293293
break
294294
}

0 commit comments

Comments
 (0)