Skip to content

fix: Namespace Sync controller should list no resources when source namespace is empty string #725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions pkg/controllers/namespacesync/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ type Reconciler struct {
TargetNamespaceFilter func(ns *corev1.Namespace) bool
}

var NamespaceHasLabelKey = func(key string) func(ns *corev1.Namespace) bool {
return func(ns *corev1.Namespace) bool {
_, ok := ns.GetLabels()[key]
return ok
}
}

func (r *Reconciler) SetupWithManager(
ctx context.Context,
mgr ctrl.Manager,
Expand Down Expand Up @@ -157,6 +150,12 @@ func (r *Reconciler) listSourceClusterClasses(
[]clusterv1.ClusterClass,
error,
) {
// Handle the empty string explicitly, because listing resources with an empty
// string namespace returns resources in all namespaces.
if r.SourceClusterClassNamespace == "" {
return nil, nil
}

ccl := &clusterv1.ClusterClassList{}
err := r.Client.List(ctx, ccl, client.InNamespace(r.SourceClusterClassNamespace))
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions pkg/controllers/namespacesync/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ func TestReconcileNewClusterClass(t *testing.T) {
}
}

func TestSourceClusterClassNamespaceEmpty(t *testing.T) {
g := NewWithT(t)

_, cleanup, err := createUniqueClusterClassAndTemplates(
sourceClusterClassNamespace,
)
g.Expect(err).ToNot(HaveOccurred())
defer func() {
g.Expect(cleanup()).To(Succeed())
}()

// This test initializes its own reconciler, instead of using the one created
// in suite_test.go, in order to configure the source namespace.
r := Reconciler{
Client: env.Client,
SourceClusterClassNamespace: "",
}

ns, err := r.listSourceClusterClasses(ctx)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(ns).To(BeEmpty())
}

func verifyClusterClassAndTemplates(
cli client.Reader,
name,
Expand Down
12 changes: 12 additions & 0 deletions pkg/controllers/namespacesync/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2024 Nutanix. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package namespacesync

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

var NamespaceHasLabelKey = func(key string) func(ns *corev1.Namespace) bool {
return func(ns *corev1.Namespace) bool {
_, ok := ns.GetLabels()[key]
return ok
}
}
61 changes: 61 additions & 0 deletions pkg/controllers/namespacesync/label_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2024 Nutanix. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package namespacesync

import (
"testing"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestNamespaceHasLabelKey(t *testing.T) {
tests := []struct {
name string
key string
ns *corev1.Namespace
want bool
}{
{
name: "match a labeled namespace",
key: "testkey",
ns: &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: "test",
Labels: map[string]string{
"testkey": "",
},
},
},
want: true,
},
{
name: "do not match if label key is not found",
key: "testkey",
ns: &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: "test",
},
},
want: false,
},
{
name: "do not match if label key is empty string",
key: "",
ns: &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: "test",
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fn := NamespaceHasLabelKey(tt.key)
if got := fn(tt.ns); got != tt.want {
t.Fatalf("got %t, want %t", got, tt.want)
}
})
}
}
Loading