Namespace Filtering in Icinga for Kubernetes: How We Built It

by | Nov 26, 2026

Icinga for Kubernetes synchronizes every namespace in a cluster. Starting with version 0.5.0, you can change that: a new namespaces option takes either a list of namespaces to include, or a list of namespaces to exclude.

I expected a quick config option and a filter in the sync. It turned out the Kubernetes API doesn’t offer what you would want here. This post explains how namespace filtering works and why we built it the way we did.

What does the configuration look like?

You set exactly one of two lists in config.yml. Either include:

namespaces:
  include:
    - production
    - staging

Or exclude:

namespaces:
  exclude:
    - development
    - testing

If you set neither, all namespaces are synchronized, just like before. Setting both, using an invalid namespace name, or listing a namespace twice stops the daemon at startup.

Using environment variables for systemd instances

If you run several Icinga for Kubernetes daemons as systemd instances, they all share one config.yml. In that case, set the filter in each instance’s environment file instead. Either include:

ICINGA_FOR_KUBERNETES_NAMESPACES_INCLUDE=productionstaging

Or exclude:

ICINGA_FOR_KUBERNETES_NAMESPACES_EXCLUDE=development,testing

The problem: Kubernetes can’t watch a set of namespaces

Icinga for Kubernetes uses informers from client-go to list and watch resources. An informer is either bound to exactly one namespace or to the whole cluster.

There is no request for “namespaces A and B”.

That is why include and exclude ended up as two completely different implementations behind the same option.

How include works

The idea is simple: if we can only watch one namespace at a time, we create one informer per namespace.

Five included namespaces means five pod informers, five deployment informers, and so on.

The sync code was written around a single informer per resource though. To avoid touching every sync, we added an Informers type. It bundles several informers of the same resource and offers the few operations the syncs actually need:

  • adding an event handler
  • running and waiting for the initial sync
  • looking up an object by key
  • listing objects, either all of them or per namespace and label selector

Since every informer only sees its own namespace, their stores never overlap. A key lookup loops over them and returns the first match. That is linear in the number of namespaces, but include lists are usually short, so a simple loop is fine.

Replacing the typed listers

One sync needed extra work. ServicePodSync links services to their pods and used the typed listers from client-go. Those only exist per informer, so they can’t see across five of them.

Informers.GetByKey and Informers.ListByNamespace replace them. ListByNamespace works the same way the listers did: it uses the namespace index and filters by label selector. A service is still only matched with pods in its own namespace.

How exclude works

For exclude, one informer per namespace would be the wrong way around. We would first have to find every namespace that is not excluded, and then keep track of new ones.

Instead, we keep one cluster-wide informer per resource and add a field selector for every excluded namespace:

metadata.namespace!=development,metadata.namespace!=testing

The API server applies the selector, so objects from excluded namespaces are never sent to Icinga for Kubernetes at all.

What about the namespaces themselves?

Namespaces are a special case. They are cluster-scoped, so there is no namespace to restrict the informer to. Field selectors can exclude names, but they can’t express “one of these names”, so they don’t work for include.

Fortunately, Kubernetes sets the label kubernetes.io/metadata.name on every namespace. Label selectors support in and notin, so one informer covers both modes:

kubernetes.io/metadata.name in (production,staging)
kubernetes.io/metadata.name notin (development,testing)

Prometheus queries only cover selected namespaces

The filter also applies to pod metrics from Prometheus. Before, Icinga for Kubernetes queried metrics for the whole cluster and threw away the results for pods it didn’t know about.

Now the queries are restricted to the selected namespaces, so Prometheus doesn’t do work that is discarded afterwards anyway.

What about cluster-scoped resources?

Two questions didn’t have an obvious answer.

Should nodes and persistent volumes follow the filter?

They don’t belong to any namespace. We decided to always synchronize them completely.

What about events in default?

Kubernetes records events about nodes and persistent volumes in the default namespace. If your filter leaves out default, those events are gone as well.

Someone who writes exclude: [default] might want exactly that. But without those events, nodes and persistent volumes have no events at all. For now, the filter applies as written and this is documented as a limitation.

What happens when the filter changes?

If you narrow the filter and restart, objects of namespaces that are now filtered out are removed from the database. Icinga for Kubernetes already cleans up objects that no longer exist in the cluster, and from its point of view, a filtered-out namespace has vanished.

Events are the exception. They stay until they expire.

This is also why you should only run one instance per cluster. Two instances with different filters would see each other’s objects as vanished and keep deleting them.

What I would do differently

I would start by looking at what the Kubernetes API can and can’t do, before thinking about the configuration. The config looked like one feature with two lists, but it required two entirely different implementations under the hood.

Starting from the API wouldn’t have changed the config, though. And you shouldn’t have to care that include and exclude work differently.

What is next

Namespace filtering ships with Icinga for Kubernetes 0.5.0. If you want to try it before the release, it is already on main in the GitHub repository. All options are described in the configuration docs.

With namespace filtering done, it makes sense to give Icinga for Kubernetes read access to only the namespaces it synchronizes. That is what I’m working on next: resources it isn’t allowed to read will be skipped with a warning instead of blocking the whole sync.

If you are new to the project, the introduction to Icinga for Kubernetes and the overview of Kubernetes monitoring with Icinga are good places to start. And if you have an opinion on how events in default should behave, let us know on the issue tracker.

You May Also Like…

 

Subscribe to our Newsletter

A monthly digest of the latest Icinga news, releases, articles and community topics.