6

I would like to create a new namespace which is identical to the old one.

My approach would look something like this (full command below):

kubectl get <resources> -o json --namespace OLD-NAMESPACE | jq '.items[].metadata.namespace = "NEW-NAMESPACE"' | kubectl create -f  -

This basically gets all resource definitions in a json format, replaces the old namespace with the new one, and applies everything.

Unfortunately, this does not work, since the old definitions contain namespace specific things. To avoid this, there used to be an --export flag, which has been deprecated see here.

Any Idea how I could do this?

Full command:

kubectl get bindings,configmaps,endpoints,limitranges,persistentvolumeclaims,persistentvolumes,pods,podtemplates,replicationcontrollers,resourcequotas,secrets,mutatingwebhookconfigurations,validatingwebhookconfigurations,controllerrevisions,daemonsets,deployments,replicasets,statefulsets,tokenreviews,localsubjectaccessreviews,selfsubjectaccessreviews,selfsubjectrulesreviews,subjectaccessreviews,horizontalpodautoscalers,cronjobs,jobs,certificatesigningrequests,leases,endpointslices,events,ingressclasses,networkpolicies,runtimeclasses,poddisruptionbudgets,rolebindings,roles,debugattachments,csidrivers,volumeattachments -o json --namespace OLD-NAMESPACE | jq '.items[].metadata.namespace = "NEW-NAMESPACE"' | kubectl create -f  -
User12547645
  • 6,955
  • 3
  • 38
  • 69
  • Can you install a clean copy from the artifacts you have checked in to source control? `git clone` the repository if you haven't already, then `kubectl apply -n NEW-NAMESPACE` or `helm install -n NEW-NAMESPACE`? – David Maze Nov 24 '20 at 12:18

2 Answers2

3

you can try this

kubectl get all -n OLD_NAMESPACE -o yaml | sed -e 's/namespace: OLD_NAMESPACE/namespace:  NEW_NAMESPACE/' | kubectl apply -f -

this will work

if you are using a higher K8s version above 1.14 sed will work to replace the namespaces

if kubectl get all not adding all resources we can apply like

kubectl get bindings,configmaps,endpoints,limitranges,persistentvolumeclaims,persistentvolumes,pods,podtemplates,replicationcontrollers,resourcequotas,secrets,mutatingwebhookconfigurations,validatingwebhookconfigurations,controllerrevisions,daemonsets,deployments,replicasets,statefulsets,tokenreviews,localsubjectaccessreviews,selfsubjectaccessreviews,selfsubjectrulesreviews,subjectaccessreviews,horizontalpodautoscalers,cronjobs,jobs,certificatesigningrequests,leases,endpointslices,events,ingressclasses,networkpolicies,runtimeclasses,poddisruptionbudgets,rolebindings,roles,debugattachments,csidrivers,volumeattachments -n OLD_NAMESPACE -o yaml | sed -e 's/namespace: OLD_NAMESPACE/namespace:  NEW_NAMESPACE/' | kubectl apply -f -
Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102
  • 2
    Thanks for the answer! I am on minikube 1.15.1. It does not seem to work there. – User12547645 Nov 24 '20 at 13:02
  • And I get `MountVolume.SetUp failed for volume "some token" : secret "some secret" not found` – User12547645 Nov 24 '20 at 13:06
  • `spec.custerIP` this is due to already one pod running and in yaml same clusterip specified. maybe removing `clusterip` will work and k8s will assign new IP. – Harsh Manvar Nov 24 '20 at 13:49
  • This won't work and the answer is actually misleading. `kubectl get all` will neither show your all objects, nor remove any internal information. Also it will list both Pods and their deployment controllers (Deployments or StatefulSets), which in most cases you don't need. – Olesya Bolobova Nov 24 '20 at 19:50
2

For now there is no any established way to solve your task.
There are some common approaches you could try.
Also see discussion here.


If a resource is created by kubectl apply then applied yaml will be saved in a special annotation.
kubectl.kubernetes.io/last-applied-configuration
You could retrieve it like this:

kubectl get deploy example -o json | \
jq -r '.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration"' | jq .

Note that kubectl create and kubectl replace do NOT retain this annotation.
See more examples here.
Also keep in mind how this approach works with object updates.


Alternatively you could resort to direct json / yaml processing.

kubectl get deploy example -ojson | \ice 
jq 'del(.metadata.creationTimestamp, .metadata.managedFields, \
  .metadata.namespace, .metadata.ownerReferences, \
  .metadata.resourceVersion, .metadata.selfLink, \
  .metadata.uid, .status)'

Make sure that you delete all service fields (for example, Helm adds a lot of stuff).


Most probably kubectl get all won't work for you. First it actually does not show all resources.
Here are the rules for expanding all alias.

 - No cluster scoped resources
 - No namespace admin level resources (limits, quota, policy, authorization rules)
 - No resources that are potentially unrecoverable (secrets and pvc)
 - Resources that are considered "similar" to #3 should be grouped the same (configmaps)

Confusingly, this information seems to have been deleted from kubernetes docs and exists only in archived copies, but it's still relevant.

Besides that kubectl get all will return you both Pods and deployment controllers (like Deployments or StatefulSets) which is not what you want, as in most times you don't want to create you pods explicitly.

Olesya Bolobova
  • 1,573
  • 1
  • 10
  • 21