@@ -2,11 +2,13 @@ package convert
22
33import (
44 "fmt"
5+ "strings"
56 "testing"
67
78 . "github.com/onsi/ginkgo/v2"
89 . "github.com/onsi/gomega"
910
11+ appsv1 "k8s.io/api/apps/v1"
1012 corev1 "k8s.io/api/core/v1"
1113 rbacv1 "k8s.io/api/rbac/v1"
1214 schedulingv1 "k8s.io/api/scheduling/v1"
@@ -73,7 +75,7 @@ var _ = Describe("RegistryV1 Suite", func() {
7375 Expect (plainBundle .Objects ).To (HaveLen (1 ))
7476
7577 By ("verifying if ns has been set correctly" )
76- resObj := containsObject ( unstructuredSvc , plainBundle .Objects )
78+ resObj := findObjectByName ( svc . Name , plainBundle .Objects )
7779 Expect (resObj ).NotTo (BeNil ())
7880 Expect (resObj .GetNamespace ()).To (BeEquivalentTo (installNamespace ))
7981 })
@@ -99,7 +101,7 @@ var _ = Describe("RegistryV1 Suite", func() {
99101 Expect (plainBundle .Objects ).To (HaveLen (1 ))
100102
101103 By ("verifying if ns has been set correctly" )
102- resObj := containsObject ( unstructuredSvc , plainBundle .Objects )
104+ resObj := findObjectByName ( svc . Name , plainBundle .Objects )
103105 Expect (resObj ).NotTo (BeNil ())
104106 Expect (resObj .GetNamespace ()).To (BeEquivalentTo (installNamespace ))
105107 })
@@ -157,7 +159,7 @@ var _ = Describe("RegistryV1 Suite", func() {
157159 Expect (plainBundle .Objects ).To (HaveLen (1 ))
158160
159161 By ("verifying if ns has been set correctly" )
160- resObj := containsObject ( unstructuredpriorityclass , plainBundle .Objects )
162+ resObj := findObjectByName ( pc . Name , plainBundle .Objects )
161163 Expect (resObj ).NotTo (BeNil ())
162164 Expect (resObj .GetNamespace ()).To (BeEmpty ())
163165 })
@@ -167,22 +169,38 @@ var _ = Describe("RegistryV1 Suite", func() {
167169 Context ("Should generate objects successfully based on target namespaces" , func () {
168170 var (
169171 svc corev1.Service
170- csv v1alpha1.ClusterServiceVersion
172+ baseCSV v1alpha1.ClusterServiceVersion
171173 watchNamespaces []string
172174 )
173175
174176 BeforeEach (func () {
175- csv = v1alpha1.ClusterServiceVersion {
177+ baseCSV = v1alpha1.ClusterServiceVersion {
176178 ObjectMeta : metav1.ObjectMeta {
177179 Name : "testCSV" ,
178180 Annotations : map [string ]string {
179181 "olm.properties" : fmt .Sprintf ("[{\" type\" : %s, \" value\" : \" %s\" }]" , property .TypeConstraint , "value" ),
180182 },
181183 },
182184 Spec : v1alpha1.ClusterServiceVersionSpec {
183- InstallModes : []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true }},
184185 InstallStrategy : v1alpha1.NamedInstallStrategy {
185186 StrategySpec : v1alpha1.StrategyDetailsDeployment {
187+ DeploymentSpecs : []v1alpha1.StrategyDeploymentSpec {
188+ {
189+ Name : "testDeployment" ,
190+ Spec : appsv1.DeploymentSpec {
191+ Template : corev1.PodTemplateSpec {
192+ Spec : corev1.PodSpec {
193+ Containers : []corev1.Container {
194+ {
195+ Name : "testContainer" ,
196+ Image : "testImage" ,
197+ },
198+ },
199+ },
200+ },
201+ },
202+ },
203+ },
186204 Permissions : []v1alpha1.StrategyDeploymentPermissions {
187205 {
188206 ServiceAccountName : "testServiceAccount" ,
@@ -199,6 +217,7 @@ var _ = Describe("RegistryV1 Suite", func() {
199217 },
200218 },
201219 }
220+
202221 svc = corev1.Service {
203222 ObjectMeta : metav1.ObjectMeta {
204223 Name : "testService" ,
@@ -208,13 +227,16 @@ var _ = Describe("RegistryV1 Suite", func() {
208227 installNamespace = "testInstallNamespace"
209228 })
210229
211- It ("should convert into plain manifests successfully" , func () {
230+ It ("should convert into plain manifests successfully with AllNamespaces" , func () {
231+ csv := baseCSV .DeepCopy ()
232+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeAllNamespaces , Supported : true }}
233+
212234 By ("creating a registry v1 bundle" )
213- watchNamespaces = []string {"testWatchNs1" , "testWatchNs2 " }
235+ watchNamespaces = []string {"" }
214236 unstructuredSvc := convertToUnstructured (svc )
215237 registryv1Bundle = RegistryV1 {
216238 PackageName : "testPkg" ,
217- CSV : csv ,
239+ CSV : * csv ,
218240 Others : []unstructured.Unstructured {unstructuredSvc },
219241 }
220242
@@ -224,41 +246,51 @@ var _ = Describe("RegistryV1 Suite", func() {
224246
225247 By ("verifying if plain bundle has required objects" )
226248 Expect (plainBundle ).ShouldNot (BeNil ())
227- Expect (plainBundle .Objects ).To (HaveLen (6 ))
249+ Expect (plainBundle .Objects ).To (HaveLen (5 ))
250+
251+ By ("verifying olm.targetNamespaces annotation in the deployment's pod template" )
252+ dep := findObjectByName ("testDeployment" , plainBundle .Objects )
253+ Expect (dep ).NotTo (BeNil ())
254+ Expect (dep .(* appsv1.Deployment ).Spec .Template .Annotations ).To (HaveKeyWithValue ("olm.targetNamespaces" , strings .Join (watchNamespaces , "," )))
228255 })
229256
230- It ("should convert into plain manifests successfully with single namespace" , func () {
231- csv = v1alpha1.ClusterServiceVersion {
232- ObjectMeta : metav1.ObjectMeta {
233- Name : "testCSV" ,
234- },
235- Spec : v1alpha1.ClusterServiceVersionSpec {
236- InstallModes : []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : true }},
237- InstallStrategy : v1alpha1.NamedInstallStrategy {
238- StrategySpec : v1alpha1.StrategyDetailsDeployment {
239- Permissions : []v1alpha1.StrategyDeploymentPermissions {
240- {
241- ServiceAccountName : "testServiceAccount" ,
242- Rules : []rbacv1.PolicyRule {
243- {
244- APIGroups : []string {"test" },
245- Resources : []string {"pods" },
246- Verbs : []string {"*" },
247- },
248- },
249- },
250- },
251- },
252- },
253- },
257+ It ("should convert into plain manifests successfully with MultiNamespace" , func () {
258+ csv := baseCSV .DeepCopy ()
259+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true }}
260+
261+ By ("creating a registry v1 bundle" )
262+ watchNamespaces = []string {"testWatchNs1" , "testWatchNs2" }
263+ unstructuredSvc := convertToUnstructured (svc )
264+ registryv1Bundle = RegistryV1 {
265+ PackageName : "testPkg" ,
266+ CSV : * csv ,
267+ Others : []unstructured.Unstructured {unstructuredSvc },
254268 }
255269
270+ By ("converting to plain" )
271+ plainBundle , err := Convert (registryv1Bundle , installNamespace , watchNamespaces )
272+ Expect (err ).NotTo (HaveOccurred ())
273+
274+ By ("verifying if plain bundle has required objects" )
275+ Expect (plainBundle ).ShouldNot (BeNil ())
276+ Expect (plainBundle .Objects ).To (HaveLen (7 ))
277+
278+ By ("verifying olm.targetNamespaces annotation in the deployment's pod template" )
279+ dep := findObjectByName ("testDeployment" , plainBundle .Objects )
280+ Expect (dep ).NotTo (BeNil ())
281+ Expect (dep .(* appsv1.Deployment ).Spec .Template .Annotations ).To (HaveKeyWithValue ("olm.targetNamespaces" , strings .Join (watchNamespaces , "," )))
282+ })
283+
284+ It ("should convert into plain manifests successfully with SingleNamespace" , func () {
285+ csv := baseCSV .DeepCopy ()
286+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : true }}
287+
256288 By ("creating a registry v1 bundle" )
257289 watchNamespaces = []string {"testWatchNs1" }
258290 unstructuredSvc := convertToUnstructured (svc )
259291 registryv1Bundle = RegistryV1 {
260292 PackageName : "testPkg" ,
261- CSV : csv ,
293+ CSV : * csv ,
262294 Others : []unstructured.Unstructured {unstructuredSvc },
263295 }
264296
@@ -268,41 +300,24 @@ var _ = Describe("RegistryV1 Suite", func() {
268300
269301 By ("verifying if plain bundle has required objects" )
270302 Expect (plainBundle ).ShouldNot (BeNil ())
271- Expect (plainBundle .Objects ).To (HaveLen (4 ))
303+ Expect (plainBundle .Objects ).To (HaveLen (5 ))
304+
305+ By ("verifying olm.targetNamespaces annotation in the deployment's pod template" )
306+ dep := findObjectByName ("testDeployment" , plainBundle .Objects )
307+ Expect (dep ).NotTo (BeNil ())
308+ Expect (dep .(* appsv1.Deployment ).Spec .Template .Annotations ).To (HaveKeyWithValue ("olm.targetNamespaces" , strings .Join (watchNamespaces , "," )))
272309 })
273310
274311 It ("should convert into plain manifests successfully with own namespace" , func () {
275- csv = v1alpha1.ClusterServiceVersion {
276- ObjectMeta : metav1.ObjectMeta {
277- Name : "testCSV" ,
278- },
279- Spec : v1alpha1.ClusterServiceVersionSpec {
280- InstallModes : []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeOwnNamespace , Supported : true }},
281- InstallStrategy : v1alpha1.NamedInstallStrategy {
282- StrategySpec : v1alpha1.StrategyDetailsDeployment {
283- Permissions : []v1alpha1.StrategyDeploymentPermissions {
284- {
285- ServiceAccountName : "testServiceAccount" ,
286- Rules : []rbacv1.PolicyRule {
287- {
288- APIGroups : []string {"test" },
289- Resources : []string {"pods" },
290- Verbs : []string {"*" },
291- },
292- },
293- },
294- },
295- },
296- },
297- },
298- }
312+ csv := baseCSV .DeepCopy ()
313+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeOwnNamespace , Supported : true }}
299314
300315 By ("creating a registry v1 bundle" )
301316 watchNamespaces = []string {installNamespace }
302317 unstructuredSvc := convertToUnstructured (svc )
303318 registryv1Bundle = RegistryV1 {
304319 PackageName : "testPkg" ,
305- CSV : csv ,
320+ CSV : * csv ,
306321 Others : []unstructured.Unstructured {unstructuredSvc },
307322 }
308323
@@ -312,16 +327,24 @@ var _ = Describe("RegistryV1 Suite", func() {
312327
313328 By ("verifying if plain bundle has required objects" )
314329 Expect (plainBundle ).ShouldNot (BeNil ())
315- Expect (plainBundle .Objects ).To (HaveLen (4 ))
330+ Expect (plainBundle .Objects ).To (HaveLen (5 ))
331+
332+ By ("verifying olm.targetNamespaces annotation in the deployment's pod template" )
333+ dep := findObjectByName ("testDeployment" , plainBundle .Objects )
334+ Expect (dep ).NotTo (BeNil ())
335+ Expect (dep .(* appsv1.Deployment ).Spec .Template .Annotations ).To (HaveKeyWithValue ("olm.targetNamespaces" , strings .Join (watchNamespaces , "," )))
316336 })
317337
318338 It ("should error when multinamespace mode is supported with an empty string in target namespaces" , func () {
339+ csv := baseCSV .DeepCopy ()
340+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true }}
341+
319342 By ("creating a registry v1 bundle" )
320343 watchNamespaces = []string {"testWatchNs1" , "" }
321344 unstructuredSvc := convertToUnstructured (svc )
322345 registryv1Bundle = RegistryV1 {
323346 PackageName : "testPkg" ,
324- CSV : csv ,
347+ CSV : * csv ,
325348 Others : []unstructured.Unstructured {unstructuredSvc },
326349 }
327350
@@ -332,21 +355,15 @@ var _ = Describe("RegistryV1 Suite", func() {
332355 })
333356
334357 It ("should error when single namespace mode is disabled with more than one target namespaces" , func () {
335- csv = v1alpha1.ClusterServiceVersion {
336- ObjectMeta : metav1.ObjectMeta {
337- Name : "testCSV" ,
338- },
339- Spec : v1alpha1.ClusterServiceVersionSpec {
340- InstallModes : []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : false }},
341- },
342- }
358+ csv := baseCSV .DeepCopy ()
359+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : false }}
343360
344361 By ("creating a registry v1 bundle" )
345362 watchNamespaces = []string {"testWatchNs1" , "testWatchNs2" }
346363 unstructuredSvc := convertToUnstructured (svc )
347364 registryv1Bundle = RegistryV1 {
348365 PackageName : "testPkg" ,
349- CSV : csv ,
366+ CSV : * csv ,
350367 Others : []unstructured.Unstructured {unstructuredSvc },
351368 }
352369
@@ -357,26 +374,20 @@ var _ = Describe("RegistryV1 Suite", func() {
357374 })
358375
359376 It ("should error when all namespace mode is disabled with target namespace containing an empty string" , func () {
360- csv = v1alpha1.ClusterServiceVersion {
361- ObjectMeta : metav1.ObjectMeta {
362- Name : "testCSV" ,
363- },
364- Spec : v1alpha1.ClusterServiceVersionSpec {
365- InstallModes : []v1alpha1.InstallMode {
366- {Type : v1alpha1 .InstallModeTypeAllNamespaces , Supported : false },
367- {Type : v1alpha1 .InstallModeTypeOwnNamespace , Supported : true },
368- {Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : true },
369- {Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true },
370- },
371- },
377+ csv := baseCSV .DeepCopy ()
378+ csv .Spec .InstallModes = []v1alpha1.InstallMode {
379+ {Type : v1alpha1 .InstallModeTypeAllNamespaces , Supported : false },
380+ {Type : v1alpha1 .InstallModeTypeOwnNamespace , Supported : true },
381+ {Type : v1alpha1 .InstallModeTypeSingleNamespace , Supported : true },
382+ {Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true },
372383 }
373384
374385 By ("creating a registry v1 bundle" )
375386 watchNamespaces = []string {"" }
376387 unstructuredSvc := convertToUnstructured (svc )
377388 registryv1Bundle = RegistryV1 {
378389 PackageName : "testPkg" ,
379- CSV : csv ,
390+ CSV : * csv ,
380391 Others : []unstructured.Unstructured {unstructuredSvc },
381392 }
382393
@@ -387,12 +398,15 @@ var _ = Describe("RegistryV1 Suite", func() {
387398 })
388399
389400 It ("should propagate csv annotations to chart metadata annotation" , func () {
401+ csv := baseCSV .DeepCopy ()
402+ csv .Spec .InstallModes = []v1alpha1.InstallMode {{Type : v1alpha1 .InstallModeTypeMultiNamespace , Supported : true }}
403+
390404 By ("creating a registry v1 bundle" )
391405 watchNamespaces = []string {"testWatchNs1" , "testWatchNs2" }
392406 unstructuredSvc := convertToUnstructured (svc )
393407 registryv1Bundle = RegistryV1 {
394408 PackageName : "testPkg" ,
395- CSV : csv ,
409+ CSV : * csv ,
396410 Others : []unstructured.Unstructured {unstructuredSvc },
397411 }
398412
@@ -462,11 +476,11 @@ func convertToUnstructured(obj interface{}) unstructured.Unstructured {
462476 return unstructured.Unstructured {Object : unstructuredObj }
463477}
464478
465- func containsObject ( obj unstructured. Unstructured , result []client.Object ) client.Object {
479+ func findObjectByName ( name string , result []client.Object ) client.Object {
466480 for _ , o := range result {
467481 // Since this is a controlled env, comparing only the names is sufficient for now.
468482 // In future, compare GVKs too by ensuring its set on the unstructuredObj.
469- if o .GetName () == obj . GetName () {
483+ if o .GetName () == name {
470484 return o
471485 }
472486 }
0 commit comments