set-namespace
set-namespace #
Overview #
The set-namespace
function update or add namespace to all namespaced
resources. Kubernetes supports multiple virtual clusters backed by the same
physical cluster through namespaces.
Namespaces are often used in the following scenarios:
- Separate resources between environments (prod, staging and test).
- Separate resources between different team or users to divide resource quota.
You can learn more about namespace [here][namespace].
Usage #
This function can be used with any KRM function orchestrators (e.g. kpt).
For all namespaced resurces, the set-namespace
function adds the namespace
if metadata.namespace
doesn’t exist. Otherwise, it updates the existing value.
It will skip the resources that are known to be cluster-scoped (e.g. Node
, CustomResourceDefinitions
, ClusterRole
). Whether a resource is namespaced
is determined by the OpenAPI schema. If the API path
contains namespaces/{namespace}
then the resource is considered namespaced.
Otherwise, it’s not. Currently, this function is using API version 1.20.4.
In addition to updating the metadata.namespace
field for applicable resources,
by default the function will also update the [fields][commonnamespace] that
target the namespace. There are a few cases that worth pointing out:
- If there is a
Namespace
resource, itsmetadata.name
field will be updated. - If there’s a
RoleBinding
orClusterRoleBinding
resource, the function will update the namespace in theServiceAccount
if one of the following are true:- the subject element
name
isdefault
. - the subject element
name
matches the name of aServiceAccount
resource declared in the package.
- the subject element
In the following example, the set-namespace
function will update:
subjects[0].namespace
sincesubjects[0].name
isdefault
.subjects[1].namespace
sincesubjects[1].name
matches aServiceAccount
name declared in the package.
apiVersion: v1
kind: ServiceAccount
metadata:
name: service-account
namespace: original-namespace