Namespaces and Names

Dealing with Namespaces and Names

Setting Namespaces and Names

Motivation

It may be useful to enforce consistency across the namespace and names of all Resources within a Project.

  • Ensure all Resources are in the correct Namespace
  • Ensure all Resources share a common naming convention
  • Copy or Fork an existing Project and change the Namespace / Names

Setting Namespace

Reference:

The Namespace for all namespaced Resources declared in the Resource Config may be set with namespace. This sets the namespace for both generated Resources (e.g. ConfigMaps and Secrets) and non-generated Resources.

Example: Set the namespace specified in the kustomization.yaml on the namespaced Resources.

Input: The kustomization.yaml and deployment.yaml files

# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: my-namespace
resources:
- deployment.yaml
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx

Applied: The Resource that is Applied to the cluster

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx-deployment
  # The namespace has been added
  namespace: my-namespace
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx

Overriding Namespaces: Setting the namespace will override the namespace on Resources if it is already set.

Propagation of the Name to Object References

Resources such as Deployments and StatefulSets may reference other Resources such as ConfigMaps and Secrets in the Pod Spec.

This sets a name prefix or suffix for both generated Resources (e.g. ConfigMaps and Secrets) and non-generated Resources.

The namePrefix or nameSuffix that is applied is propagated to references to updated resources

  • e.g. references to Secrets and ConfigMaps are updated with the namePrefix and nameSuffix.