Namespaces and Names

Dealing with Namespaces and Names

TL;DR

  • Set the Namespace for all Resources within a Project with namespace
  • Prefix the Names of all Resources within a Project with namePrefix
  • Suffix the Names of all Resources within a Project with nameSuffix

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.

Command / Examples

Check out the reference for commands and examples for setting namespace

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

Input: The kustomization.yaml and deployment.yaml files

  1. # kustomization.yaml
  2. apiVersion: kustomize.config.k8s.io/v1beta1
  3. kind: Kustomization
  4. namespace: my-namespace
  5. resources:
  6. - deployment.yaml
  1. # deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: nginx-deployment
  6. labels:
  7. app: nginx
  8. spec:
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - name: nginx
  19. image: nginx

Applied: The Resource that is Applied to the cluster

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: nginx
  6. name: nginx-deployment
  7. # The namespace has been added
  8. namespace: my-namespace
  9. spec:
  10. selector:
  11. matchLabels:
  12. app: nginx
  13. template:
  14. metadata:
  15. labels:
  16. app: nginx
  17. spec:
  18. containers:
  19. - image: nginx
  20. name: nginx

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

Command / Examples

Check out the nameprefix / namesuffix for commands and examples for setting nameprefix / namesuffix to kubernetes resources

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.

References

Apply will propagate the namePrefix to any place Resources within the project are referenced by other Resources including:

  • Service references from StatefulSets
  • ConfigMap references from PodSpecs
  • Secret references from PodSpecs