diff --git a/pkg/convert/registryv1.go b/pkg/convert/registryv1.go index 2d5ebe8e..a4ed09fe 100644 --- a/pkg/convert/registryv1.go +++ b/pkg/convert/registryv1.go @@ -143,15 +143,12 @@ func RegistryV1ToPlain(rv1 fs.FS, installNamespace string, watchNamespaces []str func validateTargetNamespaces(supportedInstallModes sets.Set[string], installNamespace string, targetNamespaces []string) error { set := sets.New[string](targetNamespaces...) - switch set.Len() { - case 0: + switch { + case set.Len() == 0 || (set.Len() == 1 && set.Has("")): if supportedInstallModes.Has(string(v1alpha1.InstallModeTypeAllNamespaces)) { return nil } - case 1: - if set.Has("") && supportedInstallModes.Has(string(v1alpha1.InstallModeTypeAllNamespaces)) { - return nil - } + case set.Len() == 1 && !set.Has(""): if supportedInstallModes.Has(string(v1alpha1.InstallModeTypeSingleNamespace)) { return nil } diff --git a/pkg/convert/registryv1_test.go b/pkg/convert/registryv1_test.go index 362d8a6f..1c8a6c4d 100644 --- a/pkg/convert/registryv1_test.go +++ b/pkg/convert/registryv1_test.go @@ -222,6 +222,94 @@ var _ = Describe("RegistryV1 Suite", func() { Expect(plainBundle.Objects).To(HaveLen(6)) }) + It("should convert into plain manifests successfully with single namespace", func() { + csv = v1alpha1.ClusterServiceVersion{ + ObjectMeta: metav1.ObjectMeta{ + Name: "testCSV", + }, + Spec: v1alpha1.ClusterServiceVersionSpec{ + InstallModes: []v1alpha1.InstallMode{{Type: v1alpha1.InstallModeTypeSingleNamespace, Supported: true}}, + InstallStrategy: v1alpha1.NamedInstallStrategy{ + StrategySpec: v1alpha1.StrategyDetailsDeployment{ + Permissions: []v1alpha1.StrategyDeploymentPermissions{ + { + ServiceAccountName: "testServiceAccount", + Rules: []rbacv1.PolicyRule{ + { + APIGroups: []string{"test"}, + Resources: []string{"pods"}, + Verbs: []string{"*"}, + }, + }, + }, + }, + }, + }, + }, + } + + By("creating a registry v1 bundle") + watchNamespaces = []string{"testWatchNs1"} + unstructuredSvc := convertToUnstructured(svc) + registryv1Bundle = RegistryV1{ + PackageName: "testPkg", + CSV: csv, + Others: []unstructured.Unstructured{unstructuredSvc}, + } + + By("converting to plain") + plainBundle, err := Convert(registryv1Bundle, installNamespace, watchNamespaces) + Expect(err).NotTo(HaveOccurred()) + + By("verifying if plain bundle has required objects") + Expect(plainBundle).ShouldNot(BeNil()) + Expect(plainBundle.Objects).To(HaveLen(4)) + }) + + It("should convert into plain manifests successfully with own namespace", func() { + csv = v1alpha1.ClusterServiceVersion{ + ObjectMeta: metav1.ObjectMeta{ + Name: "testCSV", + }, + Spec: v1alpha1.ClusterServiceVersionSpec{ + InstallModes: []v1alpha1.InstallMode{{Type: v1alpha1.InstallModeTypeOwnNamespace, Supported: true}}, + InstallStrategy: v1alpha1.NamedInstallStrategy{ + StrategySpec: v1alpha1.StrategyDetailsDeployment{ + Permissions: []v1alpha1.StrategyDeploymentPermissions{ + { + ServiceAccountName: "testServiceAccount", + Rules: []rbacv1.PolicyRule{ + { + APIGroups: []string{"test"}, + Resources: []string{"pods"}, + Verbs: []string{"*"}, + }, + }, + }, + }, + }, + }, + }, + } + + By("creating a registry v1 bundle") + watchNamespaces = []string{installNamespace} + unstructuredSvc := convertToUnstructured(svc) + registryv1Bundle = RegistryV1{ + PackageName: "testPkg", + CSV: csv, + Others: []unstructured.Unstructured{unstructuredSvc}, + } + + By("converting to plain") + plainBundle, err := Convert(registryv1Bundle, installNamespace, watchNamespaces) + Expect(err).NotTo(HaveOccurred()) + + By("verifying if plain bundle has required objects") + Expect(plainBundle).ShouldNot(BeNil()) + Expect(plainBundle.Objects).To(HaveLen(4)) + }) + It("should error when multinamespace mode is supported with an empty string in target namespaces", func() { By("creating a registry v1 bundle") watchNamespaces = []string{"testWatchNs1", ""} @@ -269,7 +357,12 @@ var _ = Describe("RegistryV1 Suite", func() { Name: "testCSV", }, Spec: v1alpha1.ClusterServiceVersionSpec{ - InstallModes: []v1alpha1.InstallMode{{Type: v1alpha1.InstallModeTypeAllNamespaces, Supported: false}}, + InstallModes: []v1alpha1.InstallMode{ + {Type: v1alpha1.InstallModeTypeAllNamespaces, Supported: false}, + {Type: v1alpha1.InstallModeTypeOwnNamespace, Supported: true}, + {Type: v1alpha1.InstallModeTypeSingleNamespace, Supported: true}, + {Type: v1alpha1.InstallModeTypeMultiNamespace, Supported: true}, + }, }, }