From f6f3432ea1dfc329bfeae3f5e936e9bc5e1bef2e Mon Sep 17 00:00:00 2001 From: Erik Godding Boye Date: Fri, 6 Jan 2023 21:46:24 +0100 Subject: [PATCH] refactor: replace code with features now available in c-r Ref. https://github.com/kubernetes-sigs/controller-runtime/pull/2054 and https://github.com/kubernetes-sigs/controller-runtime/pull/2031 --- controllers/predicates.go | 18 ---------------- controllers/suite_test.go | 4 ++-- controllers/workload_controller.go | 4 ++-- internal/cluster/cache_unstructured.go | 29 -------------------------- main.go | 4 ++-- 5 files changed, 6 insertions(+), 53 deletions(-) delete mode 100644 internal/cluster/cache_unstructured.go diff --git a/controllers/predicates.go b/controllers/predicates.go index 432566d8..d81ef951 100644 --- a/controllers/predicates.go +++ b/controllers/predicates.go @@ -106,21 +106,3 @@ var jobIsFinished = predicate.NewPredicateFuncs(func(object client.Object) bool var cisVulnerabilityOverflow = predicate.NewPredicateFuncs(func(object client.Object) bool { return object.(*stasv1alpha1.ContainerImageScan).HasVulnerabilityOverflow() }) - -// TODO: Replace with new func added to c-r in https://github.com/kubernetes-sigs/controller-runtime/pull/2031 -func notPredicate(p predicate.Predicate) predicate.Predicate { - return predicate.Funcs{ - CreateFunc: func(event event.CreateEvent) bool { - return !p.Create(event) - }, - DeleteFunc: func(event event.DeleteEvent) bool { - return !p.Delete(event) - }, - UpdateFunc: func(event event.UpdateEvent) bool { - return !p.Update(event) - }, - GenericFunc: func(event event.GenericEvent) bool { - return !p.Generic(event) - }, - } -} diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 3a2c3e9f..6fd0c13e 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -18,6 +18,7 @@ import ( "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/cluster" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/envtest" "sigs.k8s.io/controller-runtime/pkg/envtest/komega" @@ -25,7 +26,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log/zap" stasv1alpha1 "github.com/statnett/image-scanner-operator/api/v1alpha1" - "github.com/statnett/image-scanner-operator/internal/cluster" "github.com/statnett/image-scanner-operator/internal/pod" "github.com/statnett/image-scanner-operator/pkg/operator" //+kubebuilder:scaffold:imports @@ -90,7 +90,7 @@ var _ = BeforeSuite(func() { } k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ - NewClient: cluster.NewCachingClient, + NewClient: cluster.ClientBuilderWithOptions(cluster.ClientOptions{CacheUnstructured: true}), Scheme: scheme.Scheme, }) Expect(err).NotTo(HaveOccurred()) diff --git a/controllers/workload_controller.go b/controllers/workload_controller.go index 8c1cbcfa..ecc17059 100644 --- a/controllers/workload_controller.go +++ b/controllers/workload_controller.go @@ -69,8 +69,8 @@ func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error { ignoreDeletionPredicate(), )). WithEventFilter(predicate.And( - notPredicate(systemNamespace), - notPredicate(managedByImageScanner), + predicate.Not(systemNamespace), + predicate.Not(managedByImageScanner), )). Watches(&source.Kind{Type: &stasv1alpha1.ContainerImageScan{}}, &handler.EnqueueRequestForOwner{OwnerType: &corev1.Pod{}}, diff --git a/internal/cluster/cache_unstructured.go b/internal/cluster/cache_unstructured.go deleted file mode 100644 index 6d18d4bc..00000000 --- a/internal/cluster/cache_unstructured.go +++ /dev/null @@ -1,29 +0,0 @@ -package cluster - -import ( - "k8s.io/client-go/rest" - "sigs.k8s.io/controller-runtime/pkg/cache" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/cluster" -) - -// NewCachingClient is an alternative implementation of controller-runtime's -// default client for manager.Manager. -// The only difference is that this implementation sets `CacheUnstructured` to `true` to -// cache unstructured objects. -// TODO: Hoping to get this into controller-runtime, ref. https://github.com/kubernetes-sigs/controller-runtime/pull/2054 -func NewCachingClient(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) { - c, err := client.New(config, options) - if err != nil { - return nil, err - } - - return client.NewDelegatingClient(client.NewDelegatingClientInput{ - CacheReader: cache, - Client: c, - UncachedObjects: uncachedObjects, - CacheUnstructured: true, - }) -} - -var _ cluster.NewClientFunc = NewCachingClient diff --git a/main.go b/main.go index ac4ae63c..50b4e14a 100644 --- a/main.go +++ b/main.go @@ -18,11 +18,11 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/cache" + "sigs.k8s.io/controller-runtime/pkg/cluster" "sigs.k8s.io/controller-runtime/pkg/healthz" stasv1alpha1 "github.com/statnett/image-scanner-operator/api/v1alpha1" "github.com/statnett/image-scanner-operator/controllers" - "github.com/statnett/image-scanner-operator/internal/cluster" "github.com/statnett/image-scanner-operator/internal/metrics" "github.com/statnett/image-scanner-operator/internal/pod" "github.com/statnett/image-scanner-operator/internal/resources" @@ -97,7 +97,7 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) options := ctrl.Options{ - NewClient: cluster.NewCachingClient, + NewClient: cluster.ClientBuilderWithOptions(cluster.ClientOptions{CacheUnstructured: true}), Scheme: scheme, MetricsBindAddress: metricsAddr, Port: 9443,