Skip to content

Namespace Name Patterns

Source: hello-pepr-regex-ns-all

When a Capability defines namespaces: [], you can use .InNamespaceRegex() to match namespace names using regular expressions. This is useful for filtering namespaces by naming patterns without explicitly listing them.

  • Matching namespaces based on naming conventions or patterns
  • Applying policies across multiple namespaces that follow a naming standard
  • Filtering namespaces without explicitly listing each one
  • Enforcing governance on dynamically created namespaces

Important: This filter matches namespace names, not resource names. To match resource names, see regex name. The Capability must define namespaces: [] to enable cluster-wide namespace matching with regex patterns.

View full example on Github

Match Namespaces Starting with “default”

Section titled “Match Namespaces Starting with “default””
When(a.ConfigMap)
.IsCreated()
.InNamespaceRegex(/^default/)
.Mutate(function mutateDef(request) {
request.SetAnnotation("def", "seen");
});

Match Namespaces Ending with “-ns-all”

Section titled “Match Namespaces Ending with “-ns-all””
When(a.ConfigMap)
.IsCreated()
.InNamespaceRegex(/-ns-all$/)
.Watch(async cm => {
await K8s(kind.ConfigMap).Apply({
metadata: {
name: cm.metadata.name,
namespace: cm.metadata.namespace,
annotations: {
ns: "seen",
},
},
data: {
"cm-uid": cm.metadata.uid,
},
});
});
When(a.ConfigMap)
.IsCreated()
.Mutate(function mutateNon(request) {
request.SetAnnotation("non", "seen");
});