@@ -52,20 +52,9 @@ type Controller struct {
5252}
5353
5454// New creates a new deployment tracker controller.
55- func New (clientset kubernetes.Interface , namespace string , cfg * Config ) (* Controller , error ) {
55+ func New (clientset kubernetes.Interface , namespace string , excludeNamespaces string , cfg * Config ) (* Controller , error ) {
5656 // Create informer factory
57- var factory informers.SharedInformerFactory
58- if namespace == "" {
59- factory = informers .NewSharedInformerFactory (clientset ,
60- 30 * time .Second ,
61- )
62- } else {
63- factory = informers .NewSharedInformerFactoryWithOptions (
64- clientset ,
65- 30 * time .Second ,
66- informers .WithNamespace (namespace ),
67- )
68- }
57+ factory := createInformerFactory (clientset , namespace , excludeNamespaces )
6958
7059 podInformer := factory .Core ().V1 ().Pods ().Informer ()
7160
@@ -488,6 +477,40 @@ func getCacheKey(dn, digest string) string {
488477 return dn + "||" + digest
489478}
490479
480+ // createInformerFactory creates a shared informer factory with the given resync period.
481+ // If excludeNamespaces is non-empty, it will exclude those namespaces from being watched.
482+ // If namespace is non-empty, it will only watch that namespace.
483+ func createInformerFactory (clientset kubernetes.Interface , namespace string , excludeNamespaces string ) informers.SharedInformerFactory {
484+ var factory informers.SharedInformerFactory
485+ if namespace != "" {
486+ factory = informers .NewSharedInformerFactoryWithOptions (
487+ clientset ,
488+ 30 * time .Second ,
489+ informers .WithNamespace (namespace ),
490+ )
491+ } else if excludeNamespaces != "" {
492+ excludedNamespacesList := strings .Split (excludeNamespaces , "," )
493+ for i := 0 ; i < len (excludedNamespacesList ); i ++ {
494+ excludedNamespacesList [i ] = fmt .Sprintf ("metadata.namespace!=%s" , strings .TrimSpace (excludedNamespacesList [i ]))
495+ }
496+ tweakListOptions := func (options * metav1.ListOptions ) {
497+ options .FieldSelector = strings .Join (excludedNamespacesList , "," )
498+ }
499+
500+ factory = informers .NewSharedInformerFactoryWithOptions (
501+ clientset ,
502+ 30 * time .Second ,
503+ informers .WithTweakListOptions (tweakListOptions ),
504+ )
505+ } else {
506+ factory = informers .NewSharedInformerFactory (clientset ,
507+ 30 * time .Second ,
508+ )
509+ }
510+
511+ return factory
512+ }
513+
491514// getARDeploymentName converts the pod's metadata into the correct format
492515// for the deployment name for the artifact registry (this is not the same
493516// as the K8s deployment's name!
0 commit comments