When you need to copy a secret from one namespace to another in a Kubernetes cluster you may face the below error.
error: the namespace from the provided object "" does not match the namespace "". You must pass --namespace to perform this operation
The issue can be found because of the origin namespace that is referenced inside the secret. In order to bypass you can export the secret and change the referenced namespace.
kubectl get secret mysecret --namespace=ns1 -o yaml > export.yaml
In order to get it work you will need to change the namespace according to your new namespace in the export.yaml and apply it.
namespace: ns2