Skip to content

Commit 9800cfa

Browse files
committed
add exclude namespaces flag
1 parent cf9c7fb commit 9800cfa

2 files changed

Lines changed: 49 additions & 18 deletions

File tree

cmd/deployment-tracker/main.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,26 @@ func getEnvOrDefault(key, defaultValue string) string {
3333

3434
func main() {
3535
var (
36-
kubeconfig string
37-
namespace string
38-
workers int
39-
metricsPort string
36+
kubeconfig string
37+
namespace string
38+
excludeNamespaces string
39+
workers int
40+
metricsPort string
4041
)
4142

4243
flag.StringVar(&kubeconfig, "kubeconfig", "", "path to kubeconfig file (uses in-cluster config if not set)")
4344
flag.StringVar(&namespace, "namespace", "", "namespace to monitor (empty for all namespaces)")
45+
flag.StringVar(&excludeNamespaces, "exclude-namespace", "", "namespace to exclude from monitoring (empty to include all namespaces)")
4446
flag.IntVar(&workers, "workers", 2, "number of worker goroutines")
4547
flag.StringVar(&metricsPort, "metrics-port", "9090", "port to listen to for metrics")
4648
flag.Parse()
4749

50+
// Cannot use both
51+
if namespace != "" && excludeNamespaces != "" {
52+
slog.Error("Cannot set both --namespace and --exclude-namespace")
53+
os.Exit(1)
54+
}
55+
4856
// Validate worker count
4957
if workers < 1 || workers > 100 {
5058
slog.Error("Invalid worker count, must be between 1 and 100",
@@ -143,7 +151,7 @@ func main() {
143151
cancel()
144152
}()
145153

146-
cntrl, err := controller.New(clientset, namespace, &cntrlCfg)
154+
cntrl, err := controller.New(clientset, namespace, excludeNamespaces, &cntrlCfg)
147155
if err != nil {
148156
slog.Error("Failed to create controller",
149157
"error", err)

internal/controller/controller.go

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)