This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Adding namespace when converting bundle from registryv1 to plain
Currently, if the objects being passed through the bundle do not have their namespaces set, they were being created on the "rukpak-system" namespace. This PR fixes this issue for only "registryV1" bundles. It sets the namespaces to installNs if not provided by default. Signed-off-by: Varsha Prasad Narsing <[email protected]>
- Loading branch information
1 parent
5791e52
commit e38fae1
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package convert | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func TestRegistryV1Converter(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "RegstryV1 suite") | ||
} | ||
|
||
var _ = Describe("RegistryV1 Suite", func() { | ||
var _ = Describe("Convert", func() { | ||
var ( | ||
registryv1Bundle RegistryV1 | ||
installNamespace string | ||
targetNamespaces []string | ||
) | ||
Context("Should set the namespaces of object correctly", func() { | ||
var ( | ||
svc corev1.Service | ||
csv v1alpha1.ClusterServiceVersion | ||
) | ||
BeforeEach(func() { | ||
csv = v1alpha1.ClusterServiceVersion{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "testCSV", | ||
}, | ||
Spec: v1alpha1.ClusterServiceVersionSpec{ | ||
InstallModes: []v1alpha1.InstallMode{{Type: v1alpha1.InstallModeTypeAllNamespaces, Supported: true}}, | ||
}, | ||
} | ||
svc = corev1.Service{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "testService", | ||
}, | ||
} | ||
svc.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Service"}) | ||
installNamespace = "testInstallNamespace" | ||
}) | ||
|
||
It("should set the namespace to installnamespace if not available", func() { | ||
By("creating a registry v1 bundle") | ||
unstructuredSvc := convertToUnstructured(svc) | ||
registryv1Bundle = RegistryV1{ | ||
PackageName: "testPkg", | ||
CSV: csv, | ||
Others: []unstructured.Unstructured{unstructuredSvc}, | ||
} | ||
|
||
By("converting to plain") | ||
plainBundle, err := Convert(registryv1Bundle, installNamespace, targetNamespaces) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("verifying if plain bundle has required objects") | ||
Expect(plainBundle).NotTo(BeNil()) | ||
Expect(len(plainBundle.Objects)).To(BeEquivalentTo(2)) | ||
|
||
By("verifying if ns has been set correctly") | ||
found, resObj := containsObject(unstructuredSvc, plainBundle.Objects) | ||
Expect(found).To(BeTrue()) | ||
Expect(resObj).NotTo(BeNil()) | ||
Expect(resObj.GetNamespace()).To(BeEquivalentTo(installNamespace)) | ||
}) | ||
|
||
It("should not override namespace if already available", func() { | ||
By("creating a registry v1 bundle") | ||
svc.SetNamespace("otherNs") | ||
unstructuredSvc := convertToUnstructured(svc) | ||
unstructuredSvc.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Service"}) | ||
|
||
registryv1Bundle = RegistryV1{ | ||
PackageName: "testPkg", | ||
CSV: csv, | ||
Others: []unstructured.Unstructured{unstructuredSvc}, | ||
} | ||
|
||
By("converting to plain") | ||
plainBundle, err := Convert(registryv1Bundle, installNamespace, targetNamespaces) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("verifying if plain bundle has required objects") | ||
Expect(plainBundle).NotTo(BeNil()) | ||
Expect(len(plainBundle.Objects)).To(BeEquivalentTo(2)) | ||
|
||
By("verifying if ns has been set correctly") | ||
found, resObj := containsObject(unstructuredSvc, plainBundle.Objects) | ||
Expect(found).To(BeTrue()) | ||
Expect(resObj).NotTo(BeNil()) | ||
Expect(resObj.GetNamespace()).To(BeEquivalentTo("otherNs")) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
func convertToUnstructured(obj interface{}) unstructured.Unstructured { | ||
unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&obj) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(unstructuredObj).NotTo(BeNil()) | ||
return unstructured.Unstructured{Object: unstructuredObj} | ||
} | ||
|
||
func containsObject(obj unstructured.Unstructured, result []client.Object) (bool, client.Object) { | ||
for _, o := range result { | ||
// Since this is a controlled env, comparing only the names is sufficient for now. | ||
// In future, compare GVKs too by ensuring its set on the unstructuredObj. | ||
if o.GetName() == obj.GetName() { | ||
return true, o | ||
} | ||
} | ||
return false, nil | ||
} |