How to parameterize copy command for kubenetes api using python?

11/20/2019

I am trying to create a copy of a file within a container using kubernetes python api.

Below is the function I want to create:

def create_file_copy(self, file_name, pod_name, pod_namespace=None):
        if pod_namespace is None:
            pod_namespace = self.svc_ns
        stream(self.v1.connect_get_namespaced_pod_exec, name = pod_name, namespace = self.svc_ns ,command=['/bin/sh', '-c', 'cp file_name file_name_og'], stderr=True, stdin=True, stdout=True, tty=True)

NOTE: self.v1 is a kubernetes client api object which can access the kubernetes api methods.

My question is around how do I parameterize file_name in "cp file_name file_name_og" in the command parameter ?

No an expert in linux commands so any help is appreciated. Thanks

-- user_11077035
kubernetes
linux
python

1 Answer

11/20/2019

Assuming that both file_name and file_name_og are to be parameterized, this will make the cp copy command be constructed dynamically from function's arguments:

def create_file_copy(self, file_name, file_name_og, pod_name, pod_namespace=None):
    if pod_namespace is None:
        pod_namespace = self.svc_ns
    stream(self.v1.connect_get_namespaced_pod_exec, name = pod_name, namespace = self.svc_ns ,command=['/bin/sh', '-c', 'cp "' + file_name + '" "' + file_name_og + '"'], stderr=True, stdin=True, stdout=True, tty=True)
-- gears
Source: StackOverflow