Skip to content

Commit

Permalink
cache ToolchainCluster CR only in the given namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
MatousJobanek committed Jul 26, 2024
1 parent 2f6e6a3 commit 474ff3c
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
30 changes: 30 additions & 0 deletions controllers/toolchainclustercache/namespace_predicate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package toolchainclustercache

import (
"sigs.k8s.io/controller-runtime/pkg/event"
)

// CreateAndUpdateOnlyPredicate will filter out all events out of the provided namespace
type namespacePredicate struct {
namespace string
}

// Update allows events only in the given namespace
func (p namespacePredicate) Update(e event.UpdateEvent) bool {
return e.ObjectNew.GetNamespace() == p.namespace
}

// Create allows events only in the given namespace
func (p namespacePredicate) Create(e event.CreateEvent) bool {
return e.Object.GetNamespace() == p.namespace
}

// Delete allows events only in the given namespace
func (p namespacePredicate) Delete(e event.DeleteEvent) bool {
return e.Object.GetNamespace() == p.namespace
}

// Generic allows events only in the given namespace
func (p namespacePredicate) Generic(e event.GenericEvent) bool {
return e.Object.GetNamespace() == p.namespace
}
101 changes: 101 additions & 0 deletions controllers/toolchainclustercache/namespace_predicate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package toolchainclustercache

import (
"testing"

toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
"github.com/stretchr/testify/assert"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/event"
)

func TestNamespacePredicate(t *testing.T) {
// given
predicate := namespacePredicate{
namespace: "matching-namespace",
}

tests := map[string]struct {
namespace string
expected bool
}{
"with matching namespace": {
namespace: "matching-namespace",
expected: true,
},

"without matching namespace": {
namespace: "non-matching-namespace",
expected: false,
},

"without any namespace": {
namespace: "",
expected: false,
},
}

for testName, data := range tests {
t.Run(testName, func(t *testing.T) {
tc := &toolchainv1alpha1.ToolchainCluster{
ObjectMeta: v1.ObjectMeta{
Name: "tc-name",
Namespace: data.namespace,
},
}
t.Run("update event", func(t *testing.T) {
// given
ev := event.UpdateEvent{
ObjectNew: tc,
// we don't care about the old version of the object
ObjectOld: nil,
}

// when
shouldReconcile := predicate.Update(ev)

// then
assert.Equal(t, data.expected, shouldReconcile)
})

t.Run("create event", func(t *testing.T) {
// given
ev := event.CreateEvent{
Object: tc,
}

// when
shouldReconcile := predicate.Create(ev)

// then
assert.Equal(t, data.expected, shouldReconcile)
})

t.Run("generic event", func(t *testing.T) {
// given
ev := event.GenericEvent{
Object: tc,
}

// when
shouldReconcile := predicate.Generic(ev)

// then
assert.Equal(t, data.expected, shouldReconcile)
})

t.Run("delete event", func(t *testing.T) {
// given
ev := event.DeleteEvent{
Object: tc,
}

// when
shouldReconcile := predicate.Delete(ev)

// then
assert.Equal(t, data.expected, shouldReconcile)
})
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand All @@ -23,13 +24,14 @@ func NewReconciler(mgr manager.Manager, namespace string, timeout time.Duration)
client: mgr.GetClient(),
scheme: mgr.GetScheme(),
clusterCacheService: clusterCacheService,
namespace: namespace,

Check warning on line 27 in controllers/toolchainclustercache/toolchaincluster_cache_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/toolchainclustercache/toolchaincluster_cache_controller.go#L27

Added line #L27 was not covered by tests
}
}

// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&toolchainv1alpha1.ToolchainCluster{}).
For(&toolchainv1alpha1.ToolchainCluster{}, builder.WithPredicates(namespacePredicate{namespace: r.namespace})).

Check warning on line 34 in controllers/toolchainclustercache/toolchaincluster_cache_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/toolchainclustercache/toolchaincluster_cache_controller.go#L34

Added line #L34 was not covered by tests
Complete(r)
}

Expand All @@ -38,6 +40,7 @@ type Reconciler struct {
client client.Client
scheme *runtime.Scheme
clusterCacheService cluster.ToolchainClusterService
namespace string
}

// Reconcile reads that state of the cluster for a ToolchainCluster object and makes changes based on the state read
Expand Down

0 comments on commit 474ff3c

Please sign in to comment.