openshift/openshift-restclient-python

How to upload files to a pod?

Netzeband opened this issue · 2 comments

Hello,

I want to substitute bash scripts using the oc CLI by python scripts. Therefore I'm looking for a way to substitute the oc rsync command with this python package. Is this possible?
If not, what would be the alternative?
(I'm aware of the solution to call oc rsync in a python subprocess).

Thanks for your help and best regards

I don't think there's a primitive API for this, I believe the way that kubectl cp works is by creating a tar archive of the source file, and passing it via stdin to a tar command executed in the pod (ie via kubectl exec). I'm not totally sure how the oc rsync command works, but it looks like it's just passing an argument to rsync that tells it how to open a remote shell in the container. That approach won't work with the python client, since the python client is just a library and not an binary that rsync can call out to.

You should be able to replicate the kubectl cp approach using the exec API (I suggest using the underlying kubernetes-client/python library for this though, the dynamic client doesn't handle the stranger APIs like exec nearly as well). There's an example for how to use exec (though not for copying) here: https://github.com/kubernetes-client/python/blob/02ef5be4ecead787961037b236ae498944040b43/examples/pod_exec.py

It might make more sense to just shell out to oc rsync for this though, rather than trying to replicate all the logic.

Closing for now, feel free to reopen if further discussion is required.