Skip to content
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

Add exlude namespace predicate #1041

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion controllers/config/clientconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
configapi "github.com/k8ssandra/k8ssandra-operator/apis/config/v1beta1"
k8ssandraapi "github.com/k8ssandra/k8ssandra-operator/apis/k8ssandra/v1alpha1"
"github.com/k8ssandra/k8ssandra-operator/pkg/clientcache"
custompredicate "github.com/k8ssandra/k8ssandra-operator/pkg/predicate"
"github.com/k8ssandra/k8ssandra-operator/pkg/utils"
)

Expand Down Expand Up @@ -104,7 +105,7 @@ func (r *ClientConfigReconciler) SetupWithManager(mgr ctrl.Manager, cancelFunc c
}

cb := ctrl.NewControllerManagedBy(mgr).
For(&configapi.ClientConfig{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
For(&configapi.ClientConfig{}, builder.WithPredicates(predicate.GenerationChangedPredicate{}, custompredicate.NewExcludeNamespacePredicate())).
Watches(&source.Kind{Type: &corev1.Secret{}}, handler.EnqueueRequestsFromMapFunc(toMatchingClientConfig))

return cb.Complete(r)
Expand Down
3 changes: 2 additions & 1 deletion controllers/control/k8ssandratask_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

api "github.com/k8ssandra/k8ssandra-operator/apis/control/v1alpha1"
custompredicate "github.com/k8ssandra/k8ssandra-operator/pkg/predicate"
)

const (
Expand Down Expand Up @@ -352,7 +353,7 @@ func (r *K8ssandraTaskReconciler) reportInvalidSpec(
// SetupWithManager sets up the controller with the Manager.
func (r *K8ssandraTaskReconciler) SetupWithManager(mgr ctrl.Manager, clusters []cluster.Cluster) error {
cb := ctrl.NewControllerManagedBy(mgr).
For(&api.K8ssandraTask{}, builder.WithPredicates(predicate.GenerationChangedPredicate{}))
For(&api.K8ssandraTask{}, builder.WithPredicates(predicate.GenerationChangedPredicate{}, custompredicate.NewExcludeNamespacePredicate()))

kTaskLabelFilter := func(mapObj client.Object) []reconcile.Request {
requests := make([]reconcile.Request, 0)
Expand Down
3 changes: 2 additions & 1 deletion controllers/k8ssandra/k8ssandracluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package k8ssandra

import (
"context"
custompredicate "github.com/k8ssandra/k8ssandra-operator/pkg/predicate"
v1 "k8s.io/api/core/v1"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -180,7 +181,7 @@ func (r *K8ssandraClusterReconciler) afterCassandraReconciled(ctx context.Contex
// SetupWithManager sets up the controller with the Manager.
func (r *K8ssandraClusterReconciler) SetupWithManager(mgr ctrl.Manager, clusters []cluster.Cluster) error {
cb := ctrl.NewControllerManagedBy(mgr).
For(&api.K8ssandraCluster{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})) // No generation changed predicate here?
For(&api.K8ssandraCluster{}, builder.WithPredicates(predicate.GenerationChangedPredicate{}, custompredicate.NewExcludeNamespacePredicate())) // No generation changed predicate here?

clusterLabelFilter := func(mapObj client.Object) []reconcile.Request {
requests := make([]reconcile.Request, 0)
Expand Down
3 changes: 2 additions & 1 deletion controllers/medusa/medusabackupjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package medusa
import (
"context"
"fmt"
custompredicate "github.com/k8ssandra/k8ssandra-operator/pkg/predicate"
"net"
"sync"

Expand Down Expand Up @@ -268,6 +269,6 @@ func medusaBackupFinished(backup *medusav1alpha1.MedusaBackupJob) bool {
// SetupWithManager sets up the controller with the Manager.
func (r *MedusaBackupJobReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&medusav1alpha1.MedusaBackupJob{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
For(&medusav1alpha1.MedusaBackupJob{}, builder.WithPredicates(predicate.GenerationChangedPredicate{}, custompredicate.NewExcludeNamespacePredicate())).
Complete(r)
}
3 changes: 2 additions & 1 deletion controllers/medusa/medusabackupschedule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import (
"context"
"fmt"
custompredicate "github.com/k8ssandra/k8ssandra-operator/pkg/predicate"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -137,6 +138,6 @@
// SetupWithManager sets up the controller with the Manager.
func (r *MedusaBackupScheduleReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&medusav1alpha1.MedusaBackupSchedule{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
For(&medusav1alpha1.MedusaBackupSchedule{}, builder.WithPredicates(predicate.GenerationChangedPredicate{}, custompredicate.NewExcludeNamespacePredicate())).

Check warning on line 141 in controllers/medusa/medusabackupschedule_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/medusa/medusabackupschedule_controller.go#L141

Added line #L141 was not covered by tests
Complete(r)
}
3 changes: 2 additions & 1 deletion controllers/medusa/medusarestorejob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
custompredicate "github.com/k8ssandra/k8ssandra-operator/pkg/predicate"
"net"

appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -296,7 +297,7 @@ func updateMedusaRestoreInitContainer(req *medusa.RestoreRequest) error {
// SetupWithManager sets up the controller with the Manager.
func (r *MedusaRestoreJobReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&medusav1alpha1.MedusaRestoreJob{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
For(&medusav1alpha1.MedusaRestoreJob{}, builder.WithPredicates(predicate.GenerationChangedPredicate{}, custompredicate.NewExcludeNamespacePredicate())).
Complete(r)
}

Expand Down
3 changes: 2 additions & 1 deletion controllers/medusa/medusatask_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package medusa
import (
"context"
"fmt"
custompredicate "github.com/k8ssandra/k8ssandra-operator/pkg/predicate"
"net"
"sync"

Expand Down Expand Up @@ -427,6 +428,6 @@ func taskFinished(task *medusav1alpha1.MedusaTask) bool {
// SetupWithManager sets up the controller with the Manager.
func (r *MedusaTaskReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&medusav1alpha1.MedusaTask{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
For(&medusav1alpha1.MedusaTask{}, builder.WithPredicates(predicate.GenerationChangedPredicate{}, custompredicate.NewExcludeNamespacePredicate())).
Complete(r)
}
3 changes: 2 additions & 1 deletion controllers/reaper/reaper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package reaper

import (
"context"
custompredicate "github.com/k8ssandra/k8ssandra-operator/pkg/predicate"

"github.com/go-logr/logr"
cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
Expand Down Expand Up @@ -410,7 +411,7 @@ func (r *ReaperReconciler) getSecret(ctx context.Context, secretKey types.Namesp

func (r *ReaperReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&reaperapi.Reaper{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
For(&reaperapi.Reaper{}, builder.WithPredicates(predicate.GenerationChangedPredicate{}, custompredicate.NewExcludeNamespacePredicate())).
Owns(&appsv1.Deployment{}).
Owns(&corev1.Service{}).
Complete(r)
Expand Down
3 changes: 2 additions & 1 deletion controllers/replication/secret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package replication
import (
"context"
"fmt"
custompredicate "github.com/k8ssandra/k8ssandra-operator/pkg/predicate"
"github.com/k8ssandra/k8ssandra-operator/pkg/secret"
"strings"
"sync"
Expand Down Expand Up @@ -390,7 +391,7 @@ func (s *SecretSyncController) SetupWithManager(mgr ctrl.Manager, clusters []clu
}

cb := ctrl.NewControllerManagedBy(mgr).
For(&api.ReplicatedSecret{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
For(&api.ReplicatedSecret{}, builder.WithPredicates(predicate.GenerationChangedPredicate{}, custompredicate.NewExcludeNamespacePredicate())).
Watches(&source.Kind{Type: &corev1.Secret{}}, handler.EnqueueRequestsFromMapFunc(toMatchingReplicates))

for _, c := range clusters {
Expand Down
3 changes: 2 additions & 1 deletion controllers/stargate/stargate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
custompredicate "github.com/k8ssandra/k8ssandra-operator/pkg/predicate"

"github.com/go-logr/logr"
cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
Expand Down Expand Up @@ -482,7 +483,7 @@ func reconcileStargateConfigFile(
// SetupWithManager sets up the controller with the Manager.
func (r *StargateReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&api.Stargate{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
For(&api.Stargate{}, builder.WithPredicates(predicate.GenerationChangedPredicate{}, custompredicate.NewExcludeNamespacePredicate())).
Owns(&appsv1.Deployment{}).
Owns(&corev1.Service{}).
Complete(r)
Expand Down
50 changes: 50 additions & 0 deletions pkg/predicate/predicate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package predicate

import (
"os"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"strings"
)

func NewExcludeNamespacePredicate() predicate.Predicate {
var excludeWatchNamespaceEnvVar = "EXCLUDE_WATCH_NAMESPACE"

excludeWatchNamespace, found := os.LookupEnv(excludeWatchNamespaceEnvVar)
if !found {
return predicate.NewPredicateFuncs(func(object client.Object) bool {
return true
})
}
var excludeNamespacesList []string
if strings.Contains(excludeWatchNamespace, ",") {
excludeNamespacesList = strings.Split(excludeWatchNamespace, ",")
} else {
excludeNamespacesList = []string{excludeWatchNamespace}
}

return predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
return shouldBeProcessed(excludeNamespacesList, e.ObjectNew.GetNamespace())
},
DeleteFunc: func(e event.DeleteEvent) bool {
return shouldBeProcessed(excludeNamespacesList, e.Object.GetNamespace())
},
CreateFunc: func(createEvent event.CreateEvent) bool {
return shouldBeProcessed(excludeNamespacesList, createEvent.Object.GetNamespace())
},
GenericFunc: func(genericEvent event.GenericEvent) bool {
return shouldBeProcessed(excludeNamespacesList, genericEvent.Object.GetNamespace())
},
}
}

func shouldBeProcessed(excludeNamespacesList []string, namespace string) bool {
for _, excludeNs := range excludeNamespacesList {
if excludeNs == namespace {
return false
}
}
return true
}
Loading