Currently there is unfortunatelly no way to achieve your desired state with kubectl cp
command.
If there are some undocumented features, please feel free to edit this answer and provide the solution, but currently there is no single place in documentation that could suggest the opposite.
Neither here nor in the context help of the kubectl
command, available by running kubectl cp --help
, there is no option mentioned that would modify the default operation of the kubectl cp
command, which is basically a merge of the content of the already existing directory and copied one.
$ kubectl cp --help
Copy files and directories to and from containers.
Examples:
# !!!Important Note!!!
# Requires that the 'tar' binary is present in your container
# image. If 'tar' is not present, 'kubectl cp' will fail.
#
# For advanced use cases, such as symlinks, wildcard expansion or
# file mode preservation, consider using 'kubectl exec'.
# Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
tar cf - /tmp/foo | kubectl exec -i -n <some-namespace> <some-pod> -- tar xf - -C /tmp/bar
# Copy /tmp/foo from a remote pod to /tmp/bar locally
kubectl exec -n <some-namespace> <some-pod> -- tar cf - /tmp/foo | tar xf - -C /tmp/bar
# Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace
kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir
# Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>
# Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar
# Copy /tmp/foo from a remote pod to /tmp/bar locally
kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar
Options:
-c, --container='':
Container name. If omitted, use the kubectl.kubernetes.io/default-container annotation for selecting the
container to be attached or the first container in the pod will be chosen
--no-preserve=false:
The copied file/directory's ownership and permissions will not be preserved in the container
--retries=0:
Set number of retries to complete a copy operation from a container. Specify 0 to disable or any negative
value for infinite retrying. The default is 0 (no retry).
Usage:
kubectl cp <file-spec-src> <file-spec-dest> [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
Basically the default behavior of kubectl cp
command is a merge of the content of source and destination directory. Let's say we have local directory /tmp/test
containing:
/tmp/test$ ls
different_file.txt
with single line of text "some content"
. If we copy our local /tmp/test directory
to /tmp
directory on our Pod
, which already contains test
folder with a different file, let's say testfile.txt
, the content of both directories will be merged, so our destination /tmp/test
will contain eventually:
/tmp/test# ls
different_file.txt testfile.txt
If we change the content of our local different_file.txt
to "yet another content"
and run again the command:
kubectl cp /tmp/test pod-name:/tmp
it will only override the destination different_file.txt
which is already present in the destination /tmp/test
directory.
Currently there is no way to override this default behavior.