Skip to content
Latchkey

Kustomize namespace: Set Namespace on All Resources

The namespace field stamps a single namespace onto every namespaced resource in the build.

One line moves a whole overlay into a target namespace. Kustomize also rewrites references such as ServiceAccount subjects in RoleBindings to match.

What it does

Setting namespace adds or overwrites metadata.namespace on every namespaced resource the build produces. Kustomize also updates known cross-references, for example the namespace of subjects in a RoleBinding that point at a ServiceAccount in the same build.

Common usage

kustomization.yaml
# overlays/prod/kustomization.yaml
namespace: prod-web
resources:
  - ../../base

Behavior

Resource typeEffect
Deployment, Service, etc.metadata.namespace set to the value
Namespace objectIts metadata.name is also set to the value
RoleBinding subjectsSubject namespace rewritten to match
ClusterRole, ClusterRoleBindingCluster-scoped, namespace not added

In CI

Use namespace per overlay so dev and prod land in separate namespaces from one base. Confirm the namespace exists or include a Namespace manifest, since kubectl apply -k will fail if the target namespace is missing and not created in the same apply.

Common errors in CI

"namespaces \"prod-web\" not found" at apply time means the namespace is not created by this build; add a Namespace resource or create it first. The transformer does not touch cluster-scoped objects, so do not expect a ClusterRole to gain a namespace. If references are not rewritten, the subject kind or name does not match a resource in the same build.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →