Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pod Exec command does not permit the use of stdin because it defaults to v4.channel.k8s.io protocol instead of v5 #571

Closed
rami-foo opened this issue Feb 3, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@rami-foo
Copy link

rami-foo commented Feb 3, 2025

Which project are you reporting a bug for?

kr8s

What happened?

This is the main function i am calling

class K8Pod():
    def __init__(self, name, namespace):
        self.k8 = K8()
        #note that K8 is just a wrapper over the kr8s.api #569 
        #the k8.get_pod gets a pod by partial name listing pods and getting the one with matching partial name so think of it as just a kr8s.objects.Pod object
        self.pod = self.k8.get_pod(name, namespace)

    def copy_tar(self, tar_buffer: io.BytesIO, remote_path):
        """
        Copies a tar archive from a buffer to a specified path on a Kubernetes pod.
        Args:
            tar_buffer (io.BytesIO): The buffer containing the tar archive to be copied.
            remote_path (str): The destination path on the pod where the tar archive should be extracted.
        Raises:
            KubectlError: If there is an error during the execution of the tar command on the pod.
        """
  
        exec_command = ["tar", "xvf", "-", "-C", remote_path]
        try:
            self.pod.exec(exec_command, stdin=tar_buffer, check=True)
        except Exception as e:
            logger.error("Error copying tar to pod: %s", e)
            raise KubectlError(f"Error copying tar to pod {self.pod.name}: {e}") from e
  
    def copy_file(self, file_path: Path, remote_path: Path):
        """
        Copies a file to a remote path on a Kubernetes pod.
        Args:
            file_path (Path): The local path of the file to be copied.
            remote_path (Path): The destination path on the Kubernetes pod.
        Raises:
            Exception: If there is an error during the file copy process.
        """
  
        try:
            tar_buffer = create_tar_archive(file_path, file_path.name)
            self.copy_tar(tar_buffer, remote_path)
        except Exception as e:
            logger.error("Error copying file to pod: %s", e)
            raise KubectlError(f"Error copying file to pod {self.pod.name}: {e}") from e
  

Running Kubectl exec via subprocess works completely fine, the issue it when i migrated to using the kr8s lib
I am basically calling this

k8pod= K8Pod(name, namespace)
k8pod.copy_file("X","Y")

Anything else?

2025-02-03 14:54:24,062 - utils.kubernetes_utils - ERROR - Error copying file to pod: Error copying tar to pod init-gitops: Stdin is not supported with protocol v4.channel.k8s.io, only with v5.channel.k8s.io
Traceback (most recent call last):
  File "/app/utils/kubernetes_utils.py", line 325, in copy_tar
    self.pod.exec(exec_command, stdin=tar_buffer, check=True)
  File "/usr/local/lib/python3.11/site-packages/kr8s/objects.py", line 234, in exec
    return run_sync(self.async_exec)(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/kr8s/_async_utils.py", line 119, in run_sync_inner
    return portal.call(wrapped)
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/kr8s/_async_utils.py", line 91, in call
    return self._portal.call(func, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/anyio/from_thread.py", line 290, in call
    return cast(T_Retval, self.start_task_soon(func, *args).result())
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 456, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/usr/local/lib/python3.11/site-packages/anyio/from_thread.py", line 221, in _call_func
    retval = await retval_or_awaitable
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/kr8s/_objects.py", line 1280, in async_exec
    async with ex.run() as process:
  File "/usr/local/lib/python3.11/contextlib.py", line 204, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/kr8s/_exec.py", line 73, in run
    raise ExecError(
kr8s._exceptions.ExecError: Stdin is not supported with protocol v4.channel.k8s.io, only with v5.channel.k8s.io

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/app/utils/kubernetes_utils.py", line 342, in copy_file
    self.copy_tar(tar_buffer, remote_path)
  File "/app/utils/kubernetes_utils.py", line 328, in copy_tar
    raise KubectlError(f"Error copying tar to pod {self.pod.name}: {e}") from e
utils.kubernetes_utils.KubectlError: Error copying tar to pod init-gitops: Stdin is not supported with protocol v4.channel.k8s.io, only with v5.channel.k8s.io

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/app/./main.py", line 654, in <module>
    project_automator.run()
  File "/app/./main.py", line 438, in run
    initgitops_handler.prepare_initgitops_pod()
  File "/app/scripts/pods/init_gitops_pod.py", line 98, in prepare_initgitops_pod
    self.copy_files(files)
  File "/app/scripts/pods/pod_handler.py", line 73, in copy_files
    self.pod.copy_file(Path(src).resolve(), Path(dest))
  File "/app/utils/kubernetes_utils.py", line 345, in copy_file
    raise KubectlError(f"Error copying file to pod {self.pod.name}: {e}") from e
utils.kubernetes_utils.KubectlError: Error copying file to pod init-gitops: Error copying tar to pod init-gitops: Stdin is not supported with protocol v4.channel.k8s.io, only with v5.channel.k8s.io
@rami-foo rami-foo added the bug Something isn't working label Feb 3, 2025
@jacobtomlinson
Copy link
Member

We currently do not support stdin (see #342), this is due to issues in closing stdin (see #212)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants