diff --git a/pkg/convert/registryv1.go b/pkg/convert/registryv1.go index a4ed09fe..1cdf33b2 100644 --- a/pkg/convert/registryv1.go +++ b/pkg/convert/registryv1.go @@ -141,26 +141,26 @@ func RegistryV1ToPlain(rv1 fs.FS, installNamespace string, watchNamespaces []str return plainFS, nil } -func validateTargetNamespaces(supportedInstallModes sets.Set[string], installNamespace string, targetNamespaces []string) error { - set := sets.New[string](targetNamespaces...) - switch { - case set.Len() == 0 || (set.Len() == 1 && set.Has("")): +func validateInstallModes(supportedInstallModes sets.Set[string]) error { + if !supportedInstallModes.Has(string(v1alpha1.InstallModeTypeAllNamespaces)) { + return fmt.Errorf( + "the bundle does not support required %q install mode. Supported install modes: %v", + v1alpha1.InstallModeTypeAllNamespaces, + sets.List(supportedInstallModes), + ) + } + + return nil +} + +func validateTargetNamespaces(supportedInstallModes sets.Set[string], targetNamespaces []string) error { + set := sets.New(targetNamespaces...) + if set.Len() == 0 || (set.Len() == 1 && set.Has("")) { if supportedInstallModes.Has(string(v1alpha1.InstallModeTypeAllNamespaces)) { return nil } - case set.Len() == 1 && !set.Has(""): - if supportedInstallModes.Has(string(v1alpha1.InstallModeTypeSingleNamespace)) { - return nil - } - if supportedInstallModes.Has(string(v1alpha1.InstallModeTypeOwnNamespace)) && targetNamespaces[0] == installNamespace { - return nil - } - default: - if supportedInstallModes.Has(string(v1alpha1.InstallModeTypeMultiNamespace)) && !set.Has("") { - return nil - } } - return fmt.Errorf("supported install modes %v do not support target namespaces %v", sets.List[string](supportedInstallModes), targetNamespaces) + return fmt.Errorf("supported install modes %v do not support target namespaces %v", sets.List(supportedInstallModes), targetNamespaces) } func saNameOrDefault(saName string) string { @@ -183,15 +183,18 @@ func Convert(in RegistryV1, installNamespace string, targetNamespaces []string) supportedInstallModes.Insert(string(im.Type)) } } + + if err := validateInstallModes(supportedInstallModes); err != nil { + return nil, err + } + if len(targetNamespaces) == 0 { if supportedInstallModes.Has(string(v1alpha1.InstallModeTypeAllNamespaces)) { targetNamespaces = []string{""} - } else if supportedInstallModes.Has(string(v1alpha1.InstallModeTypeOwnNamespace)) { - targetNamespaces = []string{installNamespace} } } - if err := validateTargetNamespaces(supportedInstallModes, installNamespace, targetNamespaces); err != nil { + if err := validateTargetNamespaces(supportedInstallModes, targetNamespaces); err != nil { return nil, err } diff --git a/pkg/convert/registryv1_test.go b/pkg/convert/registryv1_test.go index 1c8a6c4d..742821c7 100644 --- a/pkg/convert/registryv1_test.go +++ b/pkg/convert/registryv1_test.go @@ -175,7 +175,7 @@ var _ = Describe("RegistryV1 Suite", func() { Name: "testCSV", }, Spec: v1alpha1.ClusterServiceVersionSpec{ - InstallModes: []v1alpha1.InstallMode{{Type: v1alpha1.InstallModeTypeMultiNamespace, Supported: true}}, + InstallModes: []v1alpha1.InstallMode{{Type: v1alpha1.InstallModeTypeAllNamespaces, Supported: true}}, InstallStrategy: v1alpha1.NamedInstallStrategy{ StrategySpec: v1alpha1.StrategyDetailsDeployment{ Permissions: []v1alpha1.StrategyDeploymentPermissions{ @@ -203,9 +203,8 @@ var _ = Describe("RegistryV1 Suite", func() { installNamespace = "testInstallNamespace" }) - It("should convert into plain manifests successfully", func() { + It("should convert a bundle with AllNamespaces install mode and empty list of target namespaces successfully", func() { By("creating a registry v1 bundle") - watchNamespaces = []string{"testWatchNs1", "testWatchNs2"} unstructuredSvc := convertToUnstructured(svc) registryv1Bundle = RegistryV1{ PackageName: "testPkg", @@ -214,51 +213,7 @@ var _ = Describe("RegistryV1 Suite", func() { } 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(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) + plainBundle, err := Convert(registryv1Bundle, installNamespace, nil) Expect(err).NotTo(HaveOccurred()) By("verifying if plain bundle has required objects") @@ -266,34 +221,9 @@ var _ = Describe("RegistryV1 Suite", func() { 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{"*"}, - }, - }, - }, - }, - }, - }, - }, - } - + It("should convert a bundle with AllNamespaces install mode and one empty target namespace successfully", func() { By("creating a registry v1 bundle") - watchNamespaces = []string{installNamespace} + watchNamespaces = []string{corev1.NamespaceAll} unstructuredSvc := convertToUnstructured(svc) registryv1Bundle = RegistryV1{ PackageName: "testPkg", @@ -310,34 +240,9 @@ var _ = Describe("RegistryV1 Suite", func() { 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", ""} - 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).To(HaveOccurred()) - Expect(plainBundle).To(BeNil()) - }) - - It("should error when single namespace mode is disabled with more than one target namespaces", func() { - csv = v1alpha1.ClusterServiceVersion{ - ObjectMeta: metav1.ObjectMeta{ - Name: "testCSV", - }, - Spec: v1alpha1.ClusterServiceVersionSpec{ - InstallModes: []v1alpha1.InstallMode{{Type: v1alpha1.InstallModeTypeSingleNamespace, Supported: false}}, - }, - } - + It("should error when a bundle supports AllNamespaces install mode, but multiple target namespaces provided", func() { By("creating a registry v1 bundle") - watchNamespaces = []string{"testWatchNs1", "testWatchNs2"} + watchNamespaces = []string{"testWatchNs1", corev1.NamespaceAll} unstructuredSvc := convertToUnstructured(svc) registryv1Bundle = RegistryV1{ PackageName: "testPkg", @@ -351,7 +256,7 @@ var _ = Describe("RegistryV1 Suite", func() { Expect(plainBundle).To(BeNil()) }) - It("should error when all namespace mode is disabled with target namespace containing an empty string", func() { + It("should error when AllNamespaces install mode is not supported", func() { csv = v1alpha1.ClusterServiceVersion{ ObjectMeta: metav1.ObjectMeta{ Name: "testCSV", @@ -367,7 +272,6 @@ var _ = Describe("RegistryV1 Suite", func() { } By("creating a registry v1 bundle") - watchNamespaces = []string{""} unstructuredSvc := convertToUnstructured(svc) registryv1Bundle = RegistryV1{ PackageName: "testPkg", @@ -376,7 +280,7 @@ var _ = Describe("RegistryV1 Suite", func() { } By("converting to plain") - plainBundle, err := Convert(registryv1Bundle, installNamespace, watchNamespaces) + plainBundle, err := Convert(registryv1Bundle, installNamespace, nil) Expect(err).To(HaveOccurred()) Expect(plainBundle).To(BeNil()) })