Skip to content

Instantly share code, notes, and snippets.

@adamency
Last active July 6, 2024 13:12
Show Gist options
  • Save adamency/b92bf632dd04f02b967edd07374c5c90 to your computer and use it in GitHub Desktop.
Save adamency/b92bf632dd04f02b967edd07374c5c90 to your computer and use it in GitHub Desktop.
Move Kubernetes Resource from one namespace to another
kubectl get -n <old_namespace> <kind> <resource> -o json |\
jq '.metadata.namespace = "<new_namespace>"' |\
kubectl create -f - &&\
kubectl delete -n <old_namespace> <kind> <resource>
@adamency
Copy link
Author

adamency commented Jul 6, 2024

Beware that this will copy the status section of your resource to the new one, and depending on the kind, this may have non-intended repercussions, but this should be fine for ConfigMaps and Secrets.

@PrimeDominus
Copy link

Adding here what I read on Reddit regarding this script because I think it might be useful (and for myself to remind me because I've starred this gist):

I see what you're saying because of the &&. Still I wouldn't simply trust that a successful exit of kubectl would be enough for me to go and delete the resource. But I'm very cautious. Personally, I'd query the API again and check the status of the newly-created one first... You could use jq again, if you want to keep it in a shell script

@adamency
Copy link
Author

adamency commented Jul 6, 2024

@PrimeDominus This is what I explained in the comment above. But always a good idea to add more information :). Thanks for starring the gist !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment