Skip to content

Commit 74c9cf7

Browse files
committed
fix: List no resources when source namespace is empty string
Previously, the controller listed resources in all namespaces when the source namespace was an empty string.
1 parent 43c1047 commit 74c9cf7

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

pkg/controllers/namespacesync/controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ func (r *Reconciler) listSourceClusterClasses(
150150
[]clusterv1.ClusterClass,
151151
error,
152152
) {
153+
// Handle the empty string explicitly, because listing resources with an empty
154+
// string namespace returns resources in all namespaces.
155+
if r.SourceClusterClassNamespace == "" {
156+
return []clusterv1.ClusterClass{}, nil
157+
}
158+
153159
ccl := &clusterv1.ClusterClassList{}
154160
err := r.Client.List(ctx, ccl, client.InNamespace(r.SourceClusterClassNamespace))
155161
if err != nil {

pkg/controllers/namespacesync/controller_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,29 @@ func TestReconcileNewClusterClass(t *testing.T) {
7373
}
7474
}
7575

76+
func TestSourceClusterClassNamespaceEmpty(t *testing.T) {
77+
g := NewWithT(t)
78+
79+
_, cleanup, err := createUniqueClusterClassAndTemplates(
80+
sourceClusterClassNamespace,
81+
)
82+
g.Expect(err).ToNot(HaveOccurred())
83+
defer func() {
84+
g.Expect(cleanup()).To(Succeed())
85+
}()
86+
87+
// This test initializes its own reconciler, instead of using the one created
88+
// in suite_test.go, in order to configure the source namespace.
89+
r := Reconciler{
90+
Client: env.Client,
91+
SourceClusterClassNamespace: "",
92+
}
93+
94+
ns, err := r.listSourceClusterClasses(ctx)
95+
g.Expect(err).ToNot(HaveOccurred())
96+
g.Expect(ns).To(BeEmpty())
97+
}
98+
7699
func verifyClusterClassAndTemplates(
77100
cli client.Reader,
78101
name,

pkg/controllers/namespacesync/label.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright 2024 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
13
package namespacesync
24

35
import corev1 "k8s.io/api/core/v1"

pkg/controllers/namespacesync/label_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright 2024 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
13
package namespacesync
24

35
import (

0 commit comments

Comments
 (0)