Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Fix target namespace validation on conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Mikalai Radchuk <[email protected]>
  • Loading branch information
m1kola authored and joelanford committed May 15, 2024
1 parent 31691d9 commit bd62a21
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
9 changes: 3 additions & 6 deletions pkg/convert/registryv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
95 changes: 94 additions & 1 deletion pkg/convert/registryv1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""}
Expand Down Expand Up @@ -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},
},
},
}

Expand Down

0 comments on commit bd62a21

Please sign in to comment.